Deep dive
Stability comes from determinism:
- Use fake timers or virtual clocks.
- Await all async work and flush queues.
- For concurrency, test invariants (no lost updates, no duplicates).
- Use stress tests to increase interleavings.
Examples
Async test pattern:
await doWork();
await flushJobs();
expect(state).toEqual(...)
Common pitfalls
- Using sleep instead of waiting for a condition.
- Not awaiting tasks (tests pass incorrectly).
- Relying on real time or network.
Interview follow-ups
- How do you test race conditions deterministically?
- When do you use stress tests vs unit tests?
- How do you test idempotency under concurrency?