diff --git a/packages/expect/src/jest-expect.ts b/packages/expect/src/jest-expect.ts index e6b5a71e1183..c3349220d11a 100644 --- a/packages/expect/src/jest-expect.ts +++ b/packages/expect/src/jest-expect.ts @@ -581,7 +581,6 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { `expected error not to be instance of ${name}`, expected, thrown, - false, ) } @@ -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, ) } diff --git a/test/core/test/__snapshots__/jest-expect.test.ts.snap b/test/core/test/__snapshots__/jest-expect.test.ts.snap index e451d39894c0..f9d09b20a239 100644 --- a/test/core/test/__snapshots__/jest-expect.test.ts.snap +++ b/test/core/test/__snapshots__/jest-expect.test.ts.snap @@ -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 + ++ Received: +"hello"", + "expected": "stringContainingCustom", + "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 + ++ Received: +"hello"", + "expected": "stringContainingCustom", + "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 + ++ Received: +[Error: hello]", + "expected": "stringContainingCustom", + "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", +} +`; diff --git a/test/core/test/jest-expect.test.ts b/test/core/test/jest-expect.test.ts index 814853ff3a6a..58e0848a64a3 100644 --- a/test/core/test/jest-expect.test.ts +++ b/test/core/test/jest-expect.test.ts @@ -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)))