Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(snapshot): fix "obsolete" message on snapshot update re-run #7129

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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\`;
"
`)
})
Loading