From 613425bc688e96ffc1114f87c12f3056be3acecd Mon Sep 17 00:00:00 2001 From: ibuibu Date: Sun, 20 Nov 2022 22:35:52 +0900 Subject: [PATCH] Add fail tests --- .../src/__tests__/toThrowMatchers.test.ts | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) 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', () => {