Skip to content

Commit e85e396

Browse files
fix: support snapshot with no object key sorting (#8136)
Co-authored-by: Vladimir <[email protected]>
1 parent 66a403a commit e85e396

File tree

8 files changed

+150
-2
lines changed

8 files changed

+150
-2
lines changed

packages/pretty-format/src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export type Theme = Required<{
2424
value?: string
2525
}>
2626

27+
/**
28+
* compare function used when sorting object keys, `null` can be used to skip over sorting.
29+
*/
2730
export type CompareKeys = ((a: string, b: string) => number) | null | undefined
2831

2932
type RequiredOptions = Required<PrettyFormatOptions>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,10 @@ export function resolveConfig(
489489

490490
if (resolved.snapshotFormat && 'plugins' in resolved.snapshotFormat) {
491491
(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+
}
492496
}
493497

494498
const UPDATE_SNAPSHOT = resolved.update || process.env.UPDATE_SNAPSHOT

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ export function serializeConfig(
120120
updateSnapshot: coreConfig.snapshotOptions.updateSnapshot,
121121
snapshotFormat: {
122122
...coreConfig.snapshotOptions.snapshotFormat,
123-
compareKeys: undefined,
124123
},
125124
expand:
126125
config.snapshotOptions.expand

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,9 @@ export interface InlineConfig {
568568
/**
569569
* Format options for snapshot testing.
570570
*/
571-
snapshotFormat?: Omit<PrettyFormatOptions, 'plugins'>
571+
snapshotFormat?: Omit<PrettyFormatOptions, 'plugins' | 'compareKeys'> & {
572+
compareKeys?: null | undefined
573+
}
572574

573575
/**
574576
* Path to a module which has a default export of diff config.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
})
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
`;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
snapshotFormat: {
6+
// compareKeys: null,
7+
}
8+
}
9+
})

0 commit comments

Comments
 (0)