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: make asynchronous fixtures work concurrently #4403

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/runner/src/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function mergeContextFixtures(fixtures: Record<string, any>, context: { f
return context
}

const fixtureValueMap = new Map<FixtureItem, any>()
const fixtureValueMaps = new Map<TestContext, Map<FixtureItem, any>>()
let cleanupFnArray = new Array<() => void | Promise<void>>()

export async function callFixtureCleanup() {
Expand All @@ -68,6 +68,10 @@ export function withFixtures(fn: Function, testContext?: TestContext) {
if (!usedProps.length)
return fn(context)

if (!fixtureValueMaps.get(context))
fixtureValueMaps.set(context, new Map<FixtureItem, any>())
const fixtureValueMap: Map<FixtureItem, any> = fixtureValueMaps.get(context)!

const usedFixtures = fixtures.filter(({ prop }) => usedProps.includes(prop))
const pendingFixtures = resolveDeps(usedFixtures)
let cursor = 0
Expand Down
24 changes: 24 additions & 0 deletions test/core/test/fixture-concurrent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect, test } from 'vitest'

export const myTest = test.extend<{
a: string
b: string
}>({
a: async ({ task }: any, use) => {
await new Promise<void>(resolve => setTimeout(resolve, 200))
await use(task.id)
},
b: async ({ a }, use) => {
await use(a)
},
})

myTest.concurrent('fixture - concurrent test 1', ({ a, b, task }) => {
expect(a).toBe(task.id)
expect(b).toBe(task.id)
})

myTest.concurrent('fixture - concurrent test 2', ({ a, b, task }) => {
expect(a).toBe(task.id)
expect(b).toBe(task.id)
})
Loading