Skip to content

Commit

Permalink
fix(vitest): initialize snapshot state only once for each file suite (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Dec 28, 2023
1 parent cf53d4b commit 957daa3
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 9 deletions.
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)
}
}

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()
})

0 comments on commit 957daa3

Please sign in to comment.