Originally published at claudeguide.io/claude-code-test-coverage-improvement
Improve Test Coverage with Claude Code
To improve test coverage with Claude Code, paste your coverage report directly into Claude and ask it to generate tests for every uncovered line and branch. Claude reads Jest, Vitest, and pytest --cov output, identifies exactly which code paths are untested, and writes targeted tests — including edge cases, mocks, and parameterized variants — without you needing to identify the gaps manually. A TypeScript project can move from 45% to 92% coverage in a single focused session.
Analyzing Coverage Reports
Before generating tests, get a machine-readable coverage report.
Jest / Vitest:
npx jest --coverage --coverageReporters=text-summary
# or for detailed per-file output:
npx jest --coverage --coverageReporters=lcov --coverageReporters=text
pytest:
pytest --cov=src --cov-report=term-missing --cov-report=json
The term-missing flag prints the exact uncovered line numbers — the format Claude understands best.
Prompt: feed the report to Claude Code
Here is my Jest coverage report:
[paste coverage output]
List every file below 80% branch coverage, show the uncovered branch ranges,
and generate a test file for each. Use the existing test style from
`src/__tests__/auth.test.ts` as the template.
Claude parses the coverage table, cross-references source files, and produces tests matched to your existing patterns. For a guide on the underlying test generation workflow, see Claude Code test generation.
Targeted Test Generation for Uncovered Branches
Coverage tools report line coverage and branch coverage separately. Branch coverage is harder to hit — every if, ternary, &&, and || creates two branches.
Prompt for branch-specific tests:
This function has 3 uncovered branches according to Istanbul:
- line 24: the `if (user.role === 'admin')` false branch
- line 31: the catch block
- line 45: the early return when `items.length === 0`
Generate Jest tests that trigger each uncovered branch.
Source file:
[paste the function]
Claude produces one describe block per uncovered branch, with assertions that verify the exact return value or side effect of each path.
Edge-Case Testing Patterns
High coverage numbers do not guarantee quality unless edge cases are covered. Use these prompts after hitting line/branch targets.
Boundary value prompt:
For the `calculateDiscount(price, quantity)` function, generate tests covering:
- price = 0, price = -1, price = Number.MAX_SAFE_INTEGER
- quantity = 0, quantity = 1, quantity = 1000
- all combinations where the result should round to zero
Null / undefined / type coercion prompt:
Generate tests for `parseUserInput(input)` that cover:
- null, undefined, empty string, whitespace-only string
- non-string types (number, object, array)
- strings with special characters: <,
[→ Get Claude Code Power Prompts 300 — $29](https://shoutfirst.gumroad.com/l/agfda?utm_source=claudeguide&utm_medium=article&utm_campaign=claude-code-test-coverage-improvement)
*300 prompts. Instant download. 30-day money-back guarantee.*
---
## Snapshot Tests and Parameterized Tests
**Snapshot tests** catch unintended UI or serialization regressions.
Generate snapshot tests for the UserCard, OrderSummary, and InvoicePDF
React components. Each snapshot test should render the component
with realistic props pulled from our Faker fixtures in src/tests/fixtures/.
To update snapshots after intentional changes:
My snapshots are failing after the design update to UserCard.
Review the diff below, confirm the changes are intentional,
then update only the UserCard snapshot. Leave all others untouched.
[paste snapshot diff]
**Parameterized tests** reduce duplication when the same logic applies to many inputs.
Convert these 8 nearly-identical test cases for validateEmail()
into a single describe.each / it.each block.
[paste the 8 test cases]
Jest example output:
typescript
describe.each([
["valid@example.com", true],
["missing-at-sign.com", false],
["@no-local-part.com", false],
["spaces in@address.com", false],
["valid+tag@sub.domain.io", true],
])("validateEmail('%s')", (input, expected) =
→ Get Claude Code Power Prompts 300 — $29
Instant download. 30-day money-back guarantee.
Top comments (0)