Skip to content

Commit ac9ba15

Browse files
authoredJan 7, 2025
fix(snapshot): fix obsoleteness check of toMatchSnapshot("...") (#7126)
1 parent 74dbe03 commit ac9ba15

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed
 

Diff for: ‎packages/snapshot/src/port/state.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ export default class SnapshotState {
109109

110110
markSnapshotsAsCheckedForTest(testName: string): void {
111111
this._uncheckedKeys.forEach((uncheckedKey) => {
112-
if (keyToTestName(uncheckedKey) === testName) {
112+
// skip snapshots with following keys
113+
// testName n
114+
// testName > xxx n (this is for toMatchSnapshot("xxx") API)
115+
if (/ \d+$| > /.test(uncheckedKey.slice(testName.length))) {
113116
this._uncheckedKeys.delete(uncheckedKey)
114117
}
115118
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`custom a > x 1`] = `0`;
4+
5+
exports[`custom a > y 1`] = `0`;
6+
7+
exports[`custom b > w 1`] = `0`;
8+
9+
exports[`custom b > z 1`] = `0`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect, test } from 'vitest';
2+
3+
test('custom a', () => {
4+
expect(0).toMatchSnapshot('x');
5+
expect(0).toMatchSnapshot('y');
6+
});
7+
8+
test('custom b', () => {
9+
expect(0).toMatchSnapshot('z');
10+
expect(0).toMatchSnapshot('w');
11+
});

Diff for: ‎test/snapshots/test/skip-test.test.ts

+66
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'node:fs'
2+
import path from 'node:path'
23
import { expect, test } from 'vitest'
34
import { runVitest } from '../../test-utils'
45

@@ -46,3 +47,68 @@ test('snapshots in skipped test/suite is not obsolete', async () => {
4647
"
4748
`)
4849
})
50+
51+
test('handle obsoleteness of toMatchSnapshot("custom message")', async () => {
52+
const root = path.join(import.meta.dirname, './fixtures/skip-test-custom')
53+
54+
// clear snapshots
55+
fs.rmSync(path.join(root, '__snapshots__'), { recursive: true, force: true })
56+
57+
// create snapshot on first run
58+
let vitest = await runVitest({
59+
root,
60+
update: true,
61+
})
62+
expect(vitest.stdout).toContain('Snapshots 4 written')
63+
expect(fs.readFileSync(path.join(root, '__snapshots__/basic.test.ts.snap'), 'utf-8')).toMatchInlineSnapshot(`
64+
"// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
65+
66+
exports[\`custom a > x 1\`] = \`0\`;
67+
68+
exports[\`custom a > y 1\`] = \`0\`;
69+
70+
exports[\`custom b > w 1\`] = \`0\`;
71+
72+
exports[\`custom b > z 1\`] = \`0\`;
73+
"
74+
`)
75+
76+
// Skipped tests' `toMatchSnapshot("...")` is not considered obsolete
77+
vitest = await runVitest({
78+
root,
79+
testNamePattern: 'custom a',
80+
})
81+
expect(vitest.stdout).toContain('1 passed')
82+
expect(vitest.stdout).toContain('1 skipped')
83+
expect(vitest.stdout).not.toContain('obsolete')
84+
85+
vitest = await runVitest({
86+
root,
87+
testNamePattern: 'custom b',
88+
})
89+
expect(vitest.stdout).toContain('1 passed')
90+
expect(vitest.stdout).toContain('1 skipped')
91+
expect(vitest.stdout).not.toContain('obsolete')
92+
93+
// check snapshot doesn't change when skip + update
94+
vitest = await runVitest({
95+
root,
96+
update: true,
97+
testNamePattern: 'custom a',
98+
})
99+
expect(vitest.stdout).toContain('1 passed')
100+
expect(vitest.stdout).toContain('1 skipped')
101+
expect(vitest.stdout).not.toContain('obsolete')
102+
expect(fs.readFileSync(path.join(root, '__snapshots__/basic.test.ts.snap'), 'utf-8')).toMatchInlineSnapshot(`
103+
"// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
104+
105+
exports[\`custom a > x 1\`] = \`0\`;
106+
107+
exports[\`custom a > y 1\`] = \`0\`;
108+
109+
exports[\`custom b > w 1\`] = \`0\`;
110+
111+
exports[\`custom b > z 1\`] = \`0\`;
112+
"
113+
`)
114+
})

0 commit comments

Comments
 (0)
Please sign in to comment.