|
1 | 1 | import fs from 'node:fs' |
2 | 2 | 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' |
5 | 5 |
|
6 | 6 | function fsUpdate(file: string, updateFn: (data: string) => string) { |
7 | 7 | fs.writeFileSync(file, updateFn(fs.readFileSync(file, 'utf-8'))) |
@@ -40,3 +40,54 @@ test('summary', async () => { |
40 | 40 | }) |
41 | 41 | expect(vitest.stdout).toContain('Snapshots 2 updated') |
42 | 42 | }) |
| 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