Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(expect): show diff on toContain/toMatch assertion error #5267

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions docs/api/expect.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)))
Loading