Skip to content

Commit

Permalink
fix: also handle toContain
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Feb 22, 2024
1 parent 34ac7d0 commit cc3d708
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
actual.value,
)
}
// handle simple case on our own using `this.assert` to include diff in error message
if (typeof actual === 'string' && typeof item === 'string') {
return this.assert(
actual.includes(item),
`expected #{this} to contain #{exp}`,
`expected #{this} not to contain #{exp}`,
item,
actual,
)
}
// make "actual" indexable to have compatibility with jest
if (actual != null && typeof actual !== 'string')
utils.flag(this, 'object', Array.from(actual as Iterable<unknown>))
Expand Down
15 changes: 14 additions & 1 deletion test/core/test/__snapshots__/jest-expect.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,24 @@ exports[`toMatch/toContain diff 1`] = `
- world
+ hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello",
"expected": "world",
"message": "expected 'hellohellohellohellohellohellohellohe…' to match 'world'",
"message": "expected 'hellohellohellohellohellohellohellohe…' to contain 'world'",
}
`;
exports[`toMatch/toContain diff 2`] = `
{
"actual": "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello",
"diff": "- Expected
+ Received
- world
+ hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello",
"expected": "world",
"message": "expected 'hellohellohellohellohellohellohellohe…' to match 'world'",
}
`;
exports[`toMatch/toContain diff 3`] = `
{
"actual": "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello",
"diff": "- Expected:
Expand Down
1 change: 1 addition & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,7 @@ it('asymmetric matcher error', () => {
})

it('toMatch/toContain diff', () => {
snapshotError(() => expect('hello'.repeat(20)).toContain('world'))
snapshotError(() => expect('hello'.repeat(20)).toMatch('world'))
snapshotError(() => expect('hello'.repeat(20)).toMatch(/world/))
})
Expand Down

0 comments on commit cc3d708

Please sign in to comment.