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(runner): fix fixture cleanup for concurrent tests #4827

Merged
merged 2 commits into from
Jan 3, 2024
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
11 changes: 8 additions & 3 deletions packages/runner/src/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ export function mergeContextFixtures(fixtures: Record<string, any>, context: { f
}

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

export async function callFixtureCleanup() {
export async function callFixtureCleanup(context: TestContext) {
const cleanupFnArray = cleanupFnArrayMap.get(context) ?? []
for (const cleanup of cleanupFnArray.reverse())
await cleanup()
cleanupFnArray = []
cleanupFnArrayMap.delete(context)
}

export function withFixtures(fn: Function, testContext?: TestContext) {
Expand All @@ -73,6 +74,10 @@ export function withFixtures(fn: Function, testContext?: TestContext) {
fixtureValueMaps.set(context, new Map<FixtureItem, any>())
const fixtureValueMap: Map<FixtureItem, any> = fixtureValueMaps.get(context)!

if (!cleanupFnArrayMap.has(context))
cleanupFnArrayMap.set(context, [])
const cleanupFnArray = cleanupFnArrayMap.get(context)!

const usedFixtures = fixtures.filter(({ prop }) => usedProps.includes(prop))
const pendingFixtures = resolveDeps(usedFixtures)

Expand Down
2 changes: 1 addition & 1 deletion packages/runner/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export async function runTest(test: Test | Custom, runner: VitestRunner) {
try {
await callSuiteHook(test.suite, test, 'afterEach', runner, [test.context, test.suite])
await callCleanupHooks(beforeEachCleanups)
await callFixtureCleanup()
await callFixtureCleanup(test.context)
}
catch (e) {
failTask(test.result, e, runner.config.diffOptions)
Expand Down
45 changes: 45 additions & 0 deletions test/core/test/fixture-concurrent-beforeEach.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { afterAll, beforeEach, expect, test } from 'vitest'

// this test case might look exotic, but a few conditions were required to reproduce the reported bug.
// such particular conditions are marked with "[repro]" in the comments.

let globalA = 0
let globalB = 0

interface MyFixtures {
a: number
b: number
}

export const myTest = test.extend<MyFixtures>({
// [repro] fixture order must be { a, b } and not { b, a }
a: async ({}, use) => {
globalA++
await new Promise<void>(resolve => setTimeout(resolve, 200)) // [repro] async fixture
await use(globalA)
},
b: async ({}, use) => {
globalB++
await use(globalB)
},
})

// [repro] beforeEach uses only "b"
beforeEach<MyFixtures>(({ b }) => {
expect(b).toBeTypeOf('number')
})

afterAll(() => {
expect([globalA, globalB]).toEqual([2, 2])
})

// [repro] concurrent test uses both "a" and "b"
myTest.concurrent('test1', async ({ a, b }) => {
expect(a).toBeTypeOf('number')
expect(b).toBeTypeOf('number')
})

myTest.concurrent('test2', async ({ a, b }) => {
expect(a).toBeTypeOf('number')
expect(b).toBeTypeOf('number')
})
Comment on lines +27 to +45
Copy link
Contributor Author

@hi-ogawa hi-ogawa Dec 30, 2023

Choose a reason for hiding this comment

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

What's happening was that, due to concurrency:

  • beforeEach: setup "b" fixture for "test1" context
  • beforeEach: setup "b" fixture for "test2" context
  • "test1" setup "a" fixture
  • callFixtureCleanup for "test1" (but actually it also cleans up "b" fixture for "test2")
  • "test2" setup "a" fixture and "b" fixture again (3rd time)
  • callFixtureCleanup for "test2"

Loading