Skip to content

Commit

Permalink
fix(expect): show diff on toContain/toMatch assertion error (#5267)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Feb 23, 2024
1 parent 558bb88 commit 8ee59f0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 24 deletions.
4 changes: 0 additions & 4 deletions docs/api/expect.md
Expand Up @@ -544,10 +544,6 @@ test('top fruits', () => {
})
```

::: tip
If the value in the error message is too truncated, you can increase [chaiConfig.truncateThreshold](/config/#chaiconfig-truncatethreshold) in your config file.
:::

## toMatchObject

- **Type:** `(received: object | array) => Awaitable<void>`
Expand Down
24 changes: 20 additions & 4 deletions packages/expect/src/jest-expect.ts
Expand Up @@ -170,10 +170,16 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
)
})
def('toMatch', function (expected: string | RegExp) {
if (typeof expected === 'string')
return this.include(expected)
else
return this.match(expected)
const actual = this._obj as string
return this.assert(
typeof expected === 'string'
? actual.includes(expected)
: actual.match(expected),
`expected #{this} to match #{exp}`,
`expected #{this} not to match #{exp}`,
expected,
actual,
)
})
def('toContain', function (item) {
const actual = this._obj as Iterable<unknown> | string | Node | DOMTokenList
Expand Down Expand Up @@ -203,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
39 changes: 39 additions & 0 deletions test/core/test/__snapshots__/jest-expect.test.ts.snap
Expand Up @@ -310,3 +310,42 @@ exports[`asymmetric matcher error 23`] = `
"message": "expected error to be instance of MyError1",
}
`;
exports[`toMatch/toContain diff 1`] = `
{
"actual": "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello",
"diff": "- Expected
+ Received
- world
+ hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello",
"expected": "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:
/world/
+ Received:
"hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello"",
"expected": "/world/",
"message": "expected 'hellohellohellohellohellohellohellohe…' to match /world/",
}
`;
39 changes: 23 additions & 16 deletions test/core/test/jest-expect.test.ts
Expand Up @@ -993,25 +993,26 @@ it('toHaveProperty error diff', () => {
`)
})

function snapshotError(f: () => unknown) {
try {
f()
}
catch (error) {
const e = processError(error)
expect({
message: e.message,
diff: e.diff,
expected: e.expected,
actual: e.actual,
}).toMatchSnapshot()
return
}
expect.unreachable()
}

it('asymmetric matcher error', () => {
setupColors(getDefaultColors())

function snapshotError(f: () => unknown) {
try {
f()
return expect.unreachable()
}
catch (error) {
const e = processError(error)
expect({
message: e.message,
diff: e.diff,
expected: e.expected,
actual: e.actual,
}).toMatchSnapshot()
}
}

expect.extend({
stringContainingCustom(received: unknown, other: string) {
return {
Expand Down Expand Up @@ -1084,4 +1085,10 @@ it('asymmetric matcher error', () => {
}).toThrow(MyError1))
})

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/))
})

it('timeout', () => new Promise(resolve => setTimeout(resolve, 500)))

0 comments on commit 8ee59f0

Please sign in to comment.