[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

feat: add `diff.maxDepth` option and set non-`Infinity` value as a default to reduce crash (#7481)

authored by

Hiroshi Ogawa and committed by
GitHub
(Mar 17, 2025, 1:03 PM +0100) eacab25e f9e1cb49

+38 -3
+7
docs/config/index.md
··· 2331 2331 2332 2332 Print basic prototype `Object` and `Array` in diff output 2333 2333 2334 + #### diff.maxDepth 2335 + 2336 + - **Type**: `number` 2337 + - **Default**: `20` (or `8` when comparing different types) 2338 + 2339 + Limit the depth to recurse when printing nested objects 2340 + 2334 2341 ### fakeTimers 2335 2342 2336 2343 - **Type:** `FakeTimerInstallOpts`
+19
test/core/test/diff.test.ts
··· 324 324 expect(diff.trim()).toMatch(/\.\.\.$/) 325 325 }) 326 326 327 + test('diff default maxDepth', () => { 328 + function generateCycle(n: number) { 329 + const nodes = Array.from({ length: n }, (_, i) => ({ i, next: null as any })) 330 + nodes.forEach((node, i) => { 331 + node.next = nodes[(i + 1) % n] 332 + }) 333 + return nodes 334 + } 335 + 336 + // diff only appears in a deeper depth than maxDepth 337 + const xs = generateCycle(20) 338 + const ys = generateCycle(20) 339 + ys[10].i = -1 340 + const diff = getErrorDiff(xs[0], ys[0], { maxDepth: 5 }) 341 + expect(stripVTControlCharacters(diff)).toMatchInlineSnapshot( 342 + `"Compared values have no visual difference."`, 343 + ) 344 + }) 345 + 327 346 function getErrorDiff(actual: unknown, expected: unknown, options?: DiffOptions): string { 328 347 try { 329 348 expect(actual).toEqual(expected)
+5 -3
packages/utils/src/diff/index.ts
··· 53 53 prettyFormatPlugins.Error, 54 54 ] 55 55 const FORMAT_OPTIONS = { 56 + maxDepth: 20, 56 57 plugins: PLUGINS, 57 - } 58 + } satisfies PrettyFormatOptions 58 59 const FALLBACK_FORMAT_OPTIONS = { 59 60 callToJSON: false, 60 61 maxDepth: 8, 61 62 plugins: PLUGINS, 62 - } 63 + } satisfies PrettyFormatOptions 63 64 64 65 // Generate a string that will highlight the difference between two values 65 66 // with green and red. (similar to how github does code diffing) ··· 191 192 formatOptions: PrettyFormatOptions, 192 193 options?: DiffOptions, 193 194 ): PrettyFormatOptions { 194 - const { compareKeys, printBasicPrototype } = normalizeDiffOptions(options) 195 + const { compareKeys, printBasicPrototype, maxDepth } = normalizeDiffOptions(options) 195 196 196 197 return { 197 198 ...formatOptions, 198 199 compareKeys, 199 200 printBasicPrototype, 201 + maxDepth: maxDepth ?? formatOptions.maxDepth, 200 202 } 201 203 } 202 204
+3
packages/utils/src/diff/types.ts
··· 27 27 omitAnnotationLines?: boolean 28 28 patchColor?: DiffOptionsColor 29 29 printBasicPrototype?: boolean 30 + maxDepth?: number 30 31 compareKeys?: CompareKeys 31 32 truncateThreshold?: number 32 33 truncateAnnotation?: string ··· 45 46 includeChangeCounts?: boolean 46 47 omitAnnotationLines?: boolean 47 48 printBasicPrototype?: boolean 49 + maxDepth?: number 48 50 truncateThreshold?: number 49 51 truncateAnnotation?: string 50 52 } ··· 69 71 omitAnnotationLines: boolean 70 72 patchColor: DiffOptionsColor 71 73 printBasicPrototype: boolean 74 + maxDepth?: number 72 75 truncateThreshold: number 73 76 truncateAnnotation: string 74 77 truncateAnnotationColor: DiffOptionsColor
+4
packages/vitest/src/node/cli/cli-config.ts
··· 648 648 printBasicPrototype: { 649 649 description: 'Print basic prototype Object and Array (default: `true`)', 650 650 }, 651 + maxDepth: { 652 + description: 'Limit the depth to recurse when printing nested objects (default: `20`)', 653 + argument: '<maxDepth>', 654 + }, 651 655 truncateThreshold: { 652 656 description: 'Number of lines to show before and after each change (default: `0`)', 653 657 argument: '<threshold>',