diff --git a/docs/guide/cli.md b/docs/guide/cli.md index 8a7a47e6e639..ebd67860a6fe 100644 --- a/docs/guide/cli.md +++ b/docs/guide/cli.md @@ -96,6 +96,7 @@ Run only [benchmark](https://vitest.dev/guide/features.html#benchmarking-experim | `--inspect-brk` | Enables Node.js inspector with break | | `--bail ` | Stop test execution when given number of tests have failed | | `--retry ` | Retry the test specific number of times if it fails | +| `--expand-snapshot-diff` | Show full diff when snapshot fails | | `--typecheck [options]` | Custom options for typecheck pool. If passed without options, enables typechecking | | `--typecheck.enabled` | Enable typechecking alongside tests (default: `false`) | | `--typecheck.only` | Run only typecheck tests. This automatically enables typecheck (default: `false`) | diff --git a/packages/snapshot/src/client.ts b/packages/snapshot/src/client.ts index a8d19c36b619..1742a810fd67 100644 --- a/packages/snapshot/src/client.ts +++ b/packages/snapshot/src/client.ts @@ -3,7 +3,7 @@ import SnapshotState from './port/state' import type { SnapshotStateOptions } from './types' import type { RawSnapshotInfo } from './port/rawSnapshot' -function createMismatchError(message: string, actual: unknown, expected: unknown) { +function createMismatchError(message: string, expand: boolean | undefined, actual: unknown, expected: unknown) { const error = new Error(message) Object.defineProperty(error, 'actual', { value: actual, @@ -17,6 +17,7 @@ function createMismatchError(message: string, actual: unknown, expected: unknown configurable: true, writable: true, }) + Object.defineProperty(error, 'diffOptions', { value: { expand } }) return error } @@ -109,7 +110,7 @@ export class SnapshotClient { const pass = this.options.isEqual?.(received, properties) ?? false // const pass = equals(received, properties, [iterableEquality, subsetEquality]) if (!pass) - throw createMismatchError('Snapshot properties mismatched', received, properties) + throw createMismatchError('Snapshot properties mismatched', this.snapshotState?.expand, received, properties) else received = deepMergeSnapshot(received, properties) } @@ -136,7 +137,7 @@ export class SnapshotClient { }) if (!pass) - throw createMismatchError(`Snapshot \`${key || 'unknown'}\` mismatched`, actual?.trim(), expected?.trim()) + throw createMismatchError(`Snapshot \`${key || 'unknown'}\` mismatched`, this.snapshotState?.expand, actual?.trim(), expected?.trim()) } async assertRaw(options: AssertOptions): Promise { diff --git a/packages/utils/src/error.ts b/packages/utils/src/error.ts index 3b8a27f86a02..2b9057ad083d 100644 --- a/packages/utils/src/error.ts +++ b/packages/utils/src/error.ts @@ -106,7 +106,7 @@ export function processError(err: any, diffOptions?: DiffOptions) { const clonedExpected = deepClone(err.expected, { forceWritable: true }) const { replacedActual, replacedExpected } = replaceAsymmetricMatcher(clonedActual, clonedExpected) - err.diff = diff(replacedExpected, replacedActual, diffOptions) + err.diff = diff(replacedExpected, replacedActual, { ...diffOptions, ...err.diffOptions }) } if (typeof err.expected !== 'string') diff --git a/packages/vitest/src/node/cli.ts b/packages/vitest/src/node/cli.ts index 48f071ed5f59..a6dad4e46436 100644 --- a/packages/vitest/src/node/cli.ts +++ b/packages/vitest/src/node/cli.ts @@ -51,6 +51,7 @@ cli .option('--bail ', 'Stop test execution when given number of tests have failed', { default: 0 }) .option('--retry ', 'Retry the test specific number of times if it fails', { default: 0 }) .option('--diff ', 'Path to a diff config that will be used to generate diff interface') + .option('--expand-snapshot-diff', 'Show full diff when snapshot fails') .option('--typecheck [options]', 'Custom options for typecheck pool') .option('--typecheck.enabled', 'Enable typechecking alongside tests (default: false)') .option('--typecheck.only', 'Run only typecheck tests. This automatically enables typecheck (default: false)') diff --git a/packages/vitest/src/node/config.ts b/packages/vitest/src/node/config.ts index 511407bf970b..f8c604335c48 100644 --- a/packages/vitest/src/node/config.ts +++ b/packages/vitest/src/node/config.ts @@ -209,6 +209,7 @@ export function resolveConfig( const UPDATE_SNAPSHOT = resolved.update || process.env.UPDATE_SNAPSHOT resolved.snapshotOptions = { + expand: resolved.expandSnapshotDiff ?? false, snapshotFormat: resolved.snapshotFormat || {}, updateSnapshot: (isCI && !UPDATE_SNAPSHOT) ? 'none' diff --git a/packages/vitest/src/types/config.ts b/packages/vitest/src/types/config.ts index eba3a1ec2d2e..a90c1889fe97 100644 --- a/packages/vitest/src/types/config.ts +++ b/packages/vitest/src/types/config.ts @@ -633,6 +633,11 @@ export interface InlineConfig { * @default 0 */ retry?: number + + /** + * Show full diff when snapshot fails instead of a patch. + */ + expandSnapshotDiff?: boolean } export interface TypecheckConfig {