diff --git a/docs/config/index.md b/docs/config/index.md index 1fc760dc40bc..b629d4eecd7b 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -2297,6 +2297,7 @@ export default { aIndicator: c.bold('--'), bIndicator: c.bold('++'), omitAnnotationLines: true, + printBasicPrototype: false, } satisfies DiffOptions ``` @@ -2333,6 +2334,13 @@ Annotation that is output at the end of diff result if it's truncated. Color of truncate annotation, default is output with no color. +#### diff.printBasicPrototype + +- **Type**: `boolean` +- **Default**: `true` + +Allows to set pretty-format option printBasicPrototype for diff output + ### fakeTimers - **Type:** `FakeTimerInstallOpts` diff --git a/packages/utils/src/diff/index.ts b/packages/utils/src/diff/index.ts index fcb94a9f07b6..1dd05cb2f128 100644 --- a/packages/utils/src/diff/index.ts +++ b/packages/utils/src/diff/index.ts @@ -180,11 +180,12 @@ function getFormatOptions( formatOptions: PrettyFormatOptions, options?: DiffOptions, ): PrettyFormatOptions { - const { compareKeys } = normalizeDiffOptions(options) + const { compareKeys, printBasicPrototype } = normalizeDiffOptions(options) return { ...formatOptions, compareKeys, + printBasicPrototype, } } diff --git a/packages/utils/src/diff/normalizeDiffOptions.ts b/packages/utils/src/diff/normalizeDiffOptions.ts index a7d9be7134ae..f2940a38758c 100644 --- a/packages/utils/src/diff/normalizeDiffOptions.ts +++ b/packages/utils/src/diff/normalizeDiffOptions.ts @@ -34,6 +34,7 @@ function getDefaultOptions(): DiffOptionsNormalized { includeChangeCounts: false, omitAnnotationLines: false, patchColor: c.yellow, + printBasicPrototype: true, truncateThreshold: DIFF_TRUNCATE_THRESHOLD_DEFAULT, truncateAnnotation: '... Diff result is truncated', truncateAnnotationColor: noColor, diff --git a/packages/utils/src/diff/types.ts b/packages/utils/src/diff/types.ts index 951fcdbb27c1..1bb55fb9e92b 100644 --- a/packages/utils/src/diff/types.ts +++ b/packages/utils/src/diff/types.ts @@ -26,6 +26,7 @@ export interface DiffOptions { includeChangeCounts?: boolean omitAnnotationLines?: boolean patchColor?: DiffOptionsColor + printBasicPrototype?: boolean compareKeys?: CompareKeys truncateThreshold?: number truncateAnnotation?: string @@ -51,6 +52,7 @@ export interface DiffOptionsNormalized { includeChangeCounts: boolean omitAnnotationLines: boolean patchColor: DiffOptionsColor + printBasicPrototype: boolean truncateThreshold: number truncateAnnotation: string truncateAnnotationColor: DiffOptionsColor diff --git a/test/config/fixtures/diff/basic.test.ts b/test/config/fixtures/diff/basic.test.ts new file mode 100644 index 000000000000..ebc0f87bba6e --- /dev/null +++ b/test/config/fixtures/diff/basic.test.ts @@ -0,0 +1,11 @@ +import { expect, test } from 'vitest' + +test("printBasicPrototype false", () => { + expect({ + obj: { k: "foo" }, + arr: [1, 2] + }).toEqual({ + obj: { k: "bar" }, + arr: [1, 3] + }); +}) diff --git a/test/config/fixtures/diff/diff.ts b/test/config/fixtures/diff/diff.ts new file mode 100644 index 000000000000..d7fb2db32a8b --- /dev/null +++ b/test/config/fixtures/diff/diff.ts @@ -0,0 +1,5 @@ +import { DiffOptions } from "vitest"; + +export default { + printBasicPrototype: false +} satisfies DiffOptions diff --git a/test/config/fixtures/diff/vite.config.ts b/test/config/fixtures/diff/vite.config.ts new file mode 100644 index 000000000000..1776a93a0803 --- /dev/null +++ b/test/config/fixtures/diff/vite.config.ts @@ -0,0 +1,7 @@ +import {defineConfig} from 'vitest/config' + +export default defineConfig({ + test: { + diff: './diff.ts' + } +}) diff --git a/test/config/test/__snapshots__/diff.test.ts.snap b/test/config/test/__snapshots__/diff.test.ts.snap new file mode 100644 index 000000000000..5778c5d89b7e --- /dev/null +++ b/test/config/test/__snapshots__/diff.test.ts.snap @@ -0,0 +1,20 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`diff 1`] = ` +[ + "- Expected ++ Received + + { + "arr": [ + 1, +- 3, ++ 2, + ], + "obj": { +- "k": "bar", ++ "k": "foo", + }, + }", +] +`; diff --git a/test/config/test/diff.test.ts b/test/config/test/diff.test.ts new file mode 100644 index 000000000000..aa658ade6e6e --- /dev/null +++ b/test/config/test/diff.test.ts @@ -0,0 +1,15 @@ +import { stripVTControlCharacters } from 'node:util' +import { expect, test } from 'vitest' +import { runVitest } from '../../test-utils' + +test('diff', async () => { + const { ctx } = await runVitest({ + root: './fixtures/diff', + }) + const errors = ctx!.state.getFiles().flatMap(f => + f.tasks.flatMap(t => t.result?.errors ?? []), + ) + expect( + errors.map(e => e.diff && stripVTControlCharacters(e.diff)), + ).matchSnapshot() +}) diff --git a/test/config/test/failures.test.ts b/test/config/test/failures.test.ts index 591a2bd2175e..1d98a7a1a673 100644 --- a/test/config/test/failures.test.ts +++ b/test/config/test/failures.test.ts @@ -247,7 +247,9 @@ test('coverage.autoUpdate cannot update thresholds when configuration file doesn }) test('boolean flag 100 should not crash CLI', async () => { - const { stderr } = await runVitestCli('--coverage.enabled', '--coverage.thresholds.100') + let { stderr } = await runVitestCli('--coverage.enabled', '--coverage.thresholds.100') + // non-zero coverage shows up, which is non-deterministic, so strip it. + stderr = stderr.replace(/\([0-9.]+%\) does/g, '(0%) does') expect(stderr).toMatch('ERROR: Coverage for lines (0%) does not meet global threshold (100%)') expect(stderr).toMatch('ERROR: Coverage for functions (0%) does not meet global threshold (100%)')