All jobs / Frontend / web developer / Testing
Frontend / web developer interview question · 1–3 years
How do you test a search box that debounces input by 300 ms, in Jest or Vitest, without slowing the suite down?
The strongest answer
Switch on fake timers, type, advance the clock by 300 ms, assert one call with the last term, then restore real timers afterwards so other tests are not affected
Why it works
Exactly — the test controls time, runs in milliseconds, and can also assert that nothing fired at 299 ms.
What a good answer covers
- Control the clock, don’t wait for it.
- Enable fake timers.
- Advance time, then assert.
- Restore real timers after each test.
Answers that fall short
- Close, but not the strongest: Use fake timers and advance the clock, and leave them switched on for the rest of the file
The technique is right; leaked fake timers make later tests hang or pass for the wrong reason. - Weak: Wait 300 ms of real time in the test with a sleep helper before asserting
Real sleeps add up across a suite and still fail on a busy CI machine. - Red flag: Remove the debounce when running under test with an environment check in the component
Test-only branches mean production code is never tested, and the flag itself becomes a bug source.
Who gets asked this
More Testing questions
- A test passes when run alone and fails when the whole file runs, and the failing test changes when you…
- A Playwright or Cypress suite fails about one run in five on CI, in different places. The tests are full of…
- After a bad release, management asks you to enforce 90% code coverage on the frontend repository as a merge…
- How do you catch layout breakage before you translate into German and Hebrew?
- What is the difference between a unit test, a component test and an end-to-end test on the frontend, and…
- How do you assert a login button in React Testing Library?