Skip to content

Commit

Permalink
fix(snapshot): fix "obsolete" message on snapshot update re-run (#7129)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Dec 27, 2024
1 parent 2a9d67a commit c2beb8c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,13 +937,15 @@ export class Vitest {
// environment is resolved inside a worker thread
snapshotEnvironment: null as any,
}
this.snapshot.options.updateSnapshot = 'all'
}

/**
* Disable the mode that allows updating snapshots when running tests.
*/
public resetSnapshotUpdate(): void {
delete this.configOverride.snapshotOptions
this.snapshot.options.updateSnapshot = this.config.snapshotOptions.updateSnapshot
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/snapshots/test/fixtures/summary-removed/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__snapshots__
11 changes: 11 additions & 0 deletions test/snapshots/test/fixtures/summary-removed/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect, test } from 'vitest'

test('x', () => {
expect(0).toMatchSnapshot()
})

// REMOVE-START
test('y', () => {
expect(0).toMatchSnapshot()
})
// REMOVE-END
55 changes: 53 additions & 2 deletions test/snapshots/test/summary.test.ts
Original file line number Diff line number Diff line change
@@ -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')))
Expand Down Expand Up @@ -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\`;
"
`)
})

0 comments on commit c2beb8c

Please sign in to comment.