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

feat: support inline diff options and support printBasicPrototype #6740

Merged
merged 17 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
31 changes: 29 additions & 2 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2276,9 +2276,26 @@ export default defineConfig({
### diff

- **Type:** `string`
- **CLI:** `--diff=<value>`
- **CLI:** `--diff=<path>`

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'
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved

export default defineConfig({
test: {
diff: {
aIndicator: c.bold('--'),
bIndicator: c.bold('++'),
omitAnnotationLines: true,
}
}
})
```
:::

:::code-group
```ts [vitest.diff.ts]
Expand All @@ -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=<path>`

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.
Expand All @@ -2315,6 +2341,7 @@ Truncation won't take effect with default value 0.

- **Type**: `string`
- **Default**: `'... Diff result is truncated'`
- **CLI:** `--diff.truncateAnnotation=<annotation>`

Annotation that is output at the end of diff result if it's truncated.

Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/diff/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
15 changes: 15 additions & 0 deletions packages/utils/src/diff/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 48 additions & 2 deletions packages/vitest/src/node/cli/cli-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,55 @@ 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: '<path>',
normalize: true,
subcommands: {
aAnnotation: {
description: 'Annotation for expected lines (default: `Expected`)',
argument: '<annotation>',
},
aIndicator: {
description: 'Indicator for expected lines (default: `-`)',
argument: '<indicator>',
},
bAnnotation: {
description: 'Annotation for received lines (default: `Received`)',
argument: '<annotation>',
},
bIndicator: {
description: 'Indicator for received lines (default: `+`)',
argument: '<indicator>',
},
commonIndicator: {
description: 'Indicator for common lines (default: ` `)',
argument: '<indicator>',
},
contextLines: {
description: 'Number of lines of context to show around each change (default: `5`)',
argument: '<lines>',
},
emptyFirstOrLastLinePlaceholder: {
description: 'Placeholder for an empty first or last line (default: `""`)',
argument: '<placeholder>',
},
expand: {
description: 'Expand all common lines (default: `true`)',
},
includeChangeCounts: {
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: `0`)',
argument: '<threshold>',
},
truncateAnnotation: {
description: 'Annotation for truncated lines (default: `... Diff result is truncated`)',
argument: '<annotation>',
},
},
},
exclude: {
description: 'Additional file globs to be excluded from test',
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/config/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/node/config/serializeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions packages/vitest/src/node/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { SerializedDiffOptions } from '@vitest/utils/diff'
import type {
BuiltinReporterOptions,
BuiltinReporters,
Expand Down Expand Up @@ -563,7 +564,7 @@ export interface InlineConfig {
/**
* Path to a module which has a default export of diff config.
*/
diff?: string
diff?: string | SerializedDiffOptions

/**
* Paths to snapshot serializer modules.
Expand Down Expand Up @@ -979,7 +980,7 @@ export interface ResolvedConfig
mode: VitestRunMode

base?: string
diff?: string
diff?: string | SerializedDiffOptions
bail?: number

setupFiles: string[]
Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/runtime/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { SerializedDiffOptions } from '@vitest/utils/diff'

/**
* Config that tests have access to.
Expand Down Expand Up @@ -98,7 +99,7 @@ export interface SerializedConfig {
showDiff?: boolean
truncateThreshold?: number
} | undefined
diff: string | undefined
diff: string | SerializedDiffOptions | undefined
retry: number
includeTaskLocation: boolean | undefined
inspect: boolean | string | undefined
Expand Down
3 changes: 3 additions & 0 deletions packages/vitest/src/runtime/setup-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 10 additions & 0 deletions test/config/fixtures/diff/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expect, test } from 'vitest'

test('large diff', () => {
const x = [...Array(30)].map((_, i) => i);
const y = [...x];
y[0] = 1000;
y[15] = 2000;
y[29] = 3000;
expect(x).toEqual(y)
})
3 changes: 3 additions & 0 deletions test/config/fixtures/diff/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {defineConfig} from 'vitest/config'

export default defineConfig({})
83 changes: 83 additions & 0 deletions test/config/test/__snapshots__/diff.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`inline diff options: { expand: false } 1`] = `
[
"- Expected
+ Received

@@ -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,
]",
]
`;

exports[`inline diff options: undefined 1`] = `
[
"- Expected
+ Received

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,
]",
]
`;
21 changes: 21 additions & 0 deletions test/config/test/diff.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +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 { ctx } = await runVitest({
root: './fixtures/diff',
diff: options,
})
const errors = ctx!.state
.getFiles()
.flatMap(f =>
f.tasks.flatMap(t => t.result?.errors ?? []),
)
expect(
errors.map(e => e.diff && stripVTControlCharacters(e.diff)),
).matchSnapshot()
})
Loading