diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index 6be0b052ab39..23d0fd7a0613 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -937,6 +937,7 @@ export class Vitest { // environment is resolved inside a worker thread snapshotEnvironment: null as any, } + this.snapshot.options.updateSnapshot = 'all' } /** @@ -944,6 +945,7 @@ export class Vitest { */ public resetSnapshotUpdate(): void { delete this.configOverride.snapshotOptions + this.snapshot.options.updateSnapshot = this.config.snapshotOptions.updateSnapshot } /** diff --git a/test/snapshots/test/fixtures/summary-removed/.gitignore b/test/snapshots/test/fixtures/summary-removed/.gitignore new file mode 100644 index 000000000000..b05c2dfa7007 --- /dev/null +++ b/test/snapshots/test/fixtures/summary-removed/.gitignore @@ -0,0 +1 @@ +__snapshots__ diff --git a/test/snapshots/test/fixtures/summary-removed/basic.test.ts b/test/snapshots/test/fixtures/summary-removed/basic.test.ts new file mode 100644 index 000000000000..9aebc6c822d1 --- /dev/null +++ b/test/snapshots/test/fixtures/summary-removed/basic.test.ts @@ -0,0 +1,11 @@ +import { expect, test } from 'vitest' + +test('x', () => { + expect(0).toMatchSnapshot() +}) + +// REMOVE-START +test('y', () => { + expect(0).toMatchSnapshot() +}) +// REMOVE-END diff --git a/test/snapshots/test/summary.test.ts b/test/snapshots/test/summary.test.ts index 5a438afba53c..646d0b065e2b 100644 --- a/test/snapshots/test/summary.test.ts +++ b/test/snapshots/test/summary.test.ts @@ -1,7 +1,7 @@ import fs from 'node:fs' import { join } from 'node:path' -import { expect, test } from 'vitest' -import { runVitest } from '../../test-utils' +import { assert, expect, onTestFailed, onTestFinished, test } from 'vitest' +import { editFile, runVitest } from '../../test-utils' function fsUpdate(file: string, updateFn: (data: string) => string) { fs.writeFileSync(file, updateFn(fs.readFileSync(file, 'utf-8'))) @@ -40,3 +40,54 @@ test('summary', async () => { }) expect(vitest.stdout).toContain('Snapshots 2 updated') }) + +test('first obsolete then remove', async () => { + const root = join(import.meta.dirname, 'fixtures/summary-removed') + const testFile = join(root, 'basic.test.ts') + const snapshotFile = join(root, '__snapshots__/basic.test.ts.snap') + + // reset snapshot + fs.rmSync(snapshotFile, { recursive: true, force: true }) + await runVitest({ + root, + update: true, + }) + expect(fs.readFileSync(snapshotFile, 'utf-8')).toMatchInlineSnapshot(` + "// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + + exports[\`x 1\`] = \`0\`; + + exports[\`y 1\`] = \`0\`; + " + `) + + // watch run + const { ctx, ...result } = await runVitest( + { + watch: true, + root, + }, + ) + assert(ctx) + onTestFinished(() => { + ctx.close() + }) + onTestFailed(() => { + console.error(result.vitest.stdout) + console.error(result.vitest.stderr) + }) + + // remove `toMatchSnapshot()` and rerun -> obsolete snapshot + editFile(testFile, s => s.replace(/REMOVE-START.*REMOVE-END/s, '')) + await result.vitest.waitForStdout('1 obsolete') + + // rerun with update -> remove snapshot + await ctx.updateSnapshot() + await result.vitest.waitForStdout('1 removed') + expect(fs.readFileSync(snapshotFile, 'utf-8')).toMatchInlineSnapshot(` + "// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + + exports[\`x 1\`] = \`0\`; + " + `) +})