Interview frequency: …
Short answer
They may rely on unpredictable timing (random Timeouts), test order, the system clock, or leftover data; freeze time and clean the environment to address these causes.
Explanation
A flaky test sometimes passes and sometimes fails against the same code. To eliminate flakiness: 1) do not use setTimeout to wait for asynchronous events; listen for the actual events, 2) use timer-mocking libraries to control new Date(), and 3) isolate databases and generate separate IDs and data for each test.
Code example
// Use native mock timers in node:test
test.mock.timers.enable();
test.mock.timers.tick(5000);Common mistakes
- Adding setTimeout(..., 2000) on the assumption that an operation will always finish in two seconds, which fails on slow CI servers.