Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't expand snapshot diff by default #4430

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/guide/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Run only [benchmark](https://vitest.dev/guide/features.html#benchmarking-experim
| `--inspect-brk` | Enables Node.js inspector with break |
| `--bail <number>` | Stop test execution when given number of tests have failed |
| `--retry <times>` | 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`) |
Expand Down
7 changes: 4 additions & 3 deletions packages/snapshot/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -17,6 +17,7 @@ function createMismatchError(message: string, actual: unknown, expected: unknown
configurable: true,
writable: true,
})
Object.defineProperty(error, 'diffOptions', { value: { expand } })
return error
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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<void> {
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ cli
.option('--bail <number>', 'Stop test execution when given number of tests have failed', { default: 0 })
.option('--retry <times>', 'Retry the test specific number of times if it fails', { default: 0 })
.option('--diff <path>', '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)')
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
5 changes: 5 additions & 0 deletions packages/vitest/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading