Skip to content

Commit

Permalink
fix: make asynchronous fixtures work concurrently (vitest-dev#4403)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsyddall authored and LorenzoBloedow committed Dec 19, 2023
1 parent 76f46eb commit 952ffc8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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)
})

0 comments on commit 952ffc8

Please sign in to comment.