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.
jasmine.clock().install() replaces native setTimeout, setInterval and Date with a controllable fake clock. Advance virtual time with clock.tick(ms) to test timer-dependent code synchronously, then uninstall to restore natives and avoid spec leakage.
Goal I need deterministic tests for date formatting that uses Intl.DateTimeFormat with an explicit timeZone option (for example America/New_York ). The plan is to freeze the clock with jest.useFakeTimers() and jest.setSystemTime(new Date('2026-10-12T00:00:00Z')) so assertions on formatted output are stable. Constraints and uncertainty Two issues are unclear.