mirror of
https://github.com/vitest-dev/vitest.git
synced 2026-02-01 17:36:51 +00:00
fix(runner): fixture needs to be initialized for each test (#4250)
This commit is contained in:
parent
da8d057038
commit
76a9329844
@ -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) {
|
||||
@ -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)
|
||||
|
||||
@ -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)
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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)
|
||||
})
|
||||
@ -223,7 +223,7 @@ describe('test.extend()', () => {
|
||||
afterAll(() => {
|
||||
expect(serviceFn).toBeCalledTimes(0)
|
||||
expect(apiFn).toBeCalledTimes(0)
|
||||
expect(teardownFn).toBeCalledTimes(2)
|
||||
expect(teardownFn).toBeCalledTimes(4)
|
||||
})
|
||||
})
|
||||
|
||||
@ -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)
|
||||
})
|
||||
|
||||
@ -279,18 +279,16 @@ 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 }) => {
|
||||
@ -298,10 +296,33 @@ describe('test.extend()', () => {
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user