---
name: test-coverage-gaps
description: Finds risky untested code by running coverage and ranking gaps by risk such as money, auth, data loss and parsing rather than by percentage, then proposes the few tests that matter most. Use when asked where tests are missing or to improve coverage.
---

# Test Coverage Gaps

A coverage percentage says how much code ran, not how well it is protected. Your goal is to find the untested code that would hurt most if it broke, and propose a short list of tests for it.

## Workflow

### 1. Get a coverage report

Find how the project already measures coverage and use that first. Typical commands:

```sh
# JavaScript / TypeScript
npx vitest run --coverage            # or: npx jest --coverage
# Python
pytest --cov=src --cov-report=term-missing --cov-branch
# Go
go test ./... -coverprofile=cover.out && go tool cover -func=cover.out
# Java (Maven + JaCoCo)
mvn test jacoco:report
```

- If no coverage tool is configured, ask before adding a dependency; Go has it built in, Python and Node need a package.
- Enable branch coverage if available: a line can run while one side of its `if` never does.
- Run the whole suite and note failing tests first. Coverage from a broken suite is misleading.
- Write coverage output files (`coverage/`, `.coverage`, `cover.out`) outside git; check `.gitignore`.

### 2. Collect the gaps

List files and functions with uncovered lines or branches. Focus on the per-line and per-branch detail (`term-missing`, `lcov`, `go tool cover -html`), not the total.

Ignore code that is not worth testing: generated code, trivial getters, pure configuration, type definitions, thin wrappers around a framework, dead code (report dead code separately for deletion).

### 3. Rank by risk, not by size

For each gap, score the damage if it were wrong, and how likely it is to change or break:

| Risk area | Examples | Weight |
| --- | --- | --- |
| Money | prices, discounts, taxes, refunds, currency rounding, billing | very high |
| Auth and permissions | login, token checks, role checks, ownership checks | very high |
| Data loss or corruption | deletes, migrations, overwrites, transactions, sync | very high |
| Parsing and validation | user input, file formats, dates, encodings, webhooks | high |
| External contracts | API handlers, serialization, retries, idempotency | high |
| Concurrency and state | locks, queues, caching, scheduling | medium to high |
| Error paths | catch blocks, timeouts, fallbacks | medium |
| Presentation, glue | formatting, UI wiring, logging | low |

Raise the score for code that changes often (see the command below) and for code that has caused bugs before (search the history for "fix").

```sh
git log --since=6.months --name-only --pretty=format: -- src | sort | uniq -c | sort -rn | head -20
```

### 4. Propose the tests that matter

Pick the top 3 to 7 gaps. For each, write:

- Where: file and function, and the uncovered lines or branches.
- Why it matters: the concrete failure (for example "a negative quantity yields a negative total").
- Test: the cases to write in one sentence each, and the level (unit, integration, E2E).
- Cost: easy, needs a fake, needs a database or service.

Prefer a few strong behavior tests over many shallow ones. A test that executes a line without asserting anything inflates coverage and protects nothing.

### 5. Optionally write them

If the user asked you to write the tests, do the top items, following the project's test style, and run coverage again. Report the before and after for the files you touched. Do not chase a global threshold by padding tests.

## Report format

- Overall numbers (lines and branches) and the command used, in one line.
- Ranked table: risk, location, why, proposed test, cost.
- Code judged not worth testing, and any dead code found.
- Suspected bugs spotted while reading the uncovered code.
- Tests written, if any, with the new numbers.

## Pitfalls

- Treating 100 percent as the goal. Chasing it produces tests of getters and mocks.
- Trusting coverage from tests that assert nothing; scan a few covered but critical functions for real assertions.
- Missing branches: a covered `if` with no test for the `else`.
- Reading the percentage of the whole repository; look at the risky modules.
- Adding a coverage gate or changing CI thresholds without asking the user.
- Writing tests that only lock in current buggy behavior. If the code looks wrong, report it instead.

Source: [AnyModel skills catalog](https://anymodel.org/en/skills) · License: [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)
