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 1 commit
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
5 changes: 2 additions & 3 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,11 +599,10 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
const matcher = expected as any as AsymmetricMatcher<any>
return this.assert(
thrown && matcher.asymmetricMatch(thrown),
'expected thrown value #{act} to match asymmetric matcher: #{exp}',
'expected thrown value #{act} not to match asymmetric matcher: #{exp}',
'expected error to match asymmetric matcher',
'expected error not to match asymmetric matcher',
matcher,
thrown,
false,
)
}

Expand Down
50 changes: 42 additions & 8 deletions test/core/test/__snapshots__/jest-expect.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -231,35 +231,69 @@ exports[`asymmetric matcher error 16`] = `
exports[`asymmetric matcher error 17`] = `
{
"actual": "hello",
"diff": undefined,
"diff": null,
"expected": "StringContaining "xx"",
"message": "expected thrown value 'hello' to match asymmetric matcher: StringContaining "xx"",
"message": "expected error to match asymmetric matcher",
Copy link
Contributor Author

@hi-ogawa hi-ogawa Jan 20, 2024

Choose a reason for hiding this comment

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

Hmm, I just tested showDiff: true, but diff is not showing up for this case:

 FAIL  |core| test/jest-expect.test.ts > repro
AssertionError: expected error to match asymmetric matcher
  test/jest-expect.test.ts:1080:35
    1078| 
    1079| it.only("repro", () => {
    1080|   expect(() => { throw "hello" }).toThrow(expect.stringContaining("xx"))
       |                                   ^
    1081| });
    1082| 

I'll read up more on how diff generation is decided.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It like this (odd) case leads to omitDifference here

expectedType = a.getExpectedType()
// Primitive types boolean and number omit difference below.
// For example, omit difference for expect.stringMatching(regexp)
omitDifference = expectedType === 'string'

}
`;

exports[`asymmetric matcher error 18`] = `
{
"actual": "hello",
"diff": undefined,
"diff": "- Expected:
stringContainingCustom<xx>

+ Received:
"hello"",
"expected": "stringContainingCustom<xx>",
"message": "expected thrown value 'hello' to match asymmetric matcher: stringContainingCustom<xx>",
"message": "expected error to match asymmetric matcher",
}
`;

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

exports[`asymmetric matcher error 20`] = `
{
"actual": "hello",
"diff": undefined,
"diff": "- Expected:
stringContainingCustom<ll>

+ Received:
"hello"",
"expected": "stringContainingCustom<ll>",
"message": "expected error not to match asymmetric matcher",
}
`;

exports[`asymmetric matcher error 21`] = `
{
"actual": "[Error: hello]",
"diff": "- Expected:
StringContaining "ll"

+ Received:
[Error: hello]",
"expected": "StringContaining "ll"",
"message": "expected error to match asymmetric matcher",
}
`;

exports[`asymmetric matcher error 22`] = `
{
"actual": "[Error: hello]",
"diff": "- Expected:
stringContainingCustom<ll>

+ Received:
[Error: hello]",
"expected": "stringContainingCustom<ll>",
"message": "expected thrown value 'hello' not to match asymmetric matcher: stringContainingCustom<ll>",
"message": "expected error to match asymmetric matcher",
}
`;
7 changes: 7 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,13 @@ it('asymmetric matcher error', () => {
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

snapshotError(() => expect(() => {
throw new Error('hello')
}).toThrow(expect.stringContaining('ll')))
snapshotError(() => expect(() => {
throw new Error('hello')
}).toThrow((expect as any).stringContainingCustom('ll')))
})

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