Skip to content

Commit 06c14f7

Browse files
authoredDec 28, 2023
fix(vitest): fix file snapshots in skipped suites considered obsolete (#4795)
1 parent 1877d32 commit 06c14f7

File tree

6 files changed

+88
-6
lines changed

6 files changed

+88
-6
lines changed
 

‎packages/vitest/src/runtime/runners/test.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ExpectStatic } from '@vitest/expect'
33
import { GLOBAL_EXPECT, getState, setState } from '@vitest/expect'
44
import { getSnapshotClient } from '../../integrations/snapshot/chai'
55
import { vi } from '../../integrations/vi'
6-
import { getFullName, getNames, getWorkerState } from '../../utils'
6+
import { getFullName, getNames, getTests, getWorkerState } from '../../utils'
77
import { createExpect } from '../../integrations/chai/index'
88
import type { ResolvedConfig } from '../../types/config'
99
import type { VitestExecutor } from '../execute'
@@ -32,6 +32,14 @@ export class VitestTestRunner implements VitestRunner {
3232
suite.result!.heap = process.memoryUsage().heapUsed
3333

3434
if (suite.mode !== 'skip' && typeof suite.filepath !== 'undefined') {
35+
// mark snapshots in skipped tests as not obsolete
36+
for (const test of getTests(suite)) {
37+
if (test.mode === 'skip') {
38+
const name = getNames(test).slice(1).join(' > ')
39+
this.snapshotClient.skipTestSnapshots(name)
40+
}
41+
}
42+
3543
const result = await this.snapshotClient.finishCurrentRun()
3644
if (result)
3745
await rpc().snapshotSaved(result)
@@ -52,15 +60,11 @@ export class VitestTestRunner implements VitestRunner {
5260
}
5361

5462
async onBeforeRunTask(test: Test) {
55-
const name = getNames(test).slice(1).join(' > ')
56-
5763
if (this.cancelRun)
5864
test.mode = 'skip'
5965

60-
if (test.mode !== 'run') {
61-
this.snapshotClient.skipTestSnapshots(name)
66+
if (test.mode !== 'run')
6267
return
63-
}
6468

6569
clearModuleMocks(this.config)
6670

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`repro suite > inner case 1`] = `"hi-1"`;
4+
5+
exports[`top-level case 1`] = `"hi-2"`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, it } from 'vitest'
2+
3+
const ENABLE_SKIP = process.env.ENABLE_SKIP;
4+
5+
describe.skipIf(ENABLE_SKIP)('repro suite', () => {
6+
it('inner case', () => {
7+
expect('hi-1').toMatchSnapshot()
8+
})
9+
})
10+
11+
it.skipIf(ENABLE_SKIP)('top-level case', () => {
12+
expect('hi-2').toMatchSnapshot()
13+
})
14+
15+
// at least one non-skipped test is needed to reproduce a bug.
16+
// without this, there will be no SnapshotClient.startCurrentRun,
17+
// so the code to check skip/obsolete snapshot is not exercised.
18+
it('normal case', () => {
19+
expect(0).toBe(0)
20+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({})

‎test/snapshots/test/skip-test.test.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import fs from 'node:fs'
2+
import { expect, test } from 'vitest'
3+
import { runVitest } from '../../test-utils'
4+
5+
test('snapshots in skipped test/suite is not obsolete', async () => {
6+
// create snapshot on first run
7+
fs.rmSync('test/fixtures/skip-test/__snapshots__', { recursive: true, force: true })
8+
let vitest = await runVitest({
9+
root: 'test/fixtures/skip-test',
10+
update: true,
11+
})
12+
expect(vitest.stdout).toContain('Snapshots 2 written')
13+
expect(fs.readFileSync('test/fixtures/skip-test/__snapshots__/repro.test.ts.snap', 'utf-8')).toMatchInlineSnapshot(`
14+
"// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
15+
16+
exports[\`repro suite > inner case 1\`] = \`"hi-1"\`;
17+
18+
exports[\`top-level case 1\`] = \`"hi-2"\`;
19+
"
20+
`)
21+
22+
// running with `skipIf` enabled should not show "obsolete"
23+
vitest = await runVitest({
24+
root: 'test/fixtures/skip-test',
25+
env: {
26+
ENABLE_SKIP: '1',
27+
},
28+
})
29+
expect(vitest.stdout).toContain('2 skipped')
30+
expect(vitest.stdout).not.toContain('obsolete')
31+
32+
// running with `skipIf` and `update` should keep snapshots
33+
vitest = await runVitest({
34+
root: 'test/fixtures/skip-test',
35+
update: true,
36+
env: {
37+
ENABLE_SKIP: '1',
38+
},
39+
})
40+
expect(fs.readFileSync('test/fixtures/skip-test/__snapshots__/repro.test.ts.snap', 'utf-8')).toMatchInlineSnapshot(`
41+
"// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
42+
43+
exports[\`repro suite > inner case 1\`] = \`"hi-1"\`;
44+
45+
exports[\`top-level case 1\`] = \`"hi-2"\`;
46+
"
47+
`)
48+
})

‎test/snapshots/vitest.config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { defineConfig } from 'vite'
2+
import { defaultExclude } from 'vitest/config'
23

34
export default defineConfig({
45
test: {
56
globals: true,
7+
exclude: [...defaultExclude, '**/fixtures'],
68
snapshotFormat: {
79
printBasicPrototype: true,
810
},

0 commit comments

Comments
 (0)
Please sign in to comment.