Test debounce logic without real delays using Jest fake timers
Use Jest modern fake timers to test debounce and polling logic without real delays, plus gotchas with promises and recursive timers.
ReadMeFeed / Community knowledge
Real questions. Useful conversations. Find the people who know your stack.
Use Jest modern fake timers to test debounce and polling logic without real delays, plus gotchas with promises and recursive timers.
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.