From b3026f10bf4329ac5a9d45758f03cadc35ddf635 Mon Sep 17 00:00:00 2001 From: ibuibu Date: Tue, 15 Nov 2022 21:12:50 +0900 Subject: [PATCH] Add snapshot tests --- .../toThrowMatchers.test.ts.snap | 54 +++++++++++++++++++ .../src/__tests__/toThrowMatchers.test.ts | 31 +++++++++++ 2 files changed, 85 insertions(+) diff --git a/packages/expect/src/__tests__/__snapshots__/toThrowMatchers.test.ts.snap b/packages/expect/src/__tests__/__snapshots__/toThrowMatchers.test.ts.snap index 0b2df1702e07..7685325f8f7f 100644 --- a/packages/expect/src/__tests__/__snapshots__/toThrowMatchers.test.ts.snap +++ b/packages/expect/src/__tests__/__snapshots__/toThrowMatchers.test.ts.snap @@ -146,6 +146,33 @@ 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:19 +`; + +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:335:21 +`; + +exports[`toThrow error message and cause fail isNot true, 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:323:19 +`; + exports[`toThrow error-message fail isNot false 1`] = ` expect(received).toThrow(expected) @@ -454,6 +481,33 @@ 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:19 +`; + +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:335:21 +`; + +exports[`toThrowError error message and cause fail isNot true, 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:323:19 +`; + 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..da49400b9a58 100644 --- a/packages/expect/src/__tests__/toThrowMatchers.test.ts +++ b/packages/expect/src/__tests__/toThrowMatchers.test.ts @@ -307,6 +307,37 @@ describe.each(['toThrowError', 'toThrow'] as const)('%s', toThrow => { } }); }); + + describe('fail', () => { + test('isNot false, incorrect message', () => { + expect(() => + jestExpect(() => { + throw new Error('bad', {cause: errorB}); + })[toThrow](expected), + ).toThrowErrorMatchingSnapshot(); + }); + + test('isNot true, incorrect message', () => { + expect(() => + jestExpect(() => { + throw new Error('bad', {cause: errorB}); + })[toThrow](expected), + ).toThrowErrorMatchingSnapshot(); + }); + + 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 { + expect(() => + jestExpect(() => { + throw new Error('good', {cause: errorA}); + })[toThrow](expected), + ).toThrowErrorMatchingSnapshot(); + } + }); + }); }); describe('asymmetric', () => {