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(vitest): fix file snapshots in skipped suites considered obsolete #4795

Merged
merged 5 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 14 additions & 6 deletions packages/vitest/src/runtime/runners/test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { CancelReason, Custom, ExtendedContext, Suite, TaskContext, Test, VitestRunner, VitestRunnerImportSource } from '@vitest/runner'
import type { CancelReason, Custom, ExtendedContext, File, Suite, TaskContext, Test, VitestRunner, VitestRunnerImportSource } from '@vitest/runner'
import type { ExpectStatic } from '@vitest/expect'
import { GLOBAL_EXPECT, getState, setState } from '@vitest/expect'
import { getSnapshotClient } from '../../integrations/snapshot/chai'
import { vi } from '../../integrations/vi'
import { getFullName, getNames, getWorkerState } from '../../utils'
import { getFullName, getNames, getTests, getWorkerState } from '../../utils'
import { createExpect } from '../../integrations/chai/index'
import type { ResolvedConfig } from '../../types/config'
import type { VitestExecutor } from '../execute'
Expand All @@ -27,7 +27,17 @@ export class VitestTestRunner implements VitestRunner {
this.snapshotClient.clear()
}

async onAfterRunFiles() {
async onAfterRunFiles(files?: File[]) {
// mark snapshots in skipped tests as not obsolete
// TODO: this probably doesn't work when `VitestTestRunner` is handling multiple files concurrently,
// but `snapshotClient.startCurrentRun/finishCurrentRun` doesn't work in that case already?
for (const test of getTests(files ?? [])) {
if (test.mode === 'skip') {
const name = getNames(test).slice(1).join(' > ')
this.snapshotClient.skipTestSnapshots(name)
}
}
Copy link
Contributor Author

@hi-ogawa hi-ogawa Dec 22, 2023

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 when singleFork, singleThread, etc...
In the sequential case, snapshotClient.startCurrentRun/finishCurrentRun works fine, but skipTestSnapshots 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 iterates file sequentially and calls startTests with a single file:

await startTests([file], runner)

Copy link
Contributor Author

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 if SnapshotClient.finishCurrentRun is moved to onAfterRunSuite by #4796.


const result = await this.snapshotClient.finishCurrentRun()
if (result)
await rpc().snapshotSaved(result)
Expand Down Expand Up @@ -57,10 +67,8 @@ export class VitestTestRunner implements VitestRunner {
if (this.cancelRun)
test.mode = 'skip'

if (test.mode !== 'run') {
this.snapshotClient.skipTestSnapshots(name)
if (test.mode !== 'run')
return
}

clearModuleMocks(this.config)
await this.snapshotClient.startCurrentRun(test.file!.filepath, name, this.workerState.config.snapshotOptions)
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[`repro suite > inner case 1`] = `"hi-1"`;

exports[`top-level case 1`] = `"hi-2"`;
19 changes: 19 additions & 0 deletions test/snapshots/test/fixtures/skip-test/repro.test.ts
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)
})
3 changes: 3 additions & 0 deletions test/snapshots/test/fixtures/skip-test/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({})
48 changes: 48 additions & 0 deletions test/snapshots/test/skip-test.test.ts
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"\`;
"
`)
})
2 changes: 2 additions & 0 deletions test/snapshots/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { defineConfig } from 'vite'
import { defaultExclude } from 'vitest/config'

export default defineConfig({
test: {
globals: true,
exclude: [...defaultExclude, '**/fixtures'],
snapshotFormat: {
printBasicPrototype: true,
},
Expand Down