Jest · Unit tests that survive refactors
Can you use Jest for performance testing?
Short answer
Jest is not a benchmark harness. Asserting on wall-clock durations is flaky in CI, so use coarse regression checks with generous margins and dedicated benchmarks for real measurements.
A timing assertion such as expecting an operation to finish in under 50 milliseconds measures the machine, the JIT, the worker pool and any concurrent test, not only the code. On a shared CI runner the same test can pass locally and fail in a pipeline for reasons unrelated to a regression.
test("processes a large batch within the budget", () => {
const rows = buildRows(10000);
const started = performance.now();
processBatch(rows);
const elapsed = performance.now() - started;
expect(elapsed).toBeLessThan(2000);
});What Jest is good for
- Catching an algorithmic regression with a very generous ceiling
- Asserting on call counts that indicate accidental repeated work
- Memory leak checks through open handles and repeated mount and unmount
- Guarding a bundle or snapshot size with a separate script
For real measurements, use a benchmark tool that warms up, runs many iterations and reports percentiles. For render performance, use the React profiler or a browser trace. A single timing assertion in Jest should be treated as a smoke alarm, not a performance budget.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why are timing assertions flaky in CI?
- 2What would you use instead of Jest for real benchmarks?
- 3How can a test catch an accidental quadratic algorithm?
- 4Why is a generous threshold better than a tight one?
Say it in the interview
Jest can catch egregious regressions with a generous timing ceiling, but it is not a benchmark tool. Wall-clock assertions depend on the machine and on parallel workers, so tight thresholds are flaky. For real numbers I use a dedicated benchmark with warmup and percentiles, or a profiler for rendering. In Jest I prefer asserting on call counts and complexity signals.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Jest mock interview and get scored feedback on depth, clarity and confidence.