From c068a3ad5aef87db1c752b3be74d3ab83b56e2a7 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 18 Oct 2024 19:21:57 +0900 Subject: [PATCH 01/15] feat(vitest): support inline `diff` options --- packages/vitest/src/node/cli/cli-config.ts | 19 +++++++++++++++++-- .../vitest/src/node/config/resolveConfig.ts | 2 +- .../vitest/src/node/config/serializeConfig.ts | 1 + packages/vitest/src/node/types/config.ts | 5 +++-- packages/vitest/src/runtime/config.ts | 3 ++- packages/vitest/src/runtime/setup-common.ts | 3 +++ 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/packages/vitest/src/node/cli/cli-config.ts b/packages/vitest/src/node/cli/cli-config.ts index 841d49b19000..27aa892ea474 100644 --- a/packages/vitest/src/node/cli/cli-config.ts +++ b/packages/vitest/src/node/cli/cli-config.ts @@ -598,9 +598,24 @@ export const cliOptionsConfig: VitestCLIOptions = { }, diff: { description: - 'Path to a diff config that will be used to generate diff interface', + 'DiffOptions object or a path to a module which exports DiffOptions object', argument: '', - normalize: true, + subcommands: { + aAnnotation: null, + aIndicator: null, + bAnnotation: null, + bIndicator: null, + commonIndicator: null, + contextLines: null, + emptyFirstOrLastLinePlaceholder: null, + expand: { + description: '(default: true)', + }, + includeChangeCounts: null, + omitAnnotationLines: null, + truncateThreshold: null, + truncateAnnotation: null, + }, }, exclude: { description: 'Additional file globs to be excluded from test', diff --git a/packages/vitest/src/node/config/resolveConfig.ts b/packages/vitest/src/node/config/resolveConfig.ts index 8d622a7df2a5..57f46d86c494 100644 --- a/packages/vitest/src/node/config/resolveConfig.ts +++ b/packages/vitest/src/node/config/resolveConfig.ts @@ -578,7 +578,7 @@ export function resolveConfig( } } - if (resolved.diff) { + if (typeof resolved.diff === 'string') { resolved.diff = resolvePath(resolved.diff, resolved.root) resolved.forceRerunTriggers.push(resolved.diff) } diff --git a/packages/vitest/src/node/config/serializeConfig.ts b/packages/vitest/src/node/config/serializeConfig.ts index 249f4bbdaa1a..234e67a3be34 100644 --- a/packages/vitest/src/node/config/serializeConfig.ts +++ b/packages/vitest/src/node/config/serializeConfig.ts @@ -37,6 +37,7 @@ export function serializeConfig( pool: config.pool, expect: config.expect, snapshotSerializers: config.snapshotSerializers, + // TODO: non serializable function? diff: config.diff, retry: config.retry, disableConsoleIntercept: config.disableConsoleIntercept, diff --git a/packages/vitest/src/node/types/config.ts b/packages/vitest/src/node/types/config.ts index 4b0939dbc2f0..ac43a6561777 100644 --- a/packages/vitest/src/node/types/config.ts +++ b/packages/vitest/src/node/types/config.ts @@ -4,6 +4,7 @@ import type { FakeTimerInstallOpts } from '@sinonjs/fake-timers' import type { SequenceHooks, SequenceSetupFiles } from '@vitest/runner' import type { ViteNodeServerOptions } from 'vite-node' import type { SnapshotStateOptions } from '@vitest/snapshot' +import type { DiffOptions } from '@vitest/utils/diff' import type { BuiltinReporterOptions, BuiltinReporters, @@ -563,7 +564,7 @@ export interface InlineConfig { /** * Path to a module which has a default export of diff config. */ - diff?: string + diff?: string | DiffOptions /** * Paths to snapshot serializer modules. @@ -979,7 +980,7 @@ export interface ResolvedConfig mode: VitestRunMode base?: string - diff?: string + diff?: string | DiffOptions bail?: number setupFiles: string[] diff --git a/packages/vitest/src/runtime/config.ts b/packages/vitest/src/runtime/config.ts index 14ebe708c67b..97b74bad8cd7 100644 --- a/packages/vitest/src/runtime/config.ts +++ b/packages/vitest/src/runtime/config.ts @@ -3,6 +3,7 @@ import type { PrettyFormatOptions } from '@vitest/pretty-format' import type { SequenceHooks, SequenceSetupFiles } from '@vitest/runner' import type { SnapshotUpdateState } from '@vitest/snapshot' import type { SnapshotEnvironment } from '@vitest/snapshot/environment' +import type { DiffOptions } from '@vitest/utils/diff' /** * Config that tests have access to. @@ -98,7 +99,7 @@ export interface SerializedConfig { showDiff?: boolean truncateThreshold?: number } | undefined - diff: string | undefined + diff: string | DiffOptions | undefined retry: number includeTaskLocation: boolean | undefined inspect: boolean | string | undefined diff --git a/packages/vitest/src/runtime/setup-common.ts b/packages/vitest/src/runtime/setup-common.ts index 9dc3d25e5f97..251e6fb30389 100644 --- a/packages/vitest/src/runtime/setup-common.ts +++ b/packages/vitest/src/runtime/setup-common.ts @@ -47,6 +47,9 @@ export async function loadDiffConfig( config: SerializedConfig, executor: VitestExecutor, ) { + if (typeof config.diff === 'object') { + return config.diff + } if (typeof config.diff !== 'string') { return } From a135563c7a195c0bc998e2c31ff6fbcd509227c4 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sat, 19 Oct 2024 09:44:51 +0900 Subject: [PATCH 02/15] test: add test --- test/config/fixtures/diff/basic.test.ts | 12 +++ test/config/fixtures/diff/vite.config.ts | 9 ++ .../test/__snapshots__/diff.test.ts.snap | 98 +++++++++++++++++++ test/config/test/diff.test.ts | 18 ++++ 4 files changed, 137 insertions(+) create mode 100644 test/config/fixtures/diff/basic.test.ts create mode 100644 test/config/fixtures/diff/vite.config.ts create mode 100644 test/config/test/__snapshots__/diff.test.ts.snap create mode 100644 test/config/test/diff.test.ts diff --git a/test/config/fixtures/diff/basic.test.ts b/test/config/fixtures/diff/basic.test.ts new file mode 100644 index 000000000000..65ee64ffb77d --- /dev/null +++ b/test/config/fixtures/diff/basic.test.ts @@ -0,0 +1,12 @@ +import { expect, test } from 'vitest' + +test('large diff', () => { + const data = ` +a +b +c +d +e +` + expect(data.repeat(6)).toEqual(`here${data.repeat(3)}and${data.repeat(3)}there`) +}) diff --git a/test/config/fixtures/diff/vite.config.ts b/test/config/fixtures/diff/vite.config.ts new file mode 100644 index 000000000000..4cd29b651e95 --- /dev/null +++ b/test/config/fixtures/diff/vite.config.ts @@ -0,0 +1,9 @@ +import {defineConfig} from 'vitest/config' + +export default defineConfig({ + test: { + diff: { + expand: false + } + } +}) 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..8386ab7eebfb --- /dev/null +++ b/test/config/test/__snapshots__/diff.test.ts.snap @@ -0,0 +1,98 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`options default 1`] = ` +[ + " +AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\na…' to deeply equal 'here\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\…' + +- Expected ++ Received + +- here ++ + a + b + c + d + e + + a + b + c + d + e + + a + b + c + d + e +- and ++ + a + b + c + d + e + + a + b + c + d + e + + a + b + c + d + e +- there ++ + + ❯ basic.test.ts:11:26 + ", +] +`; + +exports[`options expand-false 1`] = ` +[ + " +AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\na…' to deeply equal 'here\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\…' + +- Expected ++ Received + +@@ -1,6 +1,6 @@ +- here ++ + a + b + c + d + e +@@ -14,11 +14,11 @@ + a + b + c + d + e +- and ++ + a + b + c + d + e +@@ -32,6 +32,6 @@ + a + b + c + d + e +- there ++ + + ❯ basic.test.ts:11:26 + ", +] +`; diff --git a/test/config/test/diff.test.ts b/test/config/test/diff.test.ts new file mode 100644 index 000000000000..217fe71325fd --- /dev/null +++ b/test/config/test/diff.test.ts @@ -0,0 +1,18 @@ +import { describe } from 'node:test' +import { expect, test } from 'vitest' +import { runVitest } from '../../test-utils' + +describe('inline diff options', () => { + test.for([ + ['default', undefined], + ['expand-false', { expand: false }], + ])('options %s', async ([_, options]) => { + const { stdout } = await runVitest({ + root: './fixtures/diff', + diff: options, + reporters: ['junit'], + }) + const matches = stdout.matchAll(/]*>(.*)<\/failure>/gs) + expect([...matches].map(m => m[1])).matchSnapshot() + }) +}) From 1157e8320bd4b22ad664f280c9b95b4545e9e8c8 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sat, 19 Oct 2024 09:49:15 +0900 Subject: [PATCH 03/15] test: tweak --- test/config/fixtures/diff/vite.config.ts | 8 +--- .../test/__snapshots__/diff.test.ts.snap | 38 +++++++++---------- test/config/test/diff.test.ts | 6 +-- 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/test/config/fixtures/diff/vite.config.ts b/test/config/fixtures/diff/vite.config.ts index 4cd29b651e95..83e67460dd4d 100644 --- a/test/config/fixtures/diff/vite.config.ts +++ b/test/config/fixtures/diff/vite.config.ts @@ -1,9 +1,3 @@ import {defineConfig} from 'vitest/config' -export default defineConfig({ - test: { - diff: { - expand: false - } - } -}) +export default defineConfig({}) diff --git a/test/config/test/__snapshots__/diff.test.ts.snap b/test/config/test/__snapshots__/diff.test.ts.snap index 8386ab7eebfb..abd29cdc639b 100644 --- a/test/config/test/__snapshots__/diff.test.ts.snap +++ b/test/config/test/__snapshots__/diff.test.ts.snap @@ -1,6 +1,6 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`options default 1`] = ` +exports[`options { expand: false } 1`] = ` [ " AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\na…' to deeply equal 'here\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\…' @@ -8,6 +8,7 @@ AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\n - Expected + Received +@@ -1,6 +1,6 @@ - here + a @@ -15,13 +16,7 @@ AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\n c d e - - a - b - c - d - e - +@@ -14,11 +14,11 @@ a b c @@ -34,13 +29,7 @@ AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\n c d e - - a - b - c - d - e - +@@ -32,6 +32,6 @@ a b c @@ -54,7 +43,7 @@ AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\n ] `; -exports[`options expand-false 1`] = ` +exports[`options undefined 1`] = ` [ " AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\na…' to deeply equal 'here\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\…' @@ -62,7 +51,6 @@ AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\n - Expected + Received -@@ -1,6 +1,6 @@ - here + a @@ -70,7 +58,13 @@ AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\n c d e -@@ -14,11 +14,11 @@ + + a + b + c + d + e + a b c @@ -83,7 +77,13 @@ AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\n c d e -@@ -32,6 +32,6 @@ + + a + b + c + d + e + a b c diff --git a/test/config/test/diff.test.ts b/test/config/test/diff.test.ts index 217fe71325fd..3c5f7680ee9f 100644 --- a/test/config/test/diff.test.ts +++ b/test/config/test/diff.test.ts @@ -4,9 +4,9 @@ import { runVitest } from '../../test-utils' describe('inline diff options', () => { test.for([ - ['default', undefined], - ['expand-false', { expand: false }], - ])('options %s', async ([_, options]) => { + [undefined], + [{ expand: false }], + ])('options %o', async ([options]) => { const { stdout } = await runVitest({ root: './fixtures/diff', diff: options, From bfecf85592b45576af320721e34aa331fd8af573 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sat, 19 Oct 2024 10:23:26 +0900 Subject: [PATCH 04/15] test: tweak --- .../test/__snapshots__/diff.test.ts.snap | 4 ++-- test/config/test/diff.test.ts | 23 ++++++++----------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/test/config/test/__snapshots__/diff.test.ts.snap b/test/config/test/__snapshots__/diff.test.ts.snap index abd29cdc639b..c1ec7590f19f 100644 --- a/test/config/test/__snapshots__/diff.test.ts.snap +++ b/test/config/test/__snapshots__/diff.test.ts.snap @@ -1,6 +1,6 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`options { expand: false } 1`] = ` +exports[`inline diff options { expand: false } 1`] = ` [ " AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\na…' to deeply equal 'here\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\…' @@ -43,7 +43,7 @@ AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\n ] `; -exports[`options undefined 1`] = ` +exports[`inline diff options undefined 1`] = ` [ " AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\na…' to deeply equal 'here\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\…' diff --git a/test/config/test/diff.test.ts b/test/config/test/diff.test.ts index 3c5f7680ee9f..7d506ada8615 100644 --- a/test/config/test/diff.test.ts +++ b/test/config/test/diff.test.ts @@ -1,18 +1,15 @@ -import { describe } from 'node:test' import { expect, test } from 'vitest' import { runVitest } from '../../test-utils' -describe('inline diff options', () => { - test.for([ - [undefined], - [{ expand: false }], - ])('options %o', async ([options]) => { - const { stdout } = await runVitest({ - root: './fixtures/diff', - diff: options, - reporters: ['junit'], - }) - const matches = stdout.matchAll(/]*>(.*)<\/failure>/gs) - expect([...matches].map(m => m[1])).matchSnapshot() +test.for([ + [undefined], + [{ expand: false }], +])('inline diff options %o', async ([options]) => { + const { stdout } = await runVitest({ + root: './fixtures/diff', + diff: options, + reporters: ['junit'], }) + const matches = stdout.matchAll(/]*>(.*)<\/failure>/gs) + expect([...matches].map(m => m[1])).matchSnapshot() }) From ad46d06552df25882172f153405adc07dabd4e93 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sat, 19 Oct 2024 11:41:52 +0900 Subject: [PATCH 05/15] fix: fix browser mode dep --- packages/browser/src/node/plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/browser/src/node/plugin.ts b/packages/browser/src/node/plugin.ts index b5121f3e0536..fe843111b645 100644 --- a/packages/browser/src/node/plugin.ts +++ b/packages/browser/src/node/plugin.ts @@ -209,7 +209,7 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => { 'msw/browser', ] - if (project.config.diff) { + if (typeof project.config.diff === 'string') { entries.push(project.config.diff) } From 5663cc70d47394e4e28c95438451df35f8378b93 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sat, 19 Oct 2024 13:06:50 +0900 Subject: [PATCH 06/15] fix: add SerializedDiffOptions --- packages/utils/src/diff/index.ts | 2 +- packages/utils/src/diff/types.ts | 15 +++++++++++++++ packages/vitest/src/node/types/config.ts | 6 +++--- packages/vitest/src/runtime/config.ts | 4 ++-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/packages/utils/src/diff/index.ts b/packages/utils/src/diff/index.ts index d6dc8a084a83..284721d9a98f 100644 --- a/packages/utils/src/diff/index.ts +++ b/packages/utils/src/diff/index.ts @@ -23,7 +23,7 @@ import { normalizeDiffOptions } from './normalizeDiffOptions' import { diffStringsRaw, diffStringsUnified } from './printDiffs' import type { DiffOptions } from './types' -export type { DiffOptions, DiffOptionsColor } from './types' +export type { DiffOptions, SerializedDiffOptions, DiffOptionsColor } from './types' export { diffLinesRaw, diffLinesUnified, diffLinesUnified2 } export { diffStringsRaw, diffStringsUnified } diff --git a/packages/utils/src/diff/types.ts b/packages/utils/src/diff/types.ts index 951fcdbb27c1..14bc76ce9bf0 100644 --- a/packages/utils/src/diff/types.ts +++ b/packages/utils/src/diff/types.ts @@ -32,6 +32,21 @@ export interface DiffOptions { truncateAnnotationColor?: DiffOptionsColor } +export interface SerializedDiffOptions { + aAnnotation?: string + aIndicator?: string + bAnnotation?: string + bIndicator?: string + commonIndicator?: string + contextLines?: number + emptyFirstOrLastLinePlaceholder?: string + expand?: boolean + includeChangeCounts?: boolean + omitAnnotationLines?: boolean + truncateThreshold?: number + truncateAnnotation?: string +} + export interface DiffOptionsNormalized { aAnnotation: string aColor: DiffOptionsColor diff --git a/packages/vitest/src/node/types/config.ts b/packages/vitest/src/node/types/config.ts index ac43a6561777..d858e44b3d22 100644 --- a/packages/vitest/src/node/types/config.ts +++ b/packages/vitest/src/node/types/config.ts @@ -4,7 +4,7 @@ import type { FakeTimerInstallOpts } from '@sinonjs/fake-timers' import type { SequenceHooks, SequenceSetupFiles } from '@vitest/runner' import type { ViteNodeServerOptions } from 'vite-node' import type { SnapshotStateOptions } from '@vitest/snapshot' -import type { DiffOptions } from '@vitest/utils/diff' +import type { SerializedDiffOptions } from '@vitest/utils/diff' import type { BuiltinReporterOptions, BuiltinReporters, @@ -564,7 +564,7 @@ export interface InlineConfig { /** * Path to a module which has a default export of diff config. */ - diff?: string | DiffOptions + diff?: string | SerializedDiffOptions /** * Paths to snapshot serializer modules. @@ -980,7 +980,7 @@ export interface ResolvedConfig mode: VitestRunMode base?: string - diff?: string | DiffOptions + diff?: string | SerializedDiffOptions bail?: number setupFiles: string[] diff --git a/packages/vitest/src/runtime/config.ts b/packages/vitest/src/runtime/config.ts index 97b74bad8cd7..55eb5fd6095a 100644 --- a/packages/vitest/src/runtime/config.ts +++ b/packages/vitest/src/runtime/config.ts @@ -3,7 +3,7 @@ import type { PrettyFormatOptions } from '@vitest/pretty-format' import type { SequenceHooks, SequenceSetupFiles } from '@vitest/runner' import type { SnapshotUpdateState } from '@vitest/snapshot' import type { SnapshotEnvironment } from '@vitest/snapshot/environment' -import type { DiffOptions } from '@vitest/utils/diff' +import type { SerializedDiffOptions } from '@vitest/utils/diff' /** * Config that tests have access to. @@ -99,7 +99,7 @@ export interface SerializedConfig { showDiff?: boolean truncateThreshold?: number } | undefined - diff: string | DiffOptions | undefined + diff: string | SerializedDiffOptions | undefined retry: number includeTaskLocation: boolean | undefined inspect: boolean | string | undefined From de0cb0fac66eecc9e19cd04ef87d4cb075e882dc Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sat, 19 Oct 2024 13:57:11 +0900 Subject: [PATCH 07/15] fix: create cli config by copilot --- packages/vitest/src/node/cli/cli-config.ts | 55 +++++++++++++++++----- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/packages/vitest/src/node/cli/cli-config.ts b/packages/vitest/src/node/cli/cli-config.ts index 27aa892ea474..131540a31b38 100644 --- a/packages/vitest/src/node/cli/cli-config.ts +++ b/packages/vitest/src/node/cli/cli-config.ts @@ -601,20 +601,51 @@ export const cliOptionsConfig: VitestCLIOptions = { 'DiffOptions object or a path to a module which exports DiffOptions object', argument: '', subcommands: { - aAnnotation: null, - aIndicator: null, - bAnnotation: null, - bIndicator: null, - commonIndicator: null, - contextLines: null, - emptyFirstOrLastLinePlaceholder: null, + aAnnotation: { + description: 'Annotation for expected lines (default: `Expected`)', + argument: '', + }, + aIndicator: { + description: 'Indicator for expected lines (default: `>`)', + argument: '', + }, + bAnnotation: { + description: 'Annotation for received lines (default: `Received`)', + argument: '', + }, + bIndicator: { + description: 'Indicator for received lines (default: `<`)', + argument: '', + }, + commonIndicator: { + description: 'Indicator for common lines (default: ` `)', + argument: '', + }, + contextLines: { + description: 'Number of lines of context to show around each change (default: `5`)', + argument: '', + }, + emptyFirstOrLastLinePlaceholder: { + description: 'Placeholder for an empty first or last line (default: `""`)', + argument: '', + }, expand: { - description: '(default: true)', + description: 'Expand all common lines (default: true)', + }, + includeChangeCounts: { + description: 'Include comparison counts in diff output (default: `true`)', + }, + omitAnnotationLines: { + description: 'Omit annotation lines from the output (default: `false`)', + }, + truncateThreshold: { + description: 'Number of lines to show before and after each change (default: `100`)', + argument: '', + }, + truncateAnnotation: { + description: 'Annotation for truncated lines (default: `...`)', + argument: '', }, - includeChangeCounts: null, - omitAnnotationLines: null, - truncateThreshold: null, - truncateAnnotation: null, }, }, exclude: { From b519f163a086e1382b3792cbe51bebc1cec86373 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sat, 19 Oct 2024 14:03:29 +0900 Subject: [PATCH 08/15] fix: fix copilot --- packages/vitest/src/node/cli/cli-config.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/vitest/src/node/cli/cli-config.ts b/packages/vitest/src/node/cli/cli-config.ts index 131540a31b38..81c291deffb4 100644 --- a/packages/vitest/src/node/cli/cli-config.ts +++ b/packages/vitest/src/node/cli/cli-config.ts @@ -606,7 +606,7 @@ export const cliOptionsConfig: VitestCLIOptions = { argument: '', }, aIndicator: { - description: 'Indicator for expected lines (default: `>`)', + description: 'Indicator for expected lines (default: `-`)', argument: '', }, bAnnotation: { @@ -614,7 +614,7 @@ export const cliOptionsConfig: VitestCLIOptions = { argument: '', }, bIndicator: { - description: 'Indicator for received lines (default: `<`)', + description: 'Indicator for received lines (default: `+`)', argument: '', }, commonIndicator: { @@ -630,20 +630,20 @@ export const cliOptionsConfig: VitestCLIOptions = { argument: '', }, expand: { - description: 'Expand all common lines (default: true)', + description: 'Expand all common lines (default: `true`)', }, includeChangeCounts: { - description: 'Include comparison counts in diff output (default: `true`)', + description: 'Include comparison counts in diff output (default: `false`)', }, omitAnnotationLines: { description: 'Omit annotation lines from the output (default: `false`)', }, truncateThreshold: { - description: 'Number of lines to show before and after each change (default: `100`)', + description: 'Number of lines to show before and after each change (default: `0`)', argument: '', }, truncateAnnotation: { - description: 'Annotation for truncated lines (default: `...`)', + description: 'Annotation for truncated lines (default: `... Diff result is truncated`)', argument: '', }, }, From ac549e8d175c4fc147dea351ff1e196bfe931c98 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sat, 19 Oct 2024 14:14:31 +0900 Subject: [PATCH 09/15] docs: update --- docs/config/index.md | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index f9321433d458..159a3b25b43a 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -2276,9 +2276,26 @@ export default defineConfig({ ### diff - **Type:** `string` -- **CLI:** `--diff=` +- **CLI:** `--diff=` -Path to a diff config that will be used to generate diff interface. Useful if you want to customize diff display. +`DiffOptions` object or a path to a module which exports `DiffOptions`. Useful if you want to customize diff display. + +:::code-group +```ts [vitest.config.js] +import { defineConfig } from 'vitest/config' +import c from 'tinyrainbow' + +export default defineConfig({ + test: { + diff: { + aIndicator: c.bold('--'), + bIndicator: c.bold('++'), + omitAnnotationLines: true, + } + } +}) +``` +::: :::code-group ```ts [vitest.diff.ts] @@ -2303,10 +2320,19 @@ export default defineConfig({ ``` ::: +#### diff.expand + +- **Type**: `boolean` +- **Default**: `true` +- **CLI:** `--diff.expand=false` + +Expand all common lines. + #### diff.truncateThreshold - **Type**: `number` - **Default**: `0` +- **CLI:** `--diff.truncateThreshold=` The maximum length of diff result to be displayed. Diffs above this threshold will be truncated. Truncation won't take effect with default value 0. @@ -2315,6 +2341,7 @@ Truncation won't take effect with default value 0. - **Type**: `string` - **Default**: `'... Diff result is truncated'` +- **CLI:** `--diff.truncateAnnotation=` Annotation that is output at the end of diff result if it's truncated. From 745c9e5b431913b8266f0448b47b4428721b4771 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sun, 20 Oct 2024 14:31:32 +0900 Subject: [PATCH 10/15] test: tweak --- .../test/__snapshots__/diff.test.ts.snap | 24 +++++-------------- test/config/test/diff.test.ts | 16 +++++++++---- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/test/config/test/__snapshots__/diff.test.ts.snap b/test/config/test/__snapshots__/diff.test.ts.snap index c1ec7590f19f..a9476399891d 100644 --- a/test/config/test/__snapshots__/diff.test.ts.snap +++ b/test/config/test/__snapshots__/diff.test.ts.snap @@ -1,11 +1,8 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`inline diff options { expand: false } 1`] = ` +exports[`inline diff options: { expand: false } 1`] = ` [ - " -AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\na…' to deeply equal 'here\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\…' - -- Expected + "- Expected + Received @@ -1,6 +1,6 @@ @@ -36,19 +33,13 @@ AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\n d e - there -+ - - ❯ basic.test.ts:11:26 - ", ++", ] `; -exports[`inline diff options undefined 1`] = ` +exports[`inline diff options: undefined 1`] = ` [ - " -AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\na…' to deeply equal 'here\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\…' - -- Expected + "- Expected + Received - here @@ -90,9 +81,6 @@ AssertionError: expected '\\na\\nb\\nc\\nd\\ne\\n\\na\\nb\\nc\\nd\\ne\\n\\n d e - there -+ - - ❯ basic.test.ts:11:26 - ", ++", ] `; diff --git a/test/config/test/diff.test.ts b/test/config/test/diff.test.ts index 7d506ada8615..b9f8c970dc31 100644 --- a/test/config/test/diff.test.ts +++ b/test/config/test/diff.test.ts @@ -1,15 +1,21 @@ +import { stripVTControlCharacters } from 'node:util' import { expect, test } from 'vitest' import { runVitest } from '../../test-utils' test.for([ [undefined], [{ expand: false }], -])('inline diff options %o', async ([options]) => { - const { stdout } = await runVitest({ +])(`inline diff options: %o`, async ([options]) => { + const { ctx } = await runVitest({ root: './fixtures/diff', diff: options, - reporters: ['junit'], }) - const matches = stdout.matchAll(/]*>(.*)<\/failure>/gs) - expect([...matches].map(m => m[1])).matchSnapshot() + const errors = ctx!.state + .getFiles() + .flatMap(f => + f.tasks.flatMap(t => t.result?.errors ?? []), + ) + expect( + errors.map(e => e.diff && stripVTControlCharacters(e.diff)), + ).matchSnapshot() }) From e218e88fa11e5c849c33cfab6433b22d5fc4e0a1 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sun, 20 Oct 2024 14:42:08 +0900 Subject: [PATCH 11/15] test: tweak --- test/config/fixtures/diff/basic.test.ts | 14 +- .../test/__snapshots__/diff.test.ts.snap | 135 +++++++++--------- 2 files changed, 72 insertions(+), 77 deletions(-) diff --git a/test/config/fixtures/diff/basic.test.ts b/test/config/fixtures/diff/basic.test.ts index 65ee64ffb77d..3064aae6c10e 100644 --- a/test/config/fixtures/diff/basic.test.ts +++ b/test/config/fixtures/diff/basic.test.ts @@ -1,12 +1,10 @@ import { expect, test } from 'vitest' test('large diff', () => { - const data = ` -a -b -c -d -e -` - expect(data.repeat(6)).toEqual(`here${data.repeat(3)}and${data.repeat(3)}there`) + const x = [...Array(30)].map((_, i) => i); + const y = [...x]; + y[0] = 1000; + y[15] = 2000; + y[29] = 3000; + expect(x).toEqual(y) }) diff --git a/test/config/test/__snapshots__/diff.test.ts.snap b/test/config/test/__snapshots__/diff.test.ts.snap index a9476399891d..7237bfec2e04 100644 --- a/test/config/test/__snapshots__/diff.test.ts.snap +++ b/test/config/test/__snapshots__/diff.test.ts.snap @@ -5,35 +5,37 @@ exports[`inline diff options: { expand: false } 1`] = ` "- Expected + Received -@@ -1,6 +1,6 @@ -- here -+ - a - b - c - d - e -@@ -14,11 +14,11 @@ - a - b - c - d - e -- and -+ - a - b - c - d - e -@@ -32,6 +32,6 @@ - a - b - c - d - e -- there -+", +@@ -1,7 +1,7 @@ + Array [ +- 1000, ++ 0, + 1, + 2, + 3, + 4, + 5, +@@ -12,11 +12,11 @@ + 10, + 11, + 12, + 13, + 14, +- 2000, ++ 15, + 16, + 17, + 18, + 19, + 20, +@@ -26,7 +26,7 @@ + 24, + 25, + 26, + 27, + 28, +- 3000, ++ 29, + ]", ] `; @@ -42,45 +44,40 @@ exports[`inline diff options: undefined 1`] = ` "- Expected + Received -- here -+ - a - b - c - d - e - - a - b - c - d - e - - a - b - c - d - e -- and -+ - a - b - c - d - e - - a - b - c - d - e - - a - b - c - d - e -- there -+", + Array [ +- 1000, ++ 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, +- 2000, ++ 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 24, + 25, + 26, + 27, + 28, +- 3000, ++ 29, + ]", ] `; From 47efdb2fcc2bec66e5441b7e50b0ed874c656b42 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Sun, 20 Oct 2024 14:20:17 +0200 Subject: [PATCH 12/15] Update docs/config/index.md --- docs/config/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/config/index.md b/docs/config/index.md index 159a3b25b43a..3544cf1bc761 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -2283,7 +2283,7 @@ export default defineConfig({ :::code-group ```ts [vitest.config.js] import { defineConfig } from 'vitest/config' -import c from 'tinyrainbow' +import c from 'picocolors' export default defineConfig({ test: { From 2f7b214c25ec84d18804ad234f5229c8675e98fe Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 21 Oct 2024 08:26:58 +0900 Subject: [PATCH 13/15] docs: tweak --- docs/config/index.md | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index 3544cf1bc761..9e0ca9377299 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -2280,6 +2280,8 @@ export default defineConfig({ `DiffOptions` object or a path to a module which exports `DiffOptions`. Useful if you want to customize diff display. +For example, as a config object: + :::code-group ```ts [vitest.config.js] import { defineConfig } from 'vitest/config' @@ -2297,18 +2299,9 @@ export default defineConfig({ ``` ::: -:::code-group -```ts [vitest.diff.ts] -import type { DiffOptions } from 'vitest' -import c from 'tinyrainbow' - -export default { - aIndicator: c.bold('--'), - bIndicator: c.bold('++'), - omitAnnotationLines: true, -} satisfies DiffOptions -``` +Or as a module: +:::code-group ```ts [vitest.config.js] import { defineConfig } from 'vitest/config' @@ -2318,6 +2311,17 @@ export default defineConfig({ } }) ``` + +```ts [vitest.diff.ts] +import type { DiffOptions } from 'vitest' +import c from 'picocolors' + +export default { + aIndicator: c.bold('--'), + bIndicator: c.bold('++'), + omitAnnotationLines: true, +} satisfies DiffOptions +``` ::: #### diff.expand From 7aa803dd68e8b50370b54b7deb376fd735233063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Grzegorzewski?= <4864089+spamshaker@users.noreply.github.com> Date: Sat, 14 Sep 2024 09:46:39 +0200 Subject: [PATCH 14/15] feat: support `printBasicPrototype` (cherry-pick https://github.com/vitest-dev/vitest/pull/6504/) Update index.ts Adding possibility to get rid of printing prototype in diff output Update types.ts Update config documentation to include diff.printBasicPrototype Added a new configuration option `printBasicPrototype` to the diff section. This allows users to set the pretty-format option for the diff output, with a default value of `true`. Update index.ts CR Fixes test: add test chore: type error chore: lint test: fix threshold test chore: lint --- docs/config/index.md | 7 ++++ packages/utils/src/diff/index.ts | 3 +- .../utils/src/diff/normalizeDiffOptions.ts | 1 + packages/utils/src/diff/types.ts | 3 ++ test/config/fixtures/diff/basic.test.ts | 10 ++++++ test/config/fixtures/diff/vite.config.ts | 9 +++++- .../test/__snapshots__/diff.test.ts.snap | 32 +++++++++++++++++-- test/config/test/diff.test.ts | 2 +- test/config/test/failures.test.ts | 4 ++- 9 files changed, 65 insertions(+), 6 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index 0afbfdab7b9c..923db8be1a5d 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -2364,6 +2364,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 b9e39b7e27ea..d9445dc47dc0 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 14bc76ce9bf0..211ab033f12c 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 @@ -43,6 +44,7 @@ export interface SerializedDiffOptions { expand?: boolean includeChangeCounts?: boolean omitAnnotationLines?: boolean + printBasicPrototype?: boolean truncateThreshold?: number truncateAnnotation?: string } @@ -66,6 +68,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 index 3064aae6c10e..af61b0814634 100644 --- a/test/config/fixtures/diff/basic.test.ts +++ b/test/config/fixtures/diff/basic.test.ts @@ -8,3 +8,13 @@ test('large diff', () => { y[29] = 3000; expect(x).toEqual(y) }) + +test("printBasicPrototype", () => { + expect({ + obj: { k: "foo" }, + arr: [1, 2] + }).toEqual({ + obj: { k: "bar" }, + arr: [1, 3] + }); +}) diff --git a/test/config/fixtures/diff/vite.config.ts b/test/config/fixtures/diff/vite.config.ts index 83e67460dd4d..2d94b473bfba 100644 --- a/test/config/fixtures/diff/vite.config.ts +++ b/test/config/fixtures/diff/vite.config.ts @@ -1,3 +1,10 @@ import {defineConfig} from 'vitest/config' -export default defineConfig({}) +export default defineConfig({ + test: { + diff: { + // expand: false, + // printBasicPrototype: false, + } + } +}) diff --git a/test/config/test/__snapshots__/diff.test.ts.snap b/test/config/test/__snapshots__/diff.test.ts.snap index 7237bfec2e04..5ab25703726a 100644 --- a/test/config/test/__snapshots__/diff.test.ts.snap +++ b/test/config/test/__snapshots__/diff.test.ts.snap @@ -1,12 +1,12 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`inline diff options: { expand: false } 1`] = ` +exports[`inline diff options: { expand: false, printBasicPrototype: false } 1`] = ` [ "- Expected + Received @@ -1,7 +1,7 @@ - Array [ + [ - 1000, + 0, 1, @@ -36,6 +36,20 @@ exports[`inline diff options: { expand: false } 1`] = ` - 3000, + 29, ]", + "- Expected ++ Received + + { + "arr": [ + 1, +- 3, ++ 2, + ], + "obj": { +- "k": "bar", ++ "k": "foo", + }, + }", ] `; @@ -79,5 +93,19 @@ exports[`inline diff options: undefined 1`] = ` - 3000, + 29, ]", + "- Expected ++ Received + + Object { + "arr": Array [ + 1, +- 3, ++ 2, + ], + "obj": Object { +- "k": "bar", ++ "k": "foo", + }, + }", ] `; diff --git a/test/config/test/diff.test.ts b/test/config/test/diff.test.ts index 0612ed088e8e..059e1d0cacc5 100644 --- a/test/config/test/diff.test.ts +++ b/test/config/test/diff.test.ts @@ -4,7 +4,7 @@ import { runVitest } from '../../test-utils' test.for([ [undefined], - [{ expand: false }], + [{ expand: false, printBasicPrototype: false }], ])(`inline diff options: %o`, async ([options]) => { const { ctx } = await runVitest({ root: './fixtures/diff', 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%)') From 172b9857a742ff5e294341504ebaad3068a16a20 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Wed, 13 Nov 2024 10:17:57 +0900 Subject: [PATCH 15/15] fix: cli option --- docs/config/index.md | 2 +- packages/vitest/src/node/cli/cli-config.ts | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/config/index.md b/docs/config/index.md index 923db8be1a5d..a0f9152d09d2 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -2369,7 +2369,7 @@ Color of truncate annotation, default is output with no color. - **Type**: `boolean` - **Default**: `true` -Allows to set pretty-format option printBasicPrototype for diff output +Print basic prototype `Object` and `Array` in diff output ### fakeTimers diff --git a/packages/vitest/src/node/cli/cli-config.ts b/packages/vitest/src/node/cli/cli-config.ts index c6de810a89da..3d4f9e31961f 100644 --- a/packages/vitest/src/node/cli/cli-config.ts +++ b/packages/vitest/src/node/cli/cli-config.ts @@ -639,6 +639,9 @@ export const cliOptionsConfig: VitestCLIOptions = { omitAnnotationLines: { description: 'Omit annotation lines from the output (default: `false`)', }, + printBasicPrototype: { + description: 'Print basic prototype Object and Array (default: `true`)', + }, truncateThreshold: { description: 'Number of lines to show before and after each change (default: `0`)', argument: '',