Fake timers let tests advance scheduled work without waiting in real time. Promise scheduling and clock reads still need attention.
Before you start
You should understand inputs, expected outputs and the boundaries of the component under test. State the risk a test should detect before choosing a tool or mock. Distinguish controlled dependencies from real integrations so the result does not imply more coverage than the test actually provides.
The practical goal is to reason through this situation: Advance a debounce timer and verify one action after the final input. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Control the clock source
Timer callbacks and elapsed-time reads must use a compatible test clock.
Step 2: Advance relevant work
Scheduled callbacks and promise continuations may require distinct handling.
Step 3: Assert deadline behavior
Count elapsed duration rather than assuming each callback represents an exact interval.
Worked scenario
Advance a debounce timer and verify one action after the final input.
A debounce receives inputs at times zero and 50, with a 100-unit delay after the last input. Advancing to 149 should not fire; advancing to 150 should fire once for the latest value. A test must flush the supported asynchronous work before checking outcomes, using its test framework’s clock contract.
Common mistake
Advancing timers without flushing relevant async work can assert too early.
Verify the behavior
Test reset, cancellation and exact expiry; verify no stale action after unmount or disposal.
Interview exercise
Test a deadline accurately.
Answer and reasoning
Control the clock used by the implementation and distinguish elapsed-time logic from counting callbacks.
Continue learning
Compare the scenario with the Software Testing interview questions and test your understanding with the Software Testing MCQs. For terminology and implementation details, consult the reference material.