Skip to content

Commit

Permalink
fix(expect): improve toThrow(asymmetricMatcher) failure message (#5000
Browse files Browse the repository at this point in the history
)
  • Loading branch information
hi-ogawa authored Jan 23, 2024
1 parent a9a486f commit a199ac2
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 3 deletions.
4 changes: 1 addition & 3 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,6 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
`expected error not to be instance of ${name}`,
expected,
thrown,
false,
)
}

Expand All @@ -601,9 +600,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
thrown && matcher.asymmetricMatch(thrown),
'expected error to match asymmetric matcher',
'expected error not to match asymmetric matcher',
matcher.toString(),
matcher,
thrown,
false,
)
}

Expand Down
83 changes: 83 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,86 @@ exports[`asymmetric matcher error 16`] = `
"message": "expected 'hello' to deeply equal StringContaining{…}",
}
`;
exports[`asymmetric matcher error 17`] = `
{
"actual": "hello",
"diff": null,
"expected": "StringContaining "xx"",
"message": "expected error to match asymmetric matcher",
}
`;
exports[`asymmetric matcher error 18`] = `
{
"actual": "hello",
"diff": "- Expected:
stringContainingCustom<xx>
+ Received:
"hello"",
"expected": "stringContainingCustom<xx>",
"message": "expected error to match asymmetric matcher",
}
`;
exports[`asymmetric matcher error 19`] = `
{
"actual": "hello",
"diff": null,
"expected": "StringContaining "ll"",
"message": "expected error not to match asymmetric matcher",
}
`;
exports[`asymmetric matcher error 20`] = `
{
"actual": "hello",
"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 error to match asymmetric matcher",
}
`;
exports[`asymmetric matcher error 23`] = `
{
"actual": "[Error: hello]",
"diff": "- Expected:
[Function MyError1]
+ Received:
[Error: hello]",
"expected": "[Function MyError1]",
"message": "expected error to be instance of MyError1",
}
`;
25 changes: 25 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,31 @@ 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')))

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

// error constructor
class MyError1 extends Error {}
class MyError2 extends Error {}

snapshotError(() => expect(() => {
throw new MyError2('hello')
}).toThrow(MyError1))
})

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

0 comments on commit a199ac2

Please sign in to comment.