Skip to content

fakeTimers

  • Type: FakeTimerConfig

Options that Vitest will pass down to @sinon/fake-timers when using vi.useFakeTimers().

fakeTimers.now

  • Type: number | Date
  • Default: Date.now()

Installs fake timers with the specified Unix epoch.

fakeTimers.toFake

  • Type: ('setTimeout' | 'clearTimeout' | 'setImmediate' | 'clearImmediate' | 'setInterval' | 'clearInterval' | 'Date' | 'nextTick' | 'hrtime' | 'requestAnimationFrame' | 'cancelAnimationFrame' | 'requestIdleCallback' | 'cancelIdleCallback' | 'performance' | 'queueMicrotask')[]
  • Default: everything available globally except nextTick and queueMicrotask

An array with names of global methods and APIs to fake.

To only mock setTimeout() and nextTick(), specify this property as ['setTimeout', 'nextTick'].

Mocking nextTick is not supported when running Vitest inside node:child_process by using --pool=forks. NodeJS uses process.nextTick internally in node:child_process and hangs when it is mocked. Mocking nextTick is supported when running Vitest with --pool=threads.

TIP

toFake and toNotFake are mutually exclusive — only set one or the other.

fakeTimers.toNotFake

  • Type: ('setTimeout' | 'clearTimeout' | 'setImmediate' | 'clearImmediate' | 'setInterval' | 'clearInterval' | 'Date' | 'nextTick' | 'hrtime' | 'requestAnimationFrame' | 'cancelAnimationFrame' | 'requestIdleCallback' | 'cancelIdleCallback' | 'performance' | 'queueMicrotask')[]

An array with names of global methods and APIs to keep native. All other available timers will be mocked.

To only mock setInterval(), specify this property as ['setInterval'].

Mocking nextTick is not supported when running Vitest inside node:child_process by using --pool=forks. If toNotFake does not include nextTick, Vitest will throw an error when running with --pool=forks.

TIP

toNotFake and toFake are mutually exclusive — only set one or the other.

fakeTimers.loopLimit

  • Type: number
  • Default: 10_000

The maximum number of timers that will be run when calling vi.runAllTimers().

fakeTimers.shouldAdvanceTime

  • Type: boolean
  • Default: false

Tells @sinonjs/fake-timers to increment mocked time automatically based on the real system time shift (e.g. the mocked time will be incremented by 20ms for every 20ms change in the real system time).

fakeTimers.advanceTimeDelta

  • Type: number
  • Default: 20

Relevant only when using with shouldAdvanceTime: true. increment mocked time by advanceTimeDelta ms every advanceTimeDelta ms change in the real system time.

fakeTimers.shouldClearNativeTimers

  • Type: boolean
  • Default: true

Tells fake timers to clear "native" (i.e. not fake) timers by delegating to their respective handlers. When disabled, it can lead to potentially unexpected behavior if timers existed prior to starting fake timers session.