Tests that wait on real time are slow and can fail under load. A controllable clock makes time-based behavior repeatable, but fake timers should be limited to the code that actually uses a timer.
Make the clock a dependency
- Pass a clock or scheduler into code that owns a deadline, debounce, or polling interval.
- Advance fake time deliberately and await the resulting promise or state update.
- Restore real timers after each test so one test cannot alter another's environment.
clock.advanceBy(500);
await expect(result).resolves.toEqual(expected);Test the boundary, not a sleep
Avoid fixed delays such as waiting 500 milliseconds for a request to finish. Wait for a specific observable condition, and test the just-before, at, and just-after deadline cases when those boundaries matter.
If fake timers make a test hang, inspect the queue of pending timers and promises. Mixing mocked time with libraries that schedule their own work can expose a setup problem rather than a product bug.
