Skip to content

Commit c2beb8c

Browse files
authored
fix(snapshot): fix "obsolete" message on snapshot update re-run (#7129)
1 parent 2a9d67a commit c2beb8c

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

packages/vitest/src/node/core.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,13 +937,15 @@ export class Vitest {
937937
// environment is resolved inside a worker thread
938938
snapshotEnvironment: null as any,
939939
}
940+
this.snapshot.options.updateSnapshot = 'all'
940941
}
941942

942943
/**
943944
* Disable the mode that allows updating snapshots when running tests.
944945
*/
945946
public resetSnapshotUpdate(): void {
946947
delete this.configOverride.snapshotOptions
948+
this.snapshot.options.updateSnapshot = this.config.snapshotOptions.updateSnapshot
947949
}
948950

949951
/**
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__snapshots__
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect, test } from 'vitest'
2+
3+
test('x', () => {
4+
expect(0).toMatchSnapshot()
5+
})
6+
7+
// REMOVE-START
8+
test('y', () => {
9+
expect(0).toMatchSnapshot()
10+
})
11+
// REMOVE-END

test/snapshots/test/summary.test.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'node:fs'
22
import { join } from 'node:path'
3-
import { expect, test } from 'vitest'
4-
import { runVitest } from '../../test-utils'
3+
import { assert, expect, onTestFailed, onTestFinished, test } from 'vitest'
4+
import { editFile, runVitest } from '../../test-utils'
55

66
function fsUpdate(file: string, updateFn: (data: string) => string) {
77
fs.writeFileSync(file, updateFn(fs.readFileSync(file, 'utf-8')))
@@ -40,3 +40,54 @@ test('summary', async () => {
4040
})
4141
expect(vitest.stdout).toContain('Snapshots 2 updated')
4242
})
43+
44+
test('first obsolete then remove', async () => {
45+
const root = join(import.meta.dirname, 'fixtures/summary-removed')
46+
const testFile = join(root, 'basic.test.ts')
47+
const snapshotFile = join(root, '__snapshots__/basic.test.ts.snap')
48+
49+
// reset snapshot
50+
fs.rmSync(snapshotFile, { recursive: true, force: true })
51+
await runVitest({
52+
root,
53+
update: true,
54+
})
55+
expect(fs.readFileSync(snapshotFile, 'utf-8')).toMatchInlineSnapshot(`
56+
"// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
57+
58+
exports[\`x 1\`] = \`0\`;
59+
60+
exports[\`y 1\`] = \`0\`;
61+
"
62+
`)
63+
64+
// watch run
65+
const { ctx, ...result } = await runVitest(
66+
{
67+
watch: true,
68+
root,
69+
},
70+
)
71+
assert(ctx)
72+
onTestFinished(() => {
73+
ctx.close()
74+
})
75+
onTestFailed(() => {
76+
console.error(result.vitest.stdout)
77+
console.error(result.vitest.stderr)
78+
})
79+
80+
// remove `toMatchSnapshot()` and rerun -> obsolete snapshot
81+
editFile(testFile, s => s.replace(/REMOVE-START.*REMOVE-END/s, ''))
82+
await result.vitest.waitForStdout('1 obsolete')
83+
84+
// rerun with update -> remove snapshot
85+
await ctx.updateSnapshot()
86+
await result.vitest.waitForStdout('1 removed')
87+
expect(fs.readFileSync(snapshotFile, 'utf-8')).toMatchInlineSnapshot(`
88+
"// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
89+
90+
exports[\`x 1\`] = \`0\`;
91+
"
92+
`)
93+
})

0 commit comments

Comments
 (0)