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(snapshot): fix concurrent SnapshotClient.startCurrentRun with same filepath #4793

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/snapshot/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ export class SnapshotClient {
if (this.snapshotState?.testFilePath !== filepath) {
await this.finishCurrentRun()

// Need to check `Map.get/set` synchronously after async `SnapshotState.create`
// to ensure unique `SnapshotState` for given `filepath`
const snapshotState = await SnapshotState.create(
filepath,
options,
)
if (!this.getSnapshotState(filepath)) {
this.snapshotStateMap.set(
filepath,
await SnapshotState.create(
filepath,
options,
),
snapshotState,
)
}
this.snapshotState = this.getSnapshotState(filepath)
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'),
)
Comment on lines +18 to +21
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case is mostly taken from #4783. Thanks @martypdx!

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