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): improve toThrow(asymmetricMatcher) failure message #5000

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,9 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
const matcher = expected as any as AsymmetricMatcher<any>
return this.assert(
thrown && matcher.asymmetricMatch(thrown),
'expected error to match asymmetric matcher',
'expected error not to match asymmetric matcher',
matcher.toString(),
'expected thrown value #{act} to match asymmetric matcher: #{exp}',
'expected thrown value #{act} not to match asymmetric matcher: #{exp}',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with using #{act} (short for "actual") here since currently showDiff is false (5th argument of this.assert) and the detail is not visible to users on terminal.

Looking at the history of whether enable showDiff or not, it thought this might be a performance concern of object comparison #2828 #3582 ("concordance" might expected to fixed this, but later reverted and showDiff became false), but considering toEqual and normal assertions showing diff already, would it be okay to put this showDiff back to true (i.e. default)?

matcher,
thrown,
false,
)
Expand Down
36 changes: 36 additions & 0 deletions test/core/test/__snapshots__/jest-expect.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,39 @@ exports[`asymmetric matcher error 16`] = `
"message": "expected 'hello' to deeply equal StringContaining{…}",
}
`;

exports[`asymmetric matcher error 17`] = `
{
"actual": "hello",
"diff": undefined,
"expected": "StringContaining "xx"",
"message": "expected thrown value 'hello' to match asymmetric matcher: StringContaining "xx"",
}
`;

exports[`asymmetric matcher error 18`] = `
{
"actual": "hello",
"diff": undefined,
"expected": "stringContainingCustom<xx>",
"message": "expected thrown value 'hello' to match asymmetric matcher: stringContainingCustom<xx>",
}
`;

exports[`asymmetric matcher error 19`] = `
{
"actual": "hello",
"diff": undefined,
"expected": "StringContaining "ll"",
"message": "expected thrown value 'hello' not to match asymmetric matcher: StringContaining "ll"",
}
`;

exports[`asymmetric matcher error 20`] = `
{
"actual": "hello",
"diff": undefined,
"expected": "stringContainingCustom<ll>",
"message": "expected thrown value 'hello' not to match asymmetric matcher: stringContainingCustom<ll>",
}
`;
10 changes: 10 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,16 @@ it('asymmetric matcher error', () => {

// simple truncation if pretty-format is too long
snapshotError(() => expect('hello').toEqual(expect.stringContaining('a'.repeat(40))))

// error message on `toThrow(asymmetricMatcher)` failure
function throwError() {
// eslint-disable-next-line no-throw-literal
throw 'hello'
}
snapshotError(() => expect(throwError).toThrow(expect.stringContaining('xx')))
snapshotError(() => expect(throwError).toThrow((expect as any).stringContainingCustom('xx')))
snapshotError(() => expect(throwError).not.toThrow(expect.stringContaining('ll')))
snapshotError(() => expect(throwError).not.toThrow((expect as any).stringContainingCustom('ll')))
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
})

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