A comprehensive fake timer implementation for Jest that provides both legacy and modern timer mocking capabilities, enabling developers to control time flow in tests by mocking JavaScript's built-in timer functions.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Backward-compatible fake timer implementation with custom Jest timer control and precise execution ordering. This implementation provides deterministic timer behavior for tests that require the older Jest fake timers API.
Creates a new LegacyFakeTimers instance with the specified configuration.
/**
* Creates a new LegacyFakeTimers instance
* @param options - Configuration object
*/
constructor<TimerRef = unknown>(options: {
global: typeof globalThis;
moduleMocker: ModuleMocker;
timerConfig: TimerConfig<TimerRef>;
config: StackTraceConfig;
maxLoops?: number;
}): LegacyFakeTimers<TimerRef>;Usage Example:
import { LegacyFakeTimers } from "@jest/fake-timers";
const fakeTimers = new LegacyFakeTimers({
global: globalThis,
moduleMocker: moduleMocker,
timerConfig: {
idToRef: (id) => id,
refToId: (ref) => ref
},
config: stackTraceConfig,
maxLoops: 100000
});Enables fake timers by replacing all timer APIs with mock implementations.
/**
* Enables fake timers
*/
useFakeTimers(): void;Restores the original timer implementations.
/**
* Restores original timer implementations
*/
useRealTimers(): void;Temporarily executes a callback with real timers, then restores fake timers.
/**
* Executes callback with real timers temporarily
* @param cb - Callback function to execute
*/
runWithRealTimers(cb: Callback): void;Usage Example:
fakeTimers.runWithRealTimers(() => {
// This code runs with real timers
setTimeout(() => console.log('Real timer'), 100);
});
// Fake timers are restored after callbackExecutes all pending timers, immediates, and ticks until none remain.
/**
* Runs all pending timers until none remain
*/
runAllTimers(): void;Usage Example:
setTimeout(() => console.log('Timer 1'), 100);
setTimeout(() => console.log('Timer 2'), 200);
fakeTimers.runAllTimers();
// Both timers execute in orderExecutes only the timers that are currently pending, preserving execution order.
/**
* Runs only currently pending timers
*/
runOnlyPendingTimers(): void;Executes all pending process.nextTick callbacks.
/**
* Runs all pending process.nextTick callbacks
*/
runAllTicks(): void;Executes all pending setImmediate callbacks.
/**
* Runs all pending setImmediate callbacks
*/
runAllImmediates(): void;Advances the fake clock by the specified number of milliseconds.
/**
* Advances fake time by specified milliseconds
* @param msToRun - Number of milliseconds to advance
*/
advanceTimersByTime(msToRun: number): void;Usage Example:
let executed = false;
setTimeout(() => { executed = true; }, 1000);
fakeTimers.advanceTimersByTime(1000);
console.log(executed); // trueAdvances time to the next scheduled timer and optionally repeats.
/**
* Advances time to the next scheduled timer
* @param steps - Number of timer steps to advance (default: 1)
*/
advanceTimersToNextTimer(steps?: number): void;Usage Example:
setTimeout(() => console.log('First'), 100);
setTimeout(() => console.log('Second'), 200);
fakeTimers.advanceTimersToNextTimer(1);
// Executes 'First', time is now at 100ms
fakeTimers.advanceTimersToNextTimer(1);
// Executes 'Second', time is now at 200msResets all timer state to initial conditions.
/**
* Resets all timer state to initial conditions
*/
reset(): void;Clears all pending timers without executing them.
/**
* Clears all pending timers
*/
clearAllTimers(): void;Returns the current fake time.
/**
* Gets current fake time in milliseconds
* @returns Current fake time
*/
now(): number;Returns the total number of pending timers, immediates, and ticks.
/**
* Gets the total count of pending timers
* @returns Number of pending timers, immediates, and ticks
*/
getTimerCount(): number;Disposes of the fake timers instance and clears all state.
/**
* Disposes of the fake timers instance
*/
dispose(): void;type Callback = (...args: Array<unknown>) => void;
interface TimerConfig<Ref> {
/** Convert timer ID to reference */
idToRef: (id: number) => Ref;
/** Convert reference to timer ID */
refToId: (ref: Ref) => number | void;
}
interface ModuleMocker {
fn<T extends FunctionLike = UnknownFunction>(
implementation?: T
): Mock<T>;
}
interface StackTraceConfig {
rootDir: string;
testMatch: Array<string>;
}Legacy fake timers include protection against infinite recursion:
maxLoops parameter (default: 100,000)