@@ -2,24 +2,25 @@ import type { FixtureFn, Suite, VitestRunner } from './types'
22import type { File , FixtureOptions , TestContext } from './types/tasks'
33import { createDefer , filterOutComments , isObject } from '@vitest/utils/helpers'
44import { FixtureDependencyError } from './errors'
5- import { getTestFixturesManager } from './map'
5+ import { getTestFixtures } from './map'
66import { getCurrentSuite } from './suite'
77
88export interface TestFixtureItem extends FixtureOptions {
99 name : string
1010 value : unknown
1111 scope : 'test' | 'file' | 'worker'
1212 deps : Set < string >
13- // so it's possible to call { parent } inside the same fixture
13+ // so it's possible to call base fixture inside ({ a: ({ a }, use) => {} })
1414 parent ?: TestFixtureItem
1515}
1616
17- export type TestFixtureRecord = Record < string , TestFixtureItem >
17+ export type UserFixtures = Record < string , unknown >
18+ export type FixtureRegistrations = Map < string , TestFixtureItem >
1819
1920export class TestFixtures {
20- private _suiteContexts : WeakMap < Suite | symbol , Record < string , any > >
21- private _overrides = new WeakMap < Suite , Map < string , TestFixtureItem > > ( )
22- private _registrations : Map < string , TestFixtureItem >
21+ private _suiteContexts : WeakMap < Suite | symbol , /* context object */ Record < string , unknown > >
22+ private _overrides = new WeakMap < Suite , FixtureRegistrations > ( )
23+ private _registrations : FixtureRegistrations
2324
2425 private static _definitions : TestFixtures [ ] = [ ]
2526 private static _builtinFixtures : string [ ] = [
@@ -47,22 +48,20 @@ export class TestFixtures {
4748 return TestFixtures . _definitions . map ( f => f . getFileContext ( file ) )
4849 }
4950
50- constructor (
51- registrations ?: Map < string , TestFixtureItem > ,
52- ) {
51+ constructor ( registrations ?: FixtureRegistrations ) {
5352 this . _registrations = registrations ?? new Map ( )
5453 this . _suiteContexts = new WeakMap ( )
5554 TestFixtures . _definitions . push ( this )
5655 }
5756
58- extend ( runner : VitestRunner , userFixtures : Record < string , any > ) : TestFixtures {
57+ extend ( runner : VitestRunner , userFixtures : UserFixtures ) : TestFixtures {
5958 const { suite } = getCurrentSuite ( )
6059 const isTopLevel = ! suite || suite . file === suite
6160 const registrations = this . parseUserFixtures ( runner , userFixtures , isTopLevel )
6261 return new TestFixtures ( registrations )
6362 }
6463
65- get ( suite : Suite ) : Map < string , TestFixtureItem > {
64+ get ( suite : Suite ) : FixtureRegistrations {
6665 let currentSuite : Suite | undefined = suite
6766 while ( currentSuite ) {
6867 const overrides = this . _overrides . get ( currentSuite )
@@ -78,16 +77,16 @@ export class TestFixtures {
7877 return this . _registrations
7978 }
8079
81- override ( runner : VitestRunner , userFixtures : Record < string , any > ) : void {
80+ override ( runner : VitestRunner , userFixtures : UserFixtures ) : void {
8281 const { suite : currentSuite , file } = getCurrentSuite ( )
8382 const suite = currentSuite || file
8483 const isTopLevel = ! currentSuite || currentSuite . file === currentSuite
8584 // Create a copy of the closest parent's registrations to avoid modifying them
8685 // For chained calls, this.get(suite) returns this suite's overrides; for first call, returns parent's
8786 const suiteRegistrations = new Map ( this . get ( suite ) )
8887 const registrations = this . parseUserFixtures ( runner , userFixtures , isTopLevel , suiteRegistrations )
89- // if defined in top-level, just override all registrations,
90- // we don't support overriding suite-level fixtures anyway (it will throw an error)
88+ // If defined in top-level, just override all registrations
89+ // We don't support overriding suite-level fixtures anyway (it will throw an error)
9190 if ( isTopLevel ) {
9291 this . _registrations = registrations
9392 }
@@ -112,7 +111,7 @@ export class TestFixtures {
112111
113112 private parseUserFixtures (
114113 runner : VitestRunner ,
115- userFixtures : Record < string , any > ,
114+ userFixtures : UserFixtures ,
116115 supportNonTest : boolean ,
117116 registrations = new Map < string , TestFixtureItem > ( this . _registrations ) ,
118117 ) {
@@ -272,7 +271,7 @@ export async function callFixtureCleanupFrom(context: object, fromIndex: number)
272271
273272const contextHasFixturesCache = new WeakMap < TestContext , WeakSet < TestFixtureItem > > ( )
274273
275- export function withFixtures ( runner : VitestRunner , fn : Function , testContext ?: TestContext ) {
274+ export function withFixtures ( fn : Function , testContext ?: TestContext ) {
276275 const collector = getCurrentSuite ( )
277276 const suite = collector . suite || collector . file
278277 return async ( hookContext ?: TestContext ) : Promise < any > => {
@@ -282,20 +281,20 @@ export function withFixtures(runner: VitestRunner, fn: Function, testContext?: T
282281 return fn ( { } )
283282 }
284283
285- const fixturesManager = getTestFixturesManager ( context )
286- if ( ! fixturesManager ) {
284+ const fixtures = getTestFixtures ( context )
285+ if ( ! fixtures ) {
287286 return fn ( context )
288287 }
289288
290- const fixtures = fixturesManager . get ( suite )
291- if ( ! fixtures . size ) {
289+ const registrations = fixtures . get ( suite )
290+ if ( ! registrations . size ) {
292291 return fn ( context )
293292 }
294293
295294 const usedFixtures : TestFixtureItem [ ] = [ ]
296295 const usedProps = getUsedProps ( fn )
297296
298- for ( const fixture of fixtures . values ( ) ) {
297+ for ( const fixture of registrations . values ( ) ) {
299298 if ( fixture . auto || usedProps . has ( fixture . name ) ) {
300299 usedFixtures . push ( fixture )
301300 }
@@ -310,7 +309,7 @@ export function withFixtures(runner: VitestRunner, fn: Function, testContext?: T
310309 }
311310 const cleanupFnArray = cleanupFnArrayMap . get ( context ) !
312311
313- const pendingFixtures = resolveDeps ( usedFixtures , fixtures )
312+ const pendingFixtures = resolveDeps ( usedFixtures , registrations )
314313
315314 if ( ! pendingFixtures . length ) {
316315 return fn ( context )
@@ -344,7 +343,7 @@ export function withFixtures(runner: VitestRunner, fn: Function, testContext?: T
344343 }
345344 else {
346345 const resolvedValue = await resolveScopeFixtureValue (
347- fixturesManager ,
346+ fixtures ,
348347 suite ,
349348 fixture ,
350349 )
@@ -459,7 +458,7 @@ async function resolveFixtureFunction(
459458
460459function resolveDeps (
461460 usedFixtures : TestFixtureItem [ ] ,
462- fixtures : Map < string , TestFixtureItem > ,
461+ registrations : FixtureRegistrations ,
463462 depSet = new Set < TestFixtureItem > ( ) ,
464463 pendingFixtures : TestFixtureItem [ ] = [ ] ,
465464) {
@@ -487,8 +486,8 @@ function resolveDeps(
487486
488487 depSet . add ( fixture )
489488 resolveDeps (
490- [ ...fixture . deps ] . map ( n => n === fixture . name ? fixture . parent : fixtures . get ( n ) ) . filter ( n => ! ! n ) ,
491- fixtures ,
489+ [ ...fixture . deps ] . map ( n => n === fixture . name ? fixture . parent : registrations . get ( n ) ) . filter ( n => ! ! n ) ,
490+ registrations ,
492491 depSet ,
493492 pendingFixtures ,
494493 )
0 commit comments