From a9556555e840f0b69c105a2a904107b53554c735 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 5 May 2022 18:37:01 +0300 Subject: [PATCH] fix: show custom error message if snapshot failed (#1237) --- .../vitest/src/integrations/snapshot/chai.ts | 41 +++++++++++++++++-- .../src/integrations/snapshot/client.ts | 28 +++++++++++-- 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/packages/vitest/src/integrations/snapshot/chai.ts b/packages/vitest/src/integrations/snapshot/chai.ts index c462e43179f2..d7952fc3f736 100644 --- a/packages/vitest/src/integrations/snapshot/chai.ts +++ b/packages/vitest/src/integrations/snapshot/chai.ts @@ -36,7 +36,15 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => { message = properties properties = undefined } - getSnapshotClient().assert(expected, test, message, false, properties) + const errorMessage = utils.flag(this, 'message') + getSnapshotClient().assert({ + received: expected, + test, + message, + isInline: false, + properties, + errorMessage, + }) }, ) } @@ -54,7 +62,17 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => { } if (inlineSnapshot) inlineSnapshot = stripSnapshotIndentation(inlineSnapshot) - getSnapshotClient().assert(expected, test, message, true, properties, inlineSnapshot, error) + const errorMessage = utils.flag(this, 'message') + getSnapshotClient().assert({ + received: expected, + test, + message, + isInline: true, + properties, + inlineSnapshot, + error, + errorMessage, + }) }, ) utils.addMethod( @@ -63,7 +81,13 @@ export const SnapshotPlugin: ChaiPlugin = (chai, utils) => { function (this: Record, message?: string) { const expected = utils.flag(this, 'object') const test = utils.flag(this, 'vitest-test') - getSnapshotClient().assert(getErrorString(expected), test, message) + const errorMessage = utils.flag(this, 'message') + getSnapshotClient().assert({ + received: getErrorString(expected), + test, + message, + errorMessage, + }) }, ) utils.addMethod( @@ -73,7 +97,16 @@ 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') - getSnapshotClient().assert(getErrorString(expected), test, message, true, undefined, inlineSnapshot, error) + const errorMessage = utils.flag(this, 'message') + getSnapshotClient().assert({ + received: getErrorString(expected), + test, + message, + inlineSnapshot, + isInline: true, + error, + errorMessage, + }) }, ) } diff --git a/packages/vitest/src/integrations/snapshot/client.ts b/packages/vitest/src/integrations/snapshot/client.ts index 8863bc16a083..dfa45f008498 100644 --- a/packages/vitest/src/integrations/snapshot/client.ts +++ b/packages/vitest/src/integrations/snapshot/client.ts @@ -12,6 +12,17 @@ export interface Context { fullTitle?: string } +interface AssertOptions { + received: unknown + test?: Test + message?: string + isInline?: boolean + properties?: object + inlineSnapshot?: string + error?: Error + errorMessage?: string +} + export class SnapshotClient { test: Test | undefined snapshotState: SnapshotState | undefined @@ -46,7 +57,18 @@ export class SnapshotClient { this.test = undefined } - assert(received: unknown, test = this.test, message?: string, isInline = false, properties?: object, inlineSnapshot?: string, error?: Error): void { + assert(options: AssertOptions): void { + const { + test = this.test, + message, + isInline = false, + properties, + inlineSnapshot, + error, + errorMessage, + } = options + let { received } = options + if (!test) throw new Error('Snapshot cannot be used outside of test') @@ -62,7 +84,7 @@ export class SnapshotClient { received = deepMergeSnapshot(received, properties) } catch (err: any) { - err.message = 'Snapshot mismatched' + err.message = errorMessage || 'Snapshot mismatched' throw err } } @@ -87,7 +109,7 @@ export class SnapshotClient { expect(actual.trim()).equals(expected ? expected.trim() : '') } catch (error: any) { - error.message = `Snapshot \`${key || 'unknown'}\` mismatched` + error.message = errorMessage || `Snapshot \`${key || 'unknown'}\` mismatched` throw error } }