Skip to content

Commit 391860f

Browse files
hi-ogawasheremet-vaspamshaker
authored
feat: support inline diff options and support printBasicPrototype (#6740)
Co-authored-by: Vladimir <[email protected]> Co-authored-by: Michał Grzegorzewski <[email protected]>
1 parent 32f23b9 commit 391860f

File tree

16 files changed

+295
-20
lines changed

16 files changed

+295
-20
lines changed

docs/config/index.md

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,22 +2284,32 @@ export default defineConfig({
22842284
### diff
22852285

22862286
- **Type:** `string`
2287-
- **CLI:** `--diff=<value>`
2287+
- **CLI:** `--diff=<path>`
22882288

2289-
Path to a diff config that will be used to generate diff interface. Useful if you want to customize diff display.
2289+
`DiffOptions` object or a path to a module which exports `DiffOptions`. Useful if you want to customize diff display.
2290+
2291+
For example, as a config object:
22902292

22912293
:::code-group
2292-
```ts [vitest.diff.ts]
2293-
import type { DiffOptions } from 'vitest'
2294-
import c from 'tinyrainbow'
2294+
```ts [vitest.config.js]
2295+
import { defineConfig } from 'vitest/config'
2296+
import c from 'picocolors'
22952297

2296-
export default {
2297-
aIndicator: c.bold('--'),
2298-
bIndicator: c.bold('++'),
2299-
omitAnnotationLines: true,
2300-
} satisfies DiffOptions
2298+
export default defineConfig({
2299+
test: {
2300+
diff: {
2301+
aIndicator: c.bold('--'),
2302+
bIndicator: c.bold('++'),
2303+
omitAnnotationLines: true,
2304+
}
2305+
}
2306+
})
23012307
```
2308+
:::
23022309

2310+
Or as a module:
2311+
2312+
:::code-group
23032313
```ts [vitest.config.js]
23042314
import { defineConfig } from 'vitest/config'
23052315

@@ -2309,12 +2319,32 @@ export default defineConfig({
23092319
}
23102320
})
23112321
```
2322+
2323+
```ts [vitest.diff.ts]
2324+
import type { DiffOptions } from 'vitest'
2325+
import c from 'picocolors'
2326+
2327+
export default {
2328+
aIndicator: c.bold('--'),
2329+
bIndicator: c.bold('++'),
2330+
omitAnnotationLines: true,
2331+
} satisfies DiffOptions
2332+
```
23122333
:::
23132334

2335+
#### diff.expand
2336+
2337+
- **Type**: `boolean`
2338+
- **Default**: `true`
2339+
- **CLI:** `--diff.expand=false`
2340+
2341+
Expand all common lines.
2342+
23142343
#### diff.truncateThreshold
23152344

23162345
- **Type**: `number`
23172346
- **Default**: `0`
2347+
- **CLI:** `--diff.truncateThreshold=<path>`
23182348

23192349
The maximum length of diff result to be displayed. Diffs above this threshold will be truncated.
23202350
Truncation won't take effect with default value 0.
@@ -2323,6 +2353,7 @@ Truncation won't take effect with default value 0.
23232353

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

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

@@ -2333,6 +2364,13 @@ Annotation that is output at the end of diff result if it's truncated.
23332364

23342365
Color of truncate annotation, default is output with no color.
23352366

2367+
#### diff.printBasicPrototype
2368+
2369+
- **Type**: `boolean`
2370+
- **Default**: `true`
2371+
2372+
Print basic prototype `Object` and `Array` in diff output
2373+
23362374
### fakeTimers
23372375

23382376
- **Type:** `FakeTimerInstallOpts`

packages/browser/src/node/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export default (browserServer: BrowserServer, base = '/'): Plugin[] => {
230230
'msw/browser',
231231
]
232232

233-
if (project.config.diff) {
233+
if (typeof project.config.diff === 'string') {
234234
entries.push(project.config.diff)
235235
}
236236

packages/utils/src/diff/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { getType } from './getType'
2323
import { normalizeDiffOptions } from './normalizeDiffOptions'
2424
import { diffStringsRaw, diffStringsUnified } from './printDiffs'
2525

26-
export type { DiffOptions, DiffOptionsColor } from './types'
26+
export type { DiffOptions, DiffOptionsColor, SerializedDiffOptions } from './types'
2727

2828
export { diffLinesRaw, diffLinesUnified, diffLinesUnified2 }
2929
export { diffStringsRaw, diffStringsUnified }
@@ -180,11 +180,12 @@ function getFormatOptions(
180180
formatOptions: PrettyFormatOptions,
181181
options?: DiffOptions,
182182
): PrettyFormatOptions {
183-
const { compareKeys } = normalizeDiffOptions(options)
183+
const { compareKeys, printBasicPrototype } = normalizeDiffOptions(options)
184184

185185
return {
186186
...formatOptions,
187187
compareKeys,
188+
printBasicPrototype,
188189
}
189190
}
190191

packages/utils/src/diff/normalizeDiffOptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function getDefaultOptions(): DiffOptionsNormalized {
3434
includeChangeCounts: false,
3535
omitAnnotationLines: false,
3636
patchColor: c.yellow,
37+
printBasicPrototype: true,
3738
truncateThreshold: DIFF_TRUNCATE_THRESHOLD_DEFAULT,
3839
truncateAnnotation: '... Diff result is truncated',
3940
truncateAnnotationColor: noColor,

packages/utils/src/diff/types.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,29 @@ export interface DiffOptions {
2626
includeChangeCounts?: boolean
2727
omitAnnotationLines?: boolean
2828
patchColor?: DiffOptionsColor
29+
printBasicPrototype?: boolean
2930
compareKeys?: CompareKeys
3031
truncateThreshold?: number
3132
truncateAnnotation?: string
3233
truncateAnnotationColor?: DiffOptionsColor
3334
}
3435

36+
export interface SerializedDiffOptions {
37+
aAnnotation?: string
38+
aIndicator?: string
39+
bAnnotation?: string
40+
bIndicator?: string
41+
commonIndicator?: string
42+
contextLines?: number
43+
emptyFirstOrLastLinePlaceholder?: string
44+
expand?: boolean
45+
includeChangeCounts?: boolean
46+
omitAnnotationLines?: boolean
47+
printBasicPrototype?: boolean
48+
truncateThreshold?: number
49+
truncateAnnotation?: string
50+
}
51+
3552
export interface DiffOptionsNormalized {
3653
aAnnotation: string
3754
aColor: DiffOptionsColor
@@ -51,6 +68,7 @@ export interface DiffOptionsNormalized {
5168
includeChangeCounts: boolean
5269
omitAnnotationLines: boolean
5370
patchColor: DiffOptionsColor
71+
printBasicPrototype: boolean
5472
truncateThreshold: number
5573
truncateAnnotation: string
5674
truncateAnnotationColor: DiffOptionsColor

packages/vitest/src/node/cli/cli-config.ts

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,58 @@ export const cliOptionsConfig: VitestCLIOptions = {
599599
},
600600
diff: {
601601
description:
602-
'Path to a diff config that will be used to generate diff interface',
602+
'DiffOptions object or a path to a module which exports DiffOptions object',
603603
argument: '<path>',
604-
normalize: true,
604+
subcommands: {
605+
aAnnotation: {
606+
description: 'Annotation for expected lines (default: `Expected`)',
607+
argument: '<annotation>',
608+
},
609+
aIndicator: {
610+
description: 'Indicator for expected lines (default: `-`)',
611+
argument: '<indicator>',
612+
},
613+
bAnnotation: {
614+
description: 'Annotation for received lines (default: `Received`)',
615+
argument: '<annotation>',
616+
},
617+
bIndicator: {
618+
description: 'Indicator for received lines (default: `+`)',
619+
argument: '<indicator>',
620+
},
621+
commonIndicator: {
622+
description: 'Indicator for common lines (default: ` `)',
623+
argument: '<indicator>',
624+
},
625+
contextLines: {
626+
description: 'Number of lines of context to show around each change (default: `5`)',
627+
argument: '<lines>',
628+
},
629+
emptyFirstOrLastLinePlaceholder: {
630+
description: 'Placeholder for an empty first or last line (default: `""`)',
631+
argument: '<placeholder>',
632+
},
633+
expand: {
634+
description: 'Expand all common lines (default: `true`)',
635+
},
636+
includeChangeCounts: {
637+
description: 'Include comparison counts in diff output (default: `false`)',
638+
},
639+
omitAnnotationLines: {
640+
description: 'Omit annotation lines from the output (default: `false`)',
641+
},
642+
printBasicPrototype: {
643+
description: 'Print basic prototype Object and Array (default: `true`)',
644+
},
645+
truncateThreshold: {
646+
description: 'Number of lines to show before and after each change (default: `0`)',
647+
argument: '<threshold>',
648+
},
649+
truncateAnnotation: {
650+
description: 'Annotation for truncated lines (default: `... Diff result is truncated`)',
651+
argument: '<annotation>',
652+
},
653+
},
605654
},
606655
exclude: {
607656
description: 'Additional file globs to be excluded from test',

packages/vitest/src/node/config/resolveConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ export function resolveConfig(
578578
}
579579
}
580580

581-
if (resolved.diff) {
581+
if (typeof resolved.diff === 'string') {
582582
resolved.diff = resolvePath(resolved.diff, resolved.root)
583583
resolved.forceRerunTriggers.push(resolved.diff)
584584
}

packages/vitest/src/node/config/serializeConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export function serializeConfig(
3737
pool: config.pool,
3838
expect: config.expect,
3939
snapshotSerializers: config.snapshotSerializers,
40+
// TODO: non serializable function?
4041
diff: config.diff,
4142
retry: config.retry,
4243
disableConsoleIntercept: config.disableConsoleIntercept,

packages/vitest/src/node/types/config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { FakeTimerInstallOpts } from '@sinonjs/fake-timers'
22
import type { PrettyFormatOptions } from '@vitest/pretty-format'
33
import type { SequenceHooks, SequenceSetupFiles } from '@vitest/runner'
44
import type { SnapshotStateOptions } from '@vitest/snapshot'
5+
import type { SerializedDiffOptions } from '@vitest/utils/diff'
56
import type { AliasOptions, ConfigEnv, DepOptimizationConfig, ServerOptions, UserConfig as ViteUserConfig } from 'vite'
67
import type { ViteNodeServerOptions } from 'vite-node'
78
import type { ChaiConfig } from '../../integrations/chai/config'
@@ -563,7 +564,7 @@ export interface InlineConfig {
563564
/**
564565
* Path to a module which has a default export of diff config.
565566
*/
566-
diff?: string
567+
diff?: string | SerializedDiffOptions
567568

568569
/**
569570
* Paths to snapshot serializer modules.
@@ -979,7 +980,7 @@ export interface ResolvedConfig
979980
mode: VitestRunMode
980981

981982
base?: string
982-
diff?: string
983+
diff?: string | SerializedDiffOptions
983984
bail?: number
984985

985986
setupFiles: string[]

packages/vitest/src/runtime/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { PrettyFormatOptions } from '@vitest/pretty-format'
33
import type { SequenceHooks, SequenceSetupFiles } from '@vitest/runner'
44
import type { SnapshotUpdateState } from '@vitest/snapshot'
55
import type { SnapshotEnvironment } from '@vitest/snapshot/environment'
6+
import type { SerializedDiffOptions } from '@vitest/utils/diff'
67

78
/**
89
* Config that tests have access to.
@@ -98,7 +99,7 @@ export interface SerializedConfig {
9899
showDiff?: boolean
99100
truncateThreshold?: number
100101
} | undefined
101-
diff: string | undefined
102+
diff: string | SerializedDiffOptions | undefined
102103
retry: number
103104
includeTaskLocation: boolean | undefined
104105
inspect: boolean | string | undefined

0 commit comments

Comments
 (0)