JavaScript & TypeScript / Quiz
Fake Timers: Bending Time Without Breaking Tests
Debounce in zero milliseconds, the runAllTimers infinite loop, async timer variants, and the cleanup everyone forgets.
- Difficulty
- Medium
- Format
- Quiz
- Points
- 150
- Estimate
- 10 min
// MISSION BRIEF
Your Mission
A 300ms debounce, a 30s polling loop, a session that expires at midnight. Tested naively, these make suites slow AND flaky. Fake timers make them instant and deterministic, if you drive the clock correctly.
This quiz covers jest.useFakeTimers and its Vitest twin vi.useFakeTimers: advancing time, the run-all trap, promises tangled with timers, and freezing the system clock.
Speed bonus active.
// FIRST CONTACT
Battle teaser
jest.useFakeTimers() does what to your test environment?
- AReplaces timer APIs (setTimeout, setInterval, and with modern timers also Date) with controllable fakes, so scheduled callbacks fire only when the test advances the clock
- BMakes real timers run 100x faster
- COnly affects timers created inside the test file itself
- DDisables all timers so setTimeout callbacks never run
Answers, scoring, hints, and the full battle stay sealed.
// SKILL TAGS
javascriptjestvitestfake-timersflakiness