-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
sheremet-va
merged 7 commits into
vitest-dev:main
from
hi-ogawa:fix-fail-ci-obsolete-snapshot
Jun 22, 2025
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c965c9d
fix(snapshot): fail ci when obsolete snapshot is found
hi-ogawa a4e4ab3
test: add test
hi-ogawa ec1c009
Merge branch 'main' into fix-fail-ci-obsolete-snapshot
hi-ogawa ee82876
fix: set suite fail
hi-ogawa 189a0e1
fix: add processError
hi-ogawa bc69ca3
fix: tweak message
hi-ogawa 144de2c
Merge branch 'main' into fix-fail-ci-obsolete-snapshot
hi-ogawa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
test/snapshots/test/fixtures/obsolete/src/__snapshots__/test1.test.ts.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"`; |
5 changes: 5 additions & 0 deletions
5
test/snapshots/test/fixtures/obsolete/src/__snapshots__/test2.test.ts.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { defineConfig } from 'vitest/config' | ||
|
||
export default defineConfig({}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.