@@ -348,7 +348,9 @@ function createVitest(): VitestUtils {
348348
349349 const workerState = getWorkerState ( )
350350
351- const _timers = new FakeTimers ( {
351+ let _timers : FakeTimers
352+
353+ const timers = ( ) => _timers ||= new FakeTimers ( {
352354 global : globalThis ,
353355 config : workerState . config . fakeTimers ,
354356 } )
@@ -374,77 +376,77 @@ function createVitest(): VitestUtils {
374376 }
375377
376378 if ( config )
377- _timers . configure ( { ...workerState . config . fakeTimers , ...config } )
379+ timers ( ) . configure ( { ...workerState . config . fakeTimers , ...config } )
378380 else
379- _timers . configure ( workerState . config . fakeTimers )
381+ timers ( ) . configure ( workerState . config . fakeTimers )
380382
381- _timers . useFakeTimers ( )
383+ timers ( ) . useFakeTimers ( )
382384 return utils
383385 } ,
384386
385387 isFakeTimers ( ) {
386- return _timers . isFakeTimers ( )
388+ return timers ( ) . isFakeTimers ( )
387389 } ,
388390
389391 useRealTimers ( ) {
390- _timers . useRealTimers ( )
392+ timers ( ) . useRealTimers ( )
391393 _mockedDate = null
392394 return utils
393395 } ,
394396
395397 runOnlyPendingTimers ( ) {
396- _timers . runOnlyPendingTimers ( )
398+ timers ( ) . runOnlyPendingTimers ( )
397399 return utils
398400 } ,
399401
400402 async runOnlyPendingTimersAsync ( ) {
401- await _timers . runOnlyPendingTimersAsync ( )
403+ await timers ( ) . runOnlyPendingTimersAsync ( )
402404 return utils
403405 } ,
404406
405407 runAllTimers ( ) {
406- _timers . runAllTimers ( )
408+ timers ( ) . runAllTimers ( )
407409 return utils
408410 } ,
409411
410412 async runAllTimersAsync ( ) {
411- await _timers . runAllTimersAsync ( )
413+ await timers ( ) . runAllTimersAsync ( )
412414 return utils
413415 } ,
414416
415417 runAllTicks ( ) {
416- _timers . runAllTicks ( )
418+ timers ( ) . runAllTicks ( )
417419 return utils
418420 } ,
419421
420422 advanceTimersByTime ( ms : number ) {
421- _timers . advanceTimersByTime ( ms )
423+ timers ( ) . advanceTimersByTime ( ms )
422424 return utils
423425 } ,
424426
425427 async advanceTimersByTimeAsync ( ms : number ) {
426- await _timers . advanceTimersByTimeAsync ( ms )
428+ await timers ( ) . advanceTimersByTimeAsync ( ms )
427429 return utils
428430 } ,
429431
430432 advanceTimersToNextTimer ( ) {
431- _timers . advanceTimersToNextTimer ( )
433+ timers ( ) . advanceTimersToNextTimer ( )
432434 return utils
433435 } ,
434436
435437 async advanceTimersToNextTimerAsync ( ) {
436- await _timers . advanceTimersToNextTimerAsync ( )
438+ await timers ( ) . advanceTimersToNextTimerAsync ( )
437439 return utils
438440 } ,
439441
440442 getTimerCount ( ) {
441- return _timers . getTimerCount ( )
443+ return timers ( ) . getTimerCount ( )
442444 } ,
443445
444446 setSystemTime ( time : number | string | Date ) {
445447 const date = time instanceof Date ? time : new Date ( time )
446448 _mockedDate = date
447- _timers . setSystemTime ( date )
449+ timers ( ) . setSystemTime ( date )
448450 return utils
449451 } ,
450452
@@ -453,11 +455,11 @@ function createVitest(): VitestUtils {
453455 } ,
454456
455457 getRealSystemTime ( ) {
456- return _timers . getRealSystemTime ( )
458+ return timers ( ) . getRealSystemTime ( )
457459 } ,
458460
459461 clearAllTimers ( ) {
460- _timers . clearAllTimers ( )
462+ timers ( ) . clearAllTimers ( )
461463 return utils
462464 } ,
463465
0 commit comments