The fake timers may be useful when a piece of code sets a long timeout that we don't want to wait for in a test.
Rstest provides some utility functions to help you fake timers powered by @sinonjs/fake-timers.
rs.useFakeTimers(config?: FakeTimerInstallOpts) => RstestTo enable mocking timers, you need to call this method. It uses @sinonjs/fake-timers under the hood.
You can also pass a configuration object to customize the behavior of the fake timers.
rs.useRealTimers() => RstestRestores the original timer functions (such as setTimeout, setInterval, etc.), disabling the fake timers.
rs.isFakeTimers() => booleanReturns true if fake timers are currently enabled, otherwise false.
rs.setSystemTime(now?: number | Date) => RstestSets the current system time used by fake timers. Useful for testing code that depends on the current date or time.
rs.getRealSystemTime() => numberReturns the real system time (as a timestamp), even when fake timers are enabled.
rs.runAllTicks() => RstestRuns all queued microtasks (e.g., process.nextTick).
rs.runAllTimers() => RstestExecutes all pending timers (both timeouts and intervals).
rs.runAllTimersAsync() => Promise<Rstest>Asynchronously executes all pending timers.
rs.runOnlyPendingTimers() => RstestRuns only the currently pending timers (does not schedule new ones).
rs.runOnlyPendingTimersAsync() => Promise<Rstest>Asynchronously runs only the currently pending timers.
rs.advanceTimersByTime(ms: number) => RstestAdvances the fake timers by the specified milliseconds, executing any timers scheduled within that time.
rs.advanceTimersByTimeAsync(ms: number) => Promise<Rstest>Asynchronously advances the fake timers by the specified milliseconds.
rs.advanceTimersToNextTimer(steps?: number) => RstestAdvances the timers to the next scheduled timer, optionally for a given number of steps.
rs.advanceTimersToNextTimerAsync(steps?: number) => Promise<Rstest>Asynchronously advances the timers to the next scheduled timer.
rs.advanceTimersToNextFrame() => RstestAdvances the timers to the next animation frame.
rs.getTimerCount() => numberReturns the number of fake timers still left to run.
rs.clearAllTimers() => RstestRemoves all timers that are scheduled to run.