vitest coverage thresholds not met after refactor
Handles Vitest coverage threshold failures after refactors: real gaps vs stale thresholds. Use when coverage gates fail post-refactor. Not for setting up coverage initially.
TL;DR
A refactor changed the code-to-test ratio, and the threshold is now wrong or a real gap appeared. Check which lines are uncovered: if they are dead code, delete them; if they are real logic, add tests; only then adjust the threshold.
Error
ERROR: Coverage thresholds not met: lines 82% (threshold 90%), branches 75% (threshold 85%)Steps
- Open the HTML coverage report and find the uncovered lines. Expected: a concrete list, not a percentage.
- If the uncovered code is dead (leftover from the refactor), delete it. Expected: coverage rises without new tests.
- If it is real logic, write tests for it before touching the threshold. Expected: the gap is genuinely covered.
- Only if the threshold itself is stale (for example it counted generated code), update it in config with a comment explaining why. Expected: a justified number, not a lowered bar.
- Re-run coverage. Expected: green with the code honestly covered.
When to use
- Coverage gates fail after a refactor.
- You need to decide between more tests and a threshold change.
When not to use
- First-time coverage setup (different task).
- You want to lower thresholds to make CI green (that deletes the gate's value).
Tool compatibility
- Vitest 0.30 through latest; v8 or istanbul provider.
Variant phrasings
Coverage threshold failed in Vitest
The general failure; inspect the report before acting.
Lines below threshold after refactor
Usually dead code from the refactor; delete first.
Why it happens
Refactors move, delete, and add code. Thresholds are absolute numbers against a moving codebase, so they drift out of sync.
Edge cases
- Generated code (GraphQL types, mocks) should be excluded from coverage, not covered.
- Branch coverage is stricter than line coverage; a refactor adding branches needs branch tests.
- 100% thresholds make every refactor a coverage chore; 80-90% with review is saner.
Provenance
Resolved from the public thread: https://vectle.com/posts/pstViKlNvxBWqSbikm6dclWg
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.