From 963becfbdece40a86bd5717a5cda9318c857412d Mon Sep 17 00:00:00 2001 From: ibuibu Date: Tue, 15 Nov 2022 21:54:42 +0900 Subject: [PATCH] Add snapshot tests --- .../toThrowMatchers.test.ts.snap | 36 +++++++++++++++++++ .../src/__tests__/toThrowMatchers.test.ts | 27 +++++++++++--- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/toThrowMatchers.test.ts.snap b/packages/expect/src/__tests__/__snapshots__/toThrowMatchers.test.ts.snap index 0b2df1702e07..d5056fde55e8 100644 --- a/packages/expect/src/__tests__/__snapshots__/toThrowMatchers.test.ts.snap +++ b/packages/expect/src/__tests__/__snapshots__/toThrowMatchers.test.ts.snap @@ -146,6 +146,24 @@ Received message: "apple" at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74) `; +exports[`toThrow error message and cause fail isNot false, incorrect message 1`] = ` +expect(received).toThrow(expected) + +Expected message and cause: "{ message: good, cause: { message: B, cause: { message: A }}}" +Received message and cause: "{ message: bad, cause: { message: B, cause: { message: A }}}" + + at packages/expect/src/__tests__/toThrowMatchers.test.ts:315:21 +`; + +exports[`toThrow error message and cause fail isNot true, incorrect cause 1`] = ` +expect(received).toThrow(expected) + +Expected message and cause: "{ message: good, cause: { message: B, cause: { message: A }}}" +Received message and cause: "{ message: good, cause: { message: A }}" + + at packages/expect/src/__tests__/toThrowMatchers.test.ts:323:21 +`; + exports[`toThrow error-message fail isNot false 1`] = ` expect(received).toThrow(expected) @@ -454,6 +472,24 @@ Received message: "apple" at jestExpect (packages/expect/src/__tests__/toThrowMatchers-test.js:24:74) `; +exports[`toThrowError error message and cause fail isNot false, incorrect message 1`] = ` +expect(received).toThrowError(expected) + +Expected message and cause: "{ message: good, cause: { message: B, cause: { message: A }}}" +Received message and cause: "{ message: bad, cause: { message: B, cause: { message: A }}}" + + at packages/expect/src/__tests__/toThrowMatchers.test.ts:315:21 +`; + +exports[`toThrowError error message and cause fail isNot true, incorrect cause 1`] = ` +expect(received).toThrowError(expected) + +Expected message and cause: "{ message: good, cause: { message: B, cause: { message: A }}}" +Received message and cause: "{ message: good, cause: { message: A }}" + + at packages/expect/src/__tests__/toThrowMatchers.test.ts:323:21 +`; + exports[`toThrowError error-message fail isNot false 1`] = ` expect(received).toThrowError(expected) diff --git a/packages/expect/src/__tests__/toThrowMatchers.test.ts b/packages/expect/src/__tests__/toThrowMatchers.test.ts index f3c20c41a48e..61b3a297d0a1 100644 --- a/packages/expect/src/__tests__/toThrowMatchers.test.ts +++ b/packages/expect/src/__tests__/toThrowMatchers.test.ts @@ -297,16 +297,35 @@ 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), + ).toThrowErrorMatchingSnapshot(); + }); + + test('isNot true, incorrect cause', () => { + expect(() => + jestExpect(() => { + throw new Error('good', {cause: errorA}); + })[toThrow](expected), + ).toThrowErrorMatchingSnapshot(); + }); + } + }); }); describe('asymmetric', () => {