Skip to content

Commit a199ac2

Browse files
authoredJan 23, 2024
fix(expect): improve toThrow(asymmetricMatcher) failure message (#5000)
1 parent a9a486f commit a199ac2

File tree

3 files changed

+109
-3
lines changed

3 files changed

+109
-3
lines changed
 

‎packages/expect/src/jest-expect.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,6 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
581581
`expected error not to be instance of ${name}`,
582582
expected,
583583
thrown,
584-
false,
585584
)
586585
}
587586

@@ -601,9 +600,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
601600
thrown && matcher.asymmetricMatch(thrown),
602601
'expected error to match asymmetric matcher',
603602
'expected error not to match asymmetric matcher',
604-
matcher.toString(),
603+
matcher,
605604
thrown,
606-
false,
607605
)
608606
}
609607

‎test/core/test/__snapshots__/jest-expect.test.ts.snap

+83
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,86 @@ exports[`asymmetric matcher error 16`] = `
227227
"message": "expected 'hello' to deeply equal StringContaining{…}",
228228
}
229229
`;
230+
231+
exports[`asymmetric matcher error 17`] = `
232+
{
233+
"actual": "hello",
234+
"diff": null,
235+
"expected": "StringContaining "xx"",
236+
"message": "expected error to match asymmetric matcher",
237+
}
238+
`;
239+
240+
exports[`asymmetric matcher error 18`] = `
241+
{
242+
"actual": "hello",
243+
"diff": "- Expected:
244+
stringContainingCustom<xx>
245+
246+
+ Received:
247+
"hello"",
248+
"expected": "stringContainingCustom<xx>",
249+
"message": "expected error to match asymmetric matcher",
250+
}
251+
`;
252+
253+
exports[`asymmetric matcher error 19`] = `
254+
{
255+
"actual": "hello",
256+
"diff": null,
257+
"expected": "StringContaining "ll"",
258+
"message": "expected error not to match asymmetric matcher",
259+
}
260+
`;
261+
262+
exports[`asymmetric matcher error 20`] = `
263+
{
264+
"actual": "hello",
265+
"diff": "- Expected:
266+
stringContainingCustom<ll>
267+
268+
+ Received:
269+
"hello"",
270+
"expected": "stringContainingCustom<ll>",
271+
"message": "expected error not to match asymmetric matcher",
272+
}
273+
`;
274+
275+
exports[`asymmetric matcher error 21`] = `
276+
{
277+
"actual": "[Error: hello]",
278+
"diff": "- Expected:
279+
StringContaining "ll"
280+
281+
+ Received:
282+
[Error: hello]",
283+
"expected": "StringContaining "ll"",
284+
"message": "expected error to match asymmetric matcher",
285+
}
286+
`;
287+
288+
exports[`asymmetric matcher error 22`] = `
289+
{
290+
"actual": "[Error: hello]",
291+
"diff": "- Expected:
292+
stringContainingCustom<ll>
293+
294+
+ Received:
295+
[Error: hello]",
296+
"expected": "stringContainingCustom<ll>",
297+
"message": "expected error to match asymmetric matcher",
298+
}
299+
`;
300+
301+
exports[`asymmetric matcher error 23`] = `
302+
{
303+
"actual": "[Error: hello]",
304+
"diff": "- Expected:
305+
[Function MyError1]
306+
307+
+ Received:
308+
[Error: hello]",
309+
"expected": "[Function MyError1]",
310+
"message": "expected error to be instance of MyError1",
311+
}
312+
`;

‎test/core/test/jest-expect.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,31 @@ it('asymmetric matcher error', () => {
10571057

10581058
// simple truncation if pretty-format is too long
10591059
snapshotError(() => expect('hello').toEqual(expect.stringContaining('a'.repeat(40))))
1060+
1061+
// error message on `toThrow(asymmetricMatcher)` failure
1062+
function throwError() {
1063+
// eslint-disable-next-line no-throw-literal
1064+
throw 'hello'
1065+
}
1066+
snapshotError(() => expect(throwError).toThrow(expect.stringContaining('xx')))
1067+
snapshotError(() => expect(throwError).toThrow((expect as any).stringContainingCustom('xx')))
1068+
snapshotError(() => expect(throwError).not.toThrow(expect.stringContaining('ll')))
1069+
snapshotError(() => expect(throwError).not.toThrow((expect as any).stringContainingCustom('ll')))
1070+
1071+
snapshotError(() => expect(() => {
1072+
throw new Error('hello')
1073+
}).toThrow(expect.stringContaining('ll')))
1074+
snapshotError(() => expect(() => {
1075+
throw new Error('hello')
1076+
}).toThrow((expect as any).stringContainingCustom('ll')))
1077+
1078+
// error constructor
1079+
class MyError1 extends Error {}
1080+
class MyError2 extends Error {}
1081+
1082+
snapshotError(() => expect(() => {
1083+
throw new MyError2('hello')
1084+
}).toThrow(MyError1))
10601085
})
10611086

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

0 commit comments

Comments
 (0)
Please sign in to comment.