From 7ad8e3fd205f8d7791651b9d4847db5b36d9e307 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Fri, 1 Jul 2022 14:33:18 +0300 Subject: [PATCH] fix: correctly create snapshot for rejected errors (#1567) --- .../vitest/src/integrations/snapshot/chai.ts | 28 ++++++++++++++----- .../test/__snapshots__/snapshot.test.ts.snap | 2 ++ test/core/test/snapshot-inline.test.ts | 12 +++++++- test/core/test/snapshot.test.ts | 12 +++++++- 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/packages/vitest/src/integrations/snapshot/chai.ts b/packages/vitest/src/integrations/snapshot/chai.ts index 595bee4344b3..1fb14f045bd3 100644 --- a/packages/vitest/src/integrations/snapshot/chai.ts +++ b/packages/vitest/src/integrations/snapshot/chai.ts @@ -10,15 +10,27 @@ export function getSnapshotClient(): SnapshotClient { return _client } -const getErrorString = (expected: () => void) => { +const getErrorMessage = (err: unknown) => { + if (err instanceof Error) + return err.message + + return err +} + +const getErrorString = (expected: () => void | Error, promise: string | undefined) => { + if (typeof expected !== 'function') { + if (!promise) + throw new Error(`expected must be a function, received ${typeof expected}`) + + // when "promised", it receives thrown error + return getErrorMessage(expected) + } + try { expected() } catch (e) { - if (e instanceof Error) - return e.message - - return e + return getErrorMessage(e) } throw new Error('snapshot function didn\'t threw') @@ -81,9 +93,10 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => { function (this: Record, message?: string) { const expected = utils.flag(this, 'object') const test = utils.flag(this, 'vitest-test') + const promise = utils.flag(this, 'promise') as string | undefined const errorMessage = utils.flag(this, 'message') getSnapshotClient().assert({ - received: getErrorString(expected), + received: getErrorString(expected, promise), test, message, errorMessage, @@ -97,9 +110,10 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => { const expected = utils.flag(this, 'object') const error = utils.flag(this, 'error') const test = utils.flag(this, 'vitest-test') + const promise = utils.flag(this, 'promise') as string | undefined const errorMessage = utils.flag(this, 'message') getSnapshotClient().assert({ - received: getErrorString(expected), + received: getErrorString(expected, promise), test, message, inlineSnapshot, diff --git a/test/core/test/__snapshots__/snapshot.test.ts.snap b/test/core/test/__snapshots__/snapshot.test.ts.snap index 3287a50633fc..9178db8a946e 100644 --- a/test/core/test/__snapshots__/snapshot.test.ts.snap +++ b/test/core/test/__snapshots__/snapshot.test.ts.snap @@ -36,6 +36,8 @@ exports[`throwing 3`] = ` } `; +exports[`throwing 4`] = `"omega"`; + exports[`with big array 1`] = ` { "this": { diff --git a/test/core/test/snapshot-inline.test.ts b/test/core/test/snapshot-inline.test.ts index fda2031e98a2..4a8bbb22fc2a 100644 --- a/test/core/test/snapshot-inline.test.ts +++ b/test/core/test/snapshot-inline.test.ts @@ -59,7 +59,7 @@ test('template literal', () => { `) }) -test('throwing inline snapshots', () => { +test('throwing inline snapshots', async () => { expect(() => { throw new Error('omega') }).toThrowErrorMatchingInlineSnapshot('"omega"') @@ -99,6 +99,16 @@ test('throwing inline snapshots', () => { with newlines" `) + + await expect(async () => { + throw new Error('omega') + }).rejects.toThrowErrorMatchingInlineSnapshot('"omega"') +}) + +test('throwing expect should be a function', async () => { + expect(() => { + expect(new Error('omega')).toThrowErrorMatchingInlineSnapshot() + }).toThrow(/expected must be a function/) }) test('properties inline snapshot', () => { diff --git a/test/core/test/snapshot.test.ts b/test/core/test/snapshot.test.ts index 2fed1d2106c6..4c71e84124d9 100644 --- a/test/core/test/snapshot.test.ts +++ b/test/core/test/snapshot.test.ts @@ -30,7 +30,7 @@ test('with big string', () => { }).toMatchSnapshot() }) -test('throwing', () => { +test('throwing', async () => { expect(() => { throw new Error('omega') }).toThrowErrorMatchingSnapshot() @@ -44,6 +44,16 @@ test('throwing', () => { // eslint-disable-next-line no-throw-literal throw { error: 'omega' } }).toThrowErrorMatchingSnapshot() + + await expect(async () => { + throw new Error('omega') + }).rejects.toThrowErrorMatchingSnapshot() +}) + +test('throwing expect should be a function', async () => { + expect(() => { + expect(new Error('omega')).toThrowErrorMatchingSnapshot() + }).toThrow(/expected must be a function/) }) test('properties snapshot', () => {