Jest · Unit tests that survive refactors
How do coverage thresholds work and how should you set them?
Short answer
coverageThreshold sets minimum percentages; the run fails when a metric drops below them. Set a realistic global floor and ratchet it upward instead of demanding 100 percent everywhere.
A threshold turns coverage from a report into a gate. Jest compares the computed percentages to the configured values and exits non-zero when any metric falls short. Used well, it prevents coverage from drifting down. Used badly, it encourages tests that execute code without checking anything.
export default {
collectCoverage: true,
coverageThreshold: {
global: {
statements: 80,
branches: 70,
functions: 80,
lines: 80,
},
},
};Setting a sane floor
- Measure the current number first, then set the floor slightly below it
- Ratchet the threshold up as tests are added
- Use a per-directory override for legacy code, not a lower global number
- Keep the business-critical modules above the global floor
- Review threshold changes in code review like any other quality gate
Per-file thresholds can be counterproductive. A tiny utility with a single defensive branch may be perfectly tested at 80 percent while a payments module needs closer to full branch coverage. Express that priority through targeted directory thresholds and explicit tests, not through one number for everything.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why is a per-file threshold often worse than a global one?
- 2How do you raise coverage on a legacy codebase without blocking everyone?
- 3What does branch coverage add over line coverage?
- 4How can a team game a coverage threshold?
Say it in the interview
coverageThreshold fails the run when coverage drops below configured percentages. I set the floor just below the current level and ratchet it up, rather than starting at 100 and blocking all work. For legacy areas I use a directory override instead of lowering the global bar. The threshold stops drift, but it does not judge assertion quality, so I still review whether the tests would catch a regression.
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.