Skip to content

fix(snapshot)!: fail test with obsolete snapshot on CI #7963

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

Merged
merged 7 commits into from
Jun 22, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions packages/runner/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,9 @@ export async function runSuite(suite: Suite, runner: VitestRunner): Promise<void

suite.result.duration = now() - start

updateTask('suite-finished', suite, runner)

await runner.onAfterRunSuite?.(suite)

updateTask('suite-finished', suite, runner)
}
}

Expand Down
8 changes: 8 additions & 0 deletions packages/vitest/src/runtime/runners/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ export class VitestTestRunner implements VitestRunner {
}

const result = await this.snapshotClient.finish(suite.file.filepath)
if (
this.workerState.config.snapshotOptions.updateSnapshot === 'none'
&& result.unchecked
) {
const error = new Error(`Obsolete snapshots found: ${result.uncheckedKeys.join(', ')}`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can print it in a better way (should we? obsolete snapshots are already printed one line at a time, but without the separation by test file)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tweaked the error message. Please check the example in the PR description. It's mostly a copy of the base reporter.

suite.result!.errors ??= []
suite.result!.errors.push(error)
}
await rpc().snapshotSaved(result)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`bar 1`] = `"bar"`;

exports[`foo 1`] = `"foo"`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`bar 1`] = `"bar"`;

exports[`foo 1`] = `"foo"`;
10 changes: 10 additions & 0 deletions test/snapshots/test/fixtures/obsolete/src/test1.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expect, it } from 'vitest'

it('foo', () => {
if (process.env.TEST_OBSOLETE) return
expect("foo").toMatchSnapshot();
})

it('bar', () => {
expect("bar").toMatchSnapshot();
})
9 changes: 9 additions & 0 deletions test/snapshots/test/fixtures/obsolete/src/test2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect, it } from 'vitest'

it('foo', () => {
expect("foo").toMatchSnapshot();
})

it('bar', () => {
expect("bar").toMatchSnapshot();
})
3 changes: 3 additions & 0 deletions test/snapshots/test/fixtures/obsolete/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({})
32 changes: 32 additions & 0 deletions test/snapshots/test/obsolete.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import fs from 'node:fs'
import path from 'node:path'
import { expect, test } from 'vitest'
import { runVitestCli } from '../../test-utils'

test('obsolete snapshot fails CI', async () => {
// cleanup snapshot
const root = path.join(import.meta.dirname, 'fixtures/obsolete')
fs.rmSync(path.join(root, 'src/__snapshots__'), { recursive: true, force: true })

// initial run to write snapshot
let vitest = await runVitestCli('--root', root, '--update')
expect(vitest.stdout).toContain('Snapshots 4 written')
expect(vitest.stderr).toBe('')

// test fails with obsolete snapshots
// (use cli to test `updateSnapshot: 'none'`)
vitest = await runVitestCli(
{
nodeOptions: {
env: {
CI: 'true',
TEST_OBSOLETE: 'true',
},
},
},
'--root',
root,
)
expect(vitest.stdout).toContain('1 obsolete')
expect(vitest.stderr).toContain(`Error: Obsolete snapshots found: foo 1`)
})
Loading