diff --git a/packages/expect/src/__tests__/toThrowMatchers.test.ts b/packages/expect/src/__tests__/toThrowMatchers.test.ts index f3c20c41a48e..6d7435b7ca11 100644 --- a/packages/expect/src/__tests__/toThrowMatchers.test.ts +++ b/packages/expect/src/__tests__/toThrowMatchers.test.ts @@ -297,16 +297,39 @@ describe.each(['toThrowError', 'toThrow'] as const)('%s', toThrow => { }); test('isNot true, incorrect cause', () => { - // less than v16 does not yet support Error.cause - if (Number(process.version.split('.')[0].slice(1)) < 16) { - expect(true).toBe(true); - } else { + // only v16 or higher support Error.cause + if (Number(process.version.split('.')[0].slice(1)) >= 16) { jestExpect(() => { throw new Error('good', {cause: errorA}); }).not[toThrow](expected); } }); }); + + describe('fail', () => { + // only v16 or higher support Error.cause + if (Number(process.version.split('.')[0].slice(1)) >= 16) { + test('isNot false, incorrect message', () => { + expect(() => + jestExpect(() => { + throw new Error('bad', {cause: errorB}); + })[toThrow](expected), + ).toThrow( + /^(?=.*Expected message and cause: ).*Received message and cause: /s, + ); + }); + + test('isNot true, incorrect cause', () => { + expect(() => + jestExpect(() => { + throw new Error('good', {cause: errorA}); + })[toThrow](expected), + ).toThrow( + /^(?=.*Expected message and cause: ).*Received message and cause: /s, + ); + }); + } + }); }); describe('asymmetric', () => {