-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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(vitest): fix file snapshots in skipped suites considered obsolete #4795
Merged
sheremet-va
merged 5 commits into
vitest-dev:main
from
hi-ogawa:fix-skip-suite-snapshot
Dec 28, 2023
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
74ac917
fix(vitest): fix skipped suite with snapshots considered obsolete
hi-ogawa 2ed44f9
test: add test
hi-ogawa ee72d9d
chore: minor cleanup
hi-ogawa 3413ae2
Merge branch 'main' into fix-skip-suite-snapshot
hi-ogawa 4b480ef
chore: comment
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 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/skip-test/__snapshots__/repro.test.ts.snap
This file contains 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[`repro suite > inner case 1`] = `"hi-1"`; | ||
|
||
exports[`top-level case 1`] = `"hi-2"`; |
This file contains 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,19 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
const ENABLE_SKIP = process.env.ENABLE_SKIP; | ||
|
||
describe.skipIf(ENABLE_SKIP)('repro suite', () => { | ||
it('inner case', () => { | ||
expect('hi-1').toMatchSnapshot() | ||
}) | ||
}) | ||
|
||
it.skipIf(ENABLE_SKIP)('top-level case', () => { | ||
expect('hi-2').toMatchSnapshot() | ||
}) | ||
|
||
// requires at least one non-skipped test to trigger | ||
// `SnapshotClient.startCurrentRun` on current file | ||
it('normal case', () => { | ||
sheremet-va marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(0).toBe(0) | ||
}) |
This file contains 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 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,48 @@ | ||
import fs from 'node:fs' | ||
import { expect, test } from 'vitest' | ||
import { runVitest } from '../../test-utils' | ||
|
||
test('snapshots in skipped test/suite is not obsolete', async () => { | ||
// create snapshot on first run | ||
fs.rmSync('test/fixtures/skip-test/__snapshots__', { recursive: true, force: true }) | ||
let vitest = await runVitest({ | ||
root: 'test/fixtures/skip-test', | ||
update: true, | ||
}) | ||
expect(vitest.stdout).toContain('Snapshots 2 written') | ||
expect(fs.readFileSync('test/fixtures/skip-test/__snapshots__/repro.test.ts.snap', 'utf-8')).toMatchInlineSnapshot(` | ||
"// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[\`repro suite > inner case 1\`] = \`"hi-1"\`; | ||
|
||
exports[\`top-level case 1\`] = \`"hi-2"\`; | ||
" | ||
`) | ||
|
||
// running with `skipIf` enabled should not show "obsolete" | ||
vitest = await runVitest({ | ||
root: 'test/fixtures/skip-test', | ||
env: { | ||
ENABLE_SKIP: '1', | ||
}, | ||
}) | ||
expect(vitest.stdout).toContain('2 skipped') | ||
expect(vitest.stdout).not.toContain('obsolete') | ||
|
||
// running with `skipIf` and `update` should keep snapshots | ||
vitest = await runVitest({ | ||
root: 'test/fixtures/skip-test', | ||
update: true, | ||
env: { | ||
ENABLE_SKIP: '1', | ||
}, | ||
}) | ||
expect(fs.readFileSync('test/fixtures/skip-test/__snapshots__/repro.test.ts.snap', 'utf-8')).toMatchInlineSnapshot(` | ||
"// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[\`repro suite > inner case 1\`] = \`"hi-1"\`; | ||
|
||
exports[\`top-level case 1\`] = \`"hi-2"\`; | ||
" | ||
`) | ||
}) |
This file contains 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
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'm assuming snapshot system currently doesn't work when there are multiple
files
running here, but I'm not able to reproduce such case yet.My assumption might be totally wrong, so I'm still investigating a little more.
EDIT: Actually, this approach might have a problem even when
files
are sequentially handled, which probably happens whensingleFork
,singleThread
, etc...In the sequential case,
snapshotClient.startCurrentRun/finishCurrentRun
works fine, butskipTestSnapshots
would be broken.(Maybe not... If that happens, probably
finishCurrentRun
result would be ignored in some cases.)EDIT2: Well, it looks fine actually because currently
runtime/entry.ts
always iteratesfile
sequentially and callsstartTests
with a single file:vitest/packages/vitest/src/runtime/entry.ts
Line 46 in ee72d9d
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.
The code should be moved to
onAfterRunSuite
ifSnapshotClient.finishCurrentRun
is moved toonAfterRunSuite
by #4796.