Unit Testing — closing coverage gaps
All packages enforce 100% coverage (branches, functions, lines, statements) via jest.config.js's coverageThreshold.global; any drop below fails the build. For AAA structure, mocking, placement, and type safety, follow .agents/rules/unit-testing.md — this skill only covers the coverage-completion workflow. For the general Nx-vs-direct command choice, see the root AGENTS.md.
Completing coverage for a specific file
- Run the target test file with coverage to get a baseline:
cd packages/<package-name> pnpm test:unit <test-file> - Find missing branches by inspecting
packages/<package-name>/coverage/lcov.infoforBRDAentries with zero hits. Map uncovered branches to exact conditions in the source. - Add tests to hit uncovered branches. Re-run after each change. Repeat until resolved or justified.
- Prefer adding tests over suppressing coverage.
- If uncovered logic appears obsolete, do not change production code — present evidence and request user confirmation first.
Handling unreachable or defensive branches
When a branch is genuinely unreachable at runtime and cannot be exercised through tests, mark it with an istanbul ignore comment that justifies why:
/* istanbul ignore next -- Defensive guard: invalid parser state is impossible through public API */
Rules for istanbul ignore:
- Always include a justification after the
--separator. - Prefer making code non-optional over ignoring it. If a default is never exercised, remove it instead of adding an ignore comment.
- Only use for genuinely unreachable code. If a branch can be triggered through a public API, write a test for it.
- Keep the ignore scope as narrow as possible. Place on the specific line or branch, not on entire functions.