Jest Modern Fake Timers: Replacing Real Waits with a Virtual Clock
Jest's modern fake timers replace real setTimeout waits with a controllable virtual clock, making time-dependent unit tests fast and deterministic. Here is how to use them safely.
ReadMeFeed / Community knowledge
Real questions. Useful conversations. Find the people who know your stack.
Jest's modern fake timers replace real setTimeout waits with a controllable virtual clock, making time-dependent unit tests fast and deterministic. Here is how to use them safely.
Enabling deterministic time-dependent tests requires selecting the right fake timer variant and restoring real timers after each test to prevent CI flakiness.
Jasmine's built-in mock clock lets you test debounce, expiry, and polling logic deterministically — no real waiting, no flaky CI. Here's how install(), tick(), and mockDate() work, plus the uninstall and microtask pitfalls.
Jest's modern fake timers let you test debounces, retries, and date-sensitive logic in microseconds instead of waiting real time — here's how to use them without hanging your suite.
Learn how Jest's useFakeTimers lets you control Date, setTimeout, and clearTimeout for deterministic, fast tests-complete with a debounce example and cleanup tips.
Jest's fake timers make debounce, retry, and expiry tests fast and deterministic — but captured timer references and lost fidelity mean the decision of when to fake the clock matters more than the API.
Decide when to mock timers in Jest. Modern fake timers keep tests fast and deterministic, but real timers are needed for integration tests that depend on actual elapsed time. A quick decision table, trade‑offs, and a concrete debounce test guide the choice.
When using Jest's modern fake‑timer implementation (jest.useFakeTimers({ modern: true })) and advancing time with jest.advanceTimersByTime, microtasks created by Promise.resolve().then or async/await are not automatically flushed. This leaves pending promises unresolved, causing tests that await those promises to hang unless jest.runOnlyPendingTimers (or jes
Goal: Verify that date‑conversion logic behaves identically when Vitest’s fake timers are used across different developer machines and CI runners. Constraint: vi.useFakeTimers() and vi.setSystemTime() shift the mocked clock but leave the process timezone unchanged; the effective zone is determined by the TZ environment variable that must be set before the Vi