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): initialize snapshot state only once for each file suite #4796

Merged
24 changes: 15 additions & 9 deletions packages/vitest/src/runtime/runners/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export class VitestTestRunner implements VitestRunner {
this.snapshotClient.clear()
}

async onAfterRunFiles() {
const result = await this.snapshotClient.finishCurrentRun()
if (result)
await rpc().snapshotSaved(result)
}

onAfterRunSuite(suite: Suite) {
async onAfterRunSuite(suite: Suite) {
if (this.config.logHeapUsage && typeof process !== 'undefined')
suite.result!.heap = process.memoryUsage().heapUsed

if (suite.mode !== 'skip' && typeof suite.filepath !== 'undefined') {
const result = await this.snapshotClient.finishCurrentRun()
if (result)
await rpc().snapshotSaved(result)
}
Comment on lines +30 to +38
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 moved finishCurrentRun from onAfterRunFiles hook to onAfterRunSuite hook as a refactoring since it would match with calling startCurrentRun in onBeforeRunSuite.

}

onAfterRunTask(test: Test) {
Expand Down Expand Up @@ -63,14 +63,20 @@ export class VitestTestRunner implements VitestRunner {
}

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

this.workerState.current = test
}

onBeforeRunSuite(suite: Suite) {
async onBeforeRunSuite(suite: Suite) {
if (this.cancelRun)
suite.mode = 'skip'

// initialize snapshot state before running file suite
if (suite.mode !== 'skip' && typeof suite.filepath !== 'undefined') {
// default "name" is irrelevant for Vitest since each snapshot assertion
// (e.g. `toMatchSnapshot`) specifies "filepath" / "name" pair explicitly
await this.snapshotClient.startCurrentRun(suite.filepath, '__default_name_', this.workerState.config.snapshotOptions)
}
}

onBeforeTryTask(test: Test) {
Expand Down
13 changes: 13 additions & 0 deletions test/snapshots/test-update/inline-test-template-concurrent.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { it } from 'vitest'

it.concurrent('1st', ({ expect }) => {
expect('hi1').toMatchInlineSnapshot(`"hi1"`)
})

it.concurrent('2nd', ({ expect }) => {
expect('hi2').toMatchInlineSnapshot(`"hi2"`)
})

it.concurrent('3rd', ({ expect }) => {
expect('hi3').toMatchInlineSnapshot(`"hi3"`)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`concurrent suite > snapshot 1`] = `
Object {
"foo": "bar",
}
`;
17 changes: 17 additions & 0 deletions test/snapshots/test/__snapshots__/shapshots.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`concurrent snapshot update 1`] = `
"import { it } from 'vitest'

it.concurrent('1st', ({ expect }) => {
expect('hi1').toMatchInlineSnapshot(\`"hi1"\`)
})

it.concurrent('2nd', ({ expect }) => {
expect('hi2').toMatchInlineSnapshot(\`"hi2"\`)
})

it.concurrent('3rd', ({ expect }) => {
expect('hi3').toMatchInlineSnapshot(\`"hi3"\`)
})
"
`;

exports[`js snapshots generated correctly 1`] = `
"import { describe, expect, test } from 'vitest'

Expand Down
10 changes: 10 additions & 0 deletions test/snapshots/test/shapshots-concurrent-sync.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { describe, it } from 'vitest'

// from https://github.com/vitest-dev/vitest/issues/3361
describe.concurrent('concurrent suite', () => {
it('snapshot', ({ expect }) => {
expect({ foo: 'bar' }).toMatchSnapshot()
})

it('empty test')
})
6 changes: 6 additions & 0 deletions test/snapshots/test/shapshots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ test('js snapshots generated correctly', async () => {
const content = await fs.readFile(path, 'utf8')
expect(content).toMatchSnapshot()
})

test('concurrent snapshot update', async () => {
const path = pathe.resolve(__dirname, '../test-update/inline-test-template-concurrent.test.js')
const content = await fs.readFile(path, 'utf8')
expect(content).toMatchSnapshot()
})
4 changes: 4 additions & 0 deletions test/snapshots/tools/generate-inline-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ const template = resolve(dir, './inline-test-template.js');

(async () => {
await generateInlineTest(template, filepath)
await generateInlineTest(
resolve(dir, './inline-test-template-concurrent.js'),
resolve(dir, '../test-update/inline-test-template-concurrent.test.js'),
)
})()
13 changes: 13 additions & 0 deletions test/snapshots/tools/inline-test-template-concurrent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { it } from 'vitest'

it.concurrent('1st', ({ expect }) => {
expect('hi1').toMatchInlineSnapshot()
})

it.concurrent('2nd', ({ expect }) => {
expect('hi2').toMatchInlineSnapshot()
})

it.concurrent('3rd', ({ expect }) => {
expect('hi3').toMatchInlineSnapshot()
})