Skip to content

Commit

Permalink
fix(runner): fixture needs to be initialized for each test (#4250)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing authored Oct 6, 2023
1 parent da8d057 commit 76a9329
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
17 changes: 3 additions & 14 deletions packages/runner/src/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,12 @@ export function mergeContextFixtures(fixtures: Record<string, any>, context: { f
}

const fixtureValueMap = new Map<FixtureItem, any>()
const fixtureCleanupFnMap = new Map<string, Array<() => void | Promise<void>>>()

export async function callFixtureCleanup(id: string) {
const cleanupFnArray = fixtureCleanupFnMap.get(id)
if (!cleanupFnArray)
return
let cleanupFnArray = new Array<() => void | Promise<void>>()

export async function callFixtureCleanup() {
for (const cleanup of cleanupFnArray.reverse())
await cleanup()

fixtureCleanupFnMap.delete(id)
cleanupFnArray = []
}

export function withFixtures(fn: Function, testContext?: TestContext) {
Expand All @@ -65,12 +60,6 @@ export function withFixtures(fn: Function, testContext?: TestContext) {
if (!context)
return fn({})

let cleanupFnArray = fixtureCleanupFnMap.get(context.task.suite.id)!
if (!cleanupFnArray) {
cleanupFnArray = []
fixtureCleanupFnMap.set(context.task.suite.id, cleanupFnArray)
}

const fixtures = getFixture(context)
if (!fixtures?.length)
return fn(context)
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 @@ -189,6 +189,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()
}
catch (e) {
failTask(test.result, e, runner.config.diffOptions)
Expand Down Expand Up @@ -322,7 +323,6 @@ export async function runSuite(suite: Suite, runner: VitestRunner) {
}

try {
await callFixtureCleanup(suite.id)
await callSuiteHook(suite, suite, 'afterAll', runner, [suite])
await callCleanupHooks(beforeAllCleanups)
}
Expand Down
49 changes: 35 additions & 14 deletions test/core/test/test-extend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ describe('test.extend()', () => {
expect(service).toBe(true)
})

testAPI('Should init1 time', ({ api }) => {
testAPI('Should init 1 time', ({ api }) => {
expect(api).toBe(true)
expect(apiFn).toBeCalledTimes(1)
})
Expand All @@ -223,7 +223,7 @@ describe('test.extend()', () => {
afterAll(() => {
expect(serviceFn).toBeCalledTimes(0)
expect(apiFn).toBeCalledTimes(0)
expect(teardownFn).toBeCalledTimes(2)
expect(teardownFn).toBeCalledTimes(4)
})
})

Expand Down Expand Up @@ -266,10 +266,10 @@ describe('test.extend()', () => {
expect(bar).toBe(0)
})

nestedTest('should only initialize bar', ({ foo, bar }) => {
nestedTest('should initialize foo and bar', ({ foo, bar }) => {
expect(foo).toBe(0)
expect(bar).toBe(0)
expect(fooFn).toBeCalledTimes(1)
expect(fooFn).toBeCalledTimes(2)
expect(barFn).toBeCalledTimes(1)
})

Expand All @@ -279,29 +279,50 @@ describe('test.extend()', () => {
})

afterAll(() => {
// foo setup in outside describe
// cleanup also called in outside describe
expect(fooCleanup).toHaveBeenCalledTimes(0)
// bar setup in inside describe
// cleanup also called in inside describe
expect(barFn).toHaveBeenCalledTimes(1)
expect(barCleanup).toHaveBeenCalledTimes(1)
expect(fooFn).toHaveBeenCalledTimes(2)
expect(barCleanup).toHaveBeenCalledTimes(1)
})
})

nestedTest('level 2 will not call foo cleanup', ({ foo }) => {
nestedTest('should initialize foo again', ({ foo }) => {
expect(foo).toBe(0)
expect(fooFn).toBeCalledTimes(1)
expect(fooFn).toBeCalledTimes(3)
})

afterEach<Fixture>(({ foo }) => {
expect(foo).toBe(0)
})

afterAll(() => {
// foo setup in this describe
// cleanup also called in this describe
expect(fooCleanup).toHaveBeenCalledTimes(1)
expect(fooFn).toHaveBeenCalledTimes(3)
expect(fooCleanup).toHaveBeenCalledTimes(3)
expect(barFn).toHaveBeenCalledTimes(1)
expect(barCleanup).toHaveBeenCalledTimes(1)
})
})
})

// test extend with top level test
const numbers: number[] = []
const teardownFn = vi.fn()
const teardownTest = test.extend<{
numbers: number[]
}>({
numbers: async ({}, use) => {
numbers.push(1, 2, 3)
await use(numbers)
numbers.splice(0, numbers.length)
teardownFn()
},
})

teardownTest('test without describe', ({ numbers }) => {
expect(numbers).toHaveLength(3)
})

test('teardown should be called once time', () => {
expect(numbers).toHaveLength(0)
expect(teardownFn).toBeCalledTimes(1)
})

0 comments on commit 76a9329

Please sign in to comment.