Skip to content

Commit 2123874

Browse files
authored
fix(snapshot): remove new snapshots of a failed retry (#12052)
1 parent b5a5533 commit 2123874

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎packages/snapshot/src/port/state.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,10 @@ export default class SnapshotState {
186186
const name = keyToTestName(key)
187187
const count = this._counters.get(name)
188188
if (count > 0) {
189-
if (key in this._snapshotData || key in this._initialData) {
189+
if (key in this._initialData) {
190190
this._snapshotData[key] = this._initialData[key]
191+
} else {
192+
delete this._snapshotData[key]
191193
}
192194
this._counters.set(name, count - 1)
193195
}

‎test/e2e/snapshots/test-update.test.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs, { rmSync } from 'node:fs'
22
import { join } from 'node:path'
33
import { globSync } from 'tinyglobby'
44
import { expect, test } from 'vitest'
5-
import { runVitest } from '../../test-utils'
5+
import { runInlineTests, runVitest } from '../../test-utils'
66

77
test('test update', async () => {
88
// copy fixtures/test-update to fixtures/test-update-result
@@ -84,3 +84,36 @@ function readFiles(dir: string) {
8484
files.sort().map((file) => [file, fs.readFileSync(join(dir, file), 'utf-8')]),
8585
)
8686
}
87+
88+
test('a retry removes the new snapshots of the failed attempt', async () => {
89+
const { stderr, errorTree, fs } = await runInlineTests(
90+
{
91+
'basic.test.ts': `
92+
import { expect, test } from 'vitest'
93+
94+
test('flaky', { retry: 1 }, ({ task }) => {
95+
expect('every attempt').toMatchSnapshot()
96+
if (task.result.retryCount === 0) {
97+
expect('failed attempt').toMatchSnapshot()
98+
throw new Error('boom')
99+
}
100+
})
101+
`,
102+
},
103+
{ update: 'new' },
104+
)
105+
expect(stderr).toBe('')
106+
expect(errorTree()).toMatchInlineSnapshot(`
107+
{
108+
"basic.test.ts": {
109+
"flaky": "passed",
110+
},
111+
}
112+
`)
113+
expect(fs.readFile('__snapshots__/basic.test.ts.snap')).toMatchInlineSnapshot(`
114+
"// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
115+
116+
exports[\`flaky 1\`] = \`"every attempt"\`;
117+
"
118+
`)
119+
})

0 commit comments

Comments
 (0)