Skip to content

Commit 957daa3

Browse files
authoredDec 28, 2023
fix(vitest): initialize snapshot state only once for each file suite (#4796)
1 parent cf53d4b commit 957daa3

8 files changed

+85
-9
lines changed
 

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

+15-9
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ export class VitestTestRunner implements VitestRunner {
2727
this.snapshotClient.clear()
2828
}
2929

30-
async onAfterRunFiles() {
31-
const result = await this.snapshotClient.finishCurrentRun()
32-
if (result)
33-
await rpc().snapshotSaved(result)
34-
}
35-
36-
onAfterRunSuite(suite: Suite) {
30+
async onAfterRunSuite(suite: Suite) {
3731
if (this.config.logHeapUsage && typeof process !== 'undefined')
3832
suite.result!.heap = process.memoryUsage().heapUsed
33+
34+
if (suite.mode !== 'skip' && typeof suite.filepath !== 'undefined') {
35+
const result = await this.snapshotClient.finishCurrentRun()
36+
if (result)
37+
await rpc().snapshotSaved(result)
38+
}
3939
}
4040

4141
onAfterRunTask(test: Test) {
@@ -63,14 +63,20 @@ export class VitestTestRunner implements VitestRunner {
6363
}
6464

6565
clearModuleMocks(this.config)
66-
await this.snapshotClient.startCurrentRun(test.file!.filepath, name, this.workerState.config.snapshotOptions)
6766

6867
this.workerState.current = test
6968
}
7069

71-
onBeforeRunSuite(suite: Suite) {
70+
async onBeforeRunSuite(suite: Suite) {
7271
if (this.cancelRun)
7372
suite.mode = 'skip'
73+
74+
// initialize snapshot state before running file suite
75+
if (suite.mode !== 'skip' && typeof suite.filepath !== 'undefined') {
76+
// default "name" is irrelevant for Vitest since each snapshot assertion
77+
// (e.g. `toMatchSnapshot`) specifies "filepath" / "name" pair explicitly
78+
await this.snapshotClient.startCurrentRun(suite.filepath, '__default_name_', this.workerState.config.snapshotOptions)
79+
}
7480
}
7581

7682
onBeforeTryTask(test: Test) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { it } from 'vitest'
2+
3+
it.concurrent('1st', ({ expect }) => {
4+
expect('hi1').toMatchInlineSnapshot(`"hi1"`)
5+
})
6+
7+
it.concurrent('2nd', ({ expect }) => {
8+
expect('hi2').toMatchInlineSnapshot(`"hi2"`)
9+
})
10+
11+
it.concurrent('3rd', ({ expect }) => {
12+
expect('hi3').toMatchInlineSnapshot(`"hi3"`)
13+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`concurrent suite > snapshot 1`] = `
4+
Object {
5+
"foo": "bar",
6+
}
7+
`;

‎test/snapshots/test/__snapshots__/shapshots.test.ts.snap

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`concurrent snapshot update 1`] = `
4+
"import { it } from 'vitest'
5+
6+
it.concurrent('1st', ({ expect }) => {
7+
expect('hi1').toMatchInlineSnapshot(\`"hi1"\`)
8+
})
9+
10+
it.concurrent('2nd', ({ expect }) => {
11+
expect('hi2').toMatchInlineSnapshot(\`"hi2"\`)
12+
})
13+
14+
it.concurrent('3rd', ({ expect }) => {
15+
expect('hi3').toMatchInlineSnapshot(\`"hi3"\`)
16+
})
17+
"
18+
`;
19+
320
exports[`js snapshots generated correctly 1`] = `
421
"import { describe, expect, test } from 'vitest'
522
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { describe, it } from 'vitest'
2+
3+
// from https://github.com/vitest-dev/vitest/issues/3361
4+
describe.concurrent('concurrent suite', () => {
5+
it('snapshot', ({ expect }) => {
6+
expect({ foo: 'bar' }).toMatchSnapshot()
7+
})
8+
9+
it('empty test')
10+
})

‎test/snapshots/test/shapshots.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,9 @@ test('js snapshots generated correctly', async () => {
4444
const content = await fs.readFile(path, 'utf8')
4545
expect(content).toMatchSnapshot()
4646
})
47+
48+
test('concurrent snapshot update', async () => {
49+
const path = pathe.resolve(__dirname, '../test-update/inline-test-template-concurrent.test.js')
50+
const content = await fs.readFile(path, 'utf8')
51+
expect(content).toMatchSnapshot()
52+
})

‎test/snapshots/tools/generate-inline-test.mjs

+4
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ const template = resolve(dir, './inline-test-template.js');
1515

1616
(async () => {
1717
await generateInlineTest(template, filepath)
18+
await generateInlineTest(
19+
resolve(dir, './inline-test-template-concurrent.js'),
20+
resolve(dir, '../test-update/inline-test-template-concurrent.test.js'),
21+
)
1822
})()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { it } from 'vitest'
2+
3+
it.concurrent('1st', ({ expect }) => {
4+
expect('hi1').toMatchInlineSnapshot()
5+
})
6+
7+
it.concurrent('2nd', ({ expect }) => {
8+
expect('hi2').toMatchInlineSnapshot()
9+
})
10+
11+
it.concurrent('3rd', ({ expect }) => {
12+
expect('hi3').toMatchInlineSnapshot()
13+
})

0 commit comments

Comments
 (0)
Please sign in to comment.