[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.

fix: support snapshot with no object key sorting (#8136)

Co-authored-by: Vladimir <sleuths.slews0s@icloud.com>

authored by

Hiroshi Ogawa
Vladimir
and committed by
GitHub
(Jul 14, 2025, 3:11 PM +0200) e85e396f 66a403ac

+150 -2
+3
packages/pretty-format/src/types.ts
··· 24 24 value?: string 25 25 }> 26 26 27 + /** 28 + * compare function used when sorting object keys, `null` can be used to skip over sorting. 29 + */ 27 30 export type CompareKeys = ((a: string, b: string) => number) | null | undefined 28 31 29 32 type RequiredOptions = Required<PrettyFormatOptions>
+85
test/snapshots/test/compare-keys.test.ts
··· 1 + import fs from 'node:fs' 2 + import { join } from 'node:path' 3 + import { expect, test } from 'vitest' 4 + import { runVitest } from '../../test-utils' 5 + 6 + test('compareKeys', async () => { 7 + const root = join(import.meta.dirname, 'fixtures/compare-keys') 8 + fs.rmSync(join(root, '__snapshots__'), { recursive: true, force: true }) 9 + 10 + // compareKeys null 11 + let vitest = await runVitest({ 12 + root, 13 + update: true, 14 + snapshotFormat: { 15 + compareKeys: null, 16 + }, 17 + }) 18 + expect(vitest.stderr).toBe('') 19 + expect(fs.readFileSync(join(root, '__snapshots__/basic.test.ts.snap'), 'utf-8')).toMatchInlineSnapshot(` 20 + "// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html 21 + 22 + exports[\`compareKeys 1\`] = \` 23 + { 24 + "a": 1, 25 + "b": 2, 26 + "c": 3, 27 + } 28 + \`; 29 + 30 + exports[\`compareKeys 2\`] = \` 31 + { 32 + "c": 1, 33 + "b": 2, 34 + "a": 3, 35 + } 36 + \`; 37 + 38 + exports[\`compareKeys 3\`] = \` 39 + { 40 + "b": 1, 41 + "a": 2, 42 + "c": 3, 43 + } 44 + \`; 45 + " 46 + `) 47 + 48 + // compareKeys undefined 49 + vitest = await runVitest({ 50 + root, 51 + update: true, 52 + snapshotFormat: { 53 + compareKeys: undefined, 54 + }, 55 + }) 56 + expect(vitest.stderr).toBe('') 57 + expect(fs.readFileSync(join(root, '__snapshots__/basic.test.ts.snap'), 'utf-8')).toMatchInlineSnapshot(` 58 + "// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html 59 + 60 + exports[\`compareKeys 1\`] = \` 61 + { 62 + "a": 1, 63 + "b": 2, 64 + "c": 3, 65 + } 66 + \`; 67 + 68 + exports[\`compareKeys 2\`] = \` 69 + { 70 + "a": 3, 71 + "b": 2, 72 + "c": 1, 73 + } 74 + \`; 75 + 76 + exports[\`compareKeys 3\`] = \` 77 + { 78 + "a": 2, 79 + "b": 1, 80 + "c": 3, 81 + } 82 + \`; 83 + " 84 + `) 85 + })
+4
packages/vitest/src/node/config/resolveConfig.ts
··· 489 489 490 490 if (resolved.snapshotFormat && 'plugins' in resolved.snapshotFormat) { 491 491 (resolved.snapshotFormat as any).plugins = [] 492 + // TODO: support it via separate config (like DiffOptions) or via `Function.toString()` 493 + if (typeof resolved.snapshotFormat.compareKeys === 'function') { 494 + throw new TypeError(`"snapshotFormat.compareKeys" function is not supported.`) 495 + } 492 496 } 493 497 494 498 const UPDATE_SNAPSHOT = resolved.update || process.env.UPDATE_SNAPSHOT
-1
packages/vitest/src/node/config/serializeConfig.ts
··· 120 120 updateSnapshot: coreConfig.snapshotOptions.updateSnapshot, 121 121 snapshotFormat: { 122 122 ...coreConfig.snapshotOptions.snapshotFormat, 123 - compareKeys: undefined, 124 123 }, 125 124 expand: 126 125 config.snapshotOptions.expand
+3 -1
packages/vitest/src/node/types/config.ts
··· 568 568 /** 569 569 * Format options for snapshot testing. 570 570 */ 571 - snapshotFormat?: Omit<PrettyFormatOptions, 'plugins'> 571 + snapshotFormat?: Omit<PrettyFormatOptions, 'plugins' | 'compareKeys'> & { 572 + compareKeys?: null | undefined 573 + } 572 574 573 575 /** 574 576 * Path to a module which has a default export of diff config.
+21
test/snapshots/test/fixtures/compare-keys/basic.test.ts
··· 1 + import { test, expect } from 'vitest'; 2 + 3 + test('compareKeys', () => { 4 + expect({ 5 + a: 1, 6 + b: 2, 7 + c: 3, 8 + }).toMatchSnapshot(); 9 + 10 + expect({ 11 + c: 1, 12 + b: 2, 13 + a: 3, 14 + }).toMatchSnapshot(); 15 + 16 + expect({ 17 + b: 1, 18 + a: 2, 19 + c: 3, 20 + }).toMatchSnapshot(); 21 + });
+9
test/snapshots/test/fixtures/compare-keys/vitest.config.ts
··· 1 + import { defineConfig } from "vitest/config"; 2 + 3 + export default defineConfig({ 4 + test: { 5 + snapshotFormat: { 6 + // compareKeys: null, 7 + } 8 + } 9 + })
+25
test/snapshots/test/fixtures/compare-keys/__snapshots__/basic.test.ts.snap
··· 1 + // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html 2 + 3 + exports[`compareKeys 1`] = ` 4 + { 5 + "a": 1, 6 + "b": 2, 7 + "c": 3, 8 + } 9 + `; 10 + 11 + exports[`compareKeys 2`] = ` 12 + { 13 + "a": 3, 14 + "b": 2, 15 + "c": 1, 16 + } 17 + `; 18 + 19 + exports[`compareKeys 3`] = ` 20 + { 21 + "a": 2, 22 + "b": 1, 23 + "c": 3, 24 + } 25 + `;