Test Coverage Expander
Expand unit test coverage by targeting untested branches and edge cases.
Repo Sync Before Edits (mandatory)
Before creating/updating/deleting files in an existing repository, sync the current branch with remote:
branch="$(git rev-parse --abbrev-ref HEAD)"
git fetch origin
git pull --rebase origin "$branch"
If the working tree is not clean, stash first, sync, then restore:
git stash push -u -m "pre-sync"
branch="$(git rev-parse --abbrev-ref HEAD)"
git fetch origin && git pull --rebase origin "$branch"
git stash pop
If origin is missing, pull is unavailable, or rebase/stash conflicts occur, stop and ask the user before continuing.
Workflow
0. Create Feature Branch
Before making any changes:
- Check the current branch - if already on a feature branch for this task, skip
- Check the repo for branch naming conventions (e.g.,
feat/,feature/, etc.) - Create and switch to a new branch following the repo's convention, or fallback to:
feat/test-coverage
1. Analyze Coverage
Detect the project's test runner and run the coverage report:
- JavaScript/TypeScript:
npx jest --coverageornpx vitest --coverage - Python:
pytest --cov=. --cov-report=term-missing - Go:
go test -coverprofile=coverage.out ./... - Rust:
cargo tarpaulinorcargo llvm-cov
From the report, identify:
- Untested branches and code paths
- Low-coverage files/functions (prioritize files below 60%)
- Missing error handling tests
2. Identify Test Gaps
Review code for:
- Logical branches (if/else, switch)
- Error paths and exceptions
- Boundary values (min, max, zero, empty, null)
- Edge cases and corner cases
- State transitions and side effects
3. Write Tests
Use project's testing framework:
- JavaScript/TypeScript: Jest, Vitest, Mocha
- Python: pytest, unittest
- Go: testing, testify
- Rust: built-in test framework
Target scenarios:
- Error handling and exceptions
- Boundary conditions
- Null/undefined/empty inputs
- Concurrent/async edge cases
4. Verify Improvement
Run coverage again and confirm measurable increase. Report:
- Before/after coverage percentages
- Number of new test cases added
- Files with the biggest coverage gains
Error Handling
No test framework detected
Solution: Check package.json, pyproject.toml, Cargo.toml, or go.mod for test dependencies. If none found, ask the user which framework to use and install it.
Coverage tool not installed
Solution: Install the appropriate coverage tool (nyc, pytest-cov, etc.) and retry.
Existing tests failing
Solution: Do not add new tests until existing failures are resolved. Report failing tests to the user first.
Guidelines
- Follow existing test patterns and naming conventions
- Place test files alongside source or in the project's existing test directory
- Group related test cases logically
- Use descriptive test names that explain the scenario
- Do not mock what you do not own — prefer integration tests for external boundaries