test-coverage
Test Coverage Analysis & Enhancement
I'll analyze your test coverage, identify untested code paths, and suggest comprehensive tests for uncovered areas.
Arguments: $ARGUMENTS - specific paths or coverage focus areas
Phase 1: Coverage Tool Detection
Pre-Flight Checks: Before starting, I'll verify:
- Test framework and coverage tool availability
- Existing test configuration
- Coverage thresholds and requirements
- CI/CD coverage reporting setup
Coverage Tool Detection:
# Auto-detect coverage tooling
detect_coverage_tool() {
if [ -f "package.json" ]; then
# JavaScript/TypeScript ecosystem
if grep -q "jest" package.json; then
echo "jest --coverage"
elif grep -q "vitest" package.json; then
echo "vitest --coverage"
elif grep -q "c8" package.json; then
echo "c8"
elif grep -q "nyc" package.json || grep -q "istanbul" package.json; then
echo "nyc"
fi
elif [ -f "pyproject.toml" ] || [ -f "setup.py" ]; then
# Python ecosystem
if grep -q "pytest-cov" pyproject.toml setup.py requirements.txt 2>/dev/null; then
echo "pytest-cov"
elif command -v coverage >/dev/null 2>&1; then
echo "coverage"
fi
elif [ -f "go.mod" ]; then
# Go has built-in coverage
echo "go test -cover"
elif [ -f "Gemfile" ]; then
# Ruby ecosystem
if grep -q "simplecov" Gemfile; then
echo "simplecov"
fi
elif [ -f "pom.xml" ] || [ -f "build.gradle" ]; then
# Java ecosystem
echo "jacoco"
fi
}
COVERAGE_TOOL=$(detect_coverage_tool)
if [ -z "$COVERAGE_TOOL" ]; then
echo "No coverage tool detected. Suggestions:"
echo " - JavaScript: npm install --save-dev jest (includes coverage)"
echo " - Python: pip install pytest-cov"
echo " - Go: Built-in (go test -cover)"
echo " - Ruby: gem install simplecov"
exit 1
fi
echo "Detected coverage tool: $COVERAGE_TOOL"
Coverage Report Generation: I'll use bash to run coverage and parse reports efficiently:
# Generate coverage report with minimal output formats
case "$COVERAGE_TOOL" in
"jest --coverage")
# Only generate JSON summary (skip HTML report generation)
npm test -- --coverage --coverageReporters=json-summary 2>&1 | tail -20
;;
"pytest-cov")
# Only generate JSON report (skip HTML)
pytest --cov=. --cov-report=json --cov-report=term-missing:skip-covered 2>&1 | tail -20
;;
"go test -cover")
go test -coverprofile=coverage.out ./... 2>&1
go tool cover -func=coverage.out | tail -20
;;
esac
# Parse coverage summary (avoid reading thousands of lines of HTML)
if [ -f "coverage/coverage-summary.json" ]; then
# Jest/NYC format - extract just total metrics
jq -c '.total' coverage/coverage-summary.json
elif [ -f "coverage.json" ]; then
# pytest-cov format - extract just summary
jq -c '.totals' coverage.json
fi
This gets coverage metrics without reading thousands of lines of HTML reports, saving 90%+ tokens.
Phase 2: Coverage Metrics Analysis
I'll analyze multiple coverage dimensions:
-
Line Coverage
- Percentage of lines executed
- Identifies completely untested files
-
Branch Coverage
- Percentage of if/else, switch paths tested
- More meaningful than line coverage
-
Function Coverage
- Percentage of functions called
- Identifies unused code
-
Statement Coverage
- More granular than line coverage
- Multiple statements per line counted separately
Coverage Report Parsing:
# Extract low-coverage files for targeted analysis
parse_low_coverage_files() {
if [ -f "coverage/coverage-summary.json" ]; then
# Extract files with <80% coverage
jq -r 'to_entries[] |
select(.value.lines.pct < 80) |
"\(.key): \(.value.lines.pct)% lines, \(.value.branches.pct)% branches"' \
coverage/coverage-summary.json
fi
}
LOW_COVERAGE_FILES=$(parse_low_coverage_files)
Coverage Threshold Checking:
# Check if project has coverage requirements
check_coverage_thresholds() {
# Jest configuration
if [ -f "jest.config.js" ] || [ -f "jest.config.json" ]; then
grep -A 10 "coverageThreshold" jest.config.* 2>/dev/null
fi
# pytest configuration
if [ -f "pyproject.toml" ]; then
grep -A 5 "fail_under" pyproject.toml 2>/dev/null
fi
# Go coverage threshold (custom)
if [ -f ".coverage-threshold" ]; then
cat .coverage-threshold
fi
}
THRESHOLDS=$(check_coverage_thresholds)
Phase 3: Uncovered Code Identification
Strategic File Analysis:
I'll focus on files with coverage gaps using Grep before full Read:
# Find uncovered functions in low-coverage files (JavaScript/TypeScript)
Grep pattern="^(export )?(async )?function \w+"
glob="$LOW_COVERAGE_FILE_PATTERN"
output_mode="content"
head_limit=15
-n=true
# Find uncovered classes in low-coverage files (Python)
Grep pattern="^class \w+"
glob="$LOW_COVERAGE_FILE_PATTERN"
output_mode="content"
head_limit=15
-n=true
# Find error handling that might be uncovered
Grep pattern="throw new|raise |catch \(|except "
glob="$LOW_COVERAGE_FILE_PATTERN"
output_mode="content"
head_limit=10
Uncovered Code Patterns I'll Identify:
-
Error Handling Paths
try { await operation(); } catch (error) { // UNCOVERED: Error path not tested logger.error(error); throw new CustomError(error); } -
Edge Cases in Conditionals
if (value > 100) { return 'high'; } else if (value > 50) { return 'medium'; } else { return 'low'; // UNCOVERED: Never tested } -
Validation Logic
def validate_input(data): if not data: raise ValueError("Data required") # UNCOVERED if not isinstance(data, dict): raise TypeError("Dict required") # UNCOVERED return True -
Async Error Paths
async function fetchData(url) { if (!url) { throw new Error('URL required'); // UNCOVERED } const response = await fetch(url); if (!response.ok) { throw new Error('Fetch failed'); // UNCOVERED } return response.json(); } -
Default/Fallback Cases
switch (action.type) { case 'CREATE': return handleCreate(action); case 'UPDATE': return handleUpdate(action); default: return state; // UNCOVERED: Default case not tested }
Phase 4: Missing Test Identification
I'll identify critical gaps:
-
Untested Public APIs
# Find exported functions EXPORTED_FUNCTIONS=$(Grep pattern="^export (async )?function (\w+)" glob="src/**/*.{js,ts}" output_mode="content" head_limit=20) # Check which ones lack tests (bash comparison) echo "$EXPORTED_FUNCTIONS" | while read -r line; do FUNC_NAME=$(echo "$line" | grep -oP "function \K\w+") # Quick Grep check in test files HAS_TEST=$(Grep pattern="$FUNC_NAME" glob="**/*.{test,spec}.*" output_mode="files_with_matches" head_limit=1) [ -z "$HAS_TEST" ] && echo "UNTESTED: $FUNC_NAME" done -
Untested Error Scenarios
# Find error throwing code in low-coverage files Grep pattern="throw new|raise |throw Error" glob="$LOW_COVERAGE_FILE_PATTERN" output_mode="content" head_limit=15 -n=true -
Untested Edge Cases
- Empty array/object handling
- Null/undefined checks
- Boundary values (0, -1, MAX_INT)
- Invalid input types
-
Integration Points
- API endpoints
- Database operations
- External service calls
- File system operations
Phase 5: Test Suggestions & Generation
For each uncovered area, I'll suggest tests:
Example: Uncovered Error Path
// Uncovered code in src/user-service.js:
async function createUser(data) {
if (!data.email) {
throw new Error('Email required'); // UNCOVERED
}
return db.users.create(data);
}
// Suggested test:
describe('UserService.createUser', () => {
it('should throw error when email is missing', async () => {
await expect(createUser({ name: 'Test' }))
.rejects.toThrow('Email required');
});
it('should throw error when email is null', async () => {
await expect(createUser({ email: null, name: 'Test' }))
.rejects.toThrow('Email required');
});
it('should throw error when email is empty string', async () => {
await expect(createUser({ email: '', name: 'Test' }))
.rejects.toThrow('Email required');
});
});
Example: Uncovered Branch
# Uncovered code in src/calculator.py:
def divide(a, b):
if b == 0:
raise ValueError("Cannot divide by zero") # UNCOVERED
return a / b
# Suggested test:
def test_divide_by_zero():
"""Test division by zero raises ValueError"""
with pytest.raises(ValueError, match="Cannot divide by zero"):
divide(10, 0)
def test_divide_by_negative():
"""Test division by negative number"""
assert divide(10, -2) == -5.0
def test_divide_floats():
"""Test division with floating point precision"""
assert abs(divide(1, 3) - 0.333333) < 0.00001
Example: Uncovered Edge Case
// Uncovered code in src/array-utils.js:
function getFirst(arr) {
return arr[0]; // What if arr is empty? UNCOVERED
}
// Suggested tests:
describe('getFirst', () => {
it('should return first element of array', () => {
expect(getFirst([1, 2, 3])).toBe(1);
});
it('should return undefined for empty array', () => {
expect(getFirst([])).toBeUndefined();
});
it('should handle single-element array', () => {
expect(getFirst([42])).toBe(42);
});
it('should return first element even if falsy', () => {
expect(getFirst([0, 1, 2])).toBe(0);
expect(getFirst([null, 1, 2])).toBeNull();
});
});
Phase 6: Coverage Improvement Strategy
Prioritization Framework:
I'll prioritize test additions based on:
-
Risk Level (Critical ā Low)
- Critical: Security, data integrity, financial logic
- High: Core business logic, user-facing features
- Medium: Utility functions, helpers
- Low: Logging, formatting, trivial getters
-
Impact (High ā Low)
- High: Code used frequently, critical paths
- Medium: Occasional use, important features
- Low: Rarely used, edge features
-
Effort (Low ā High)
- Low: Simple unit tests, pure functions
- Medium: Mocked integration tests
- High: Complex integration/E2E tests
Coverage Improvement Plan:
COVERAGE IMPROVEMENT STRATEGY
==============================
Phase 1: Critical Uncovered Code (Priority: High Risk + High Impact)
āāā auth/login.js:45-52 - Error handling in authentication
āāā payment/process.js:78-85 - Failed payment scenarios
āāā user/validate.js:23-30 - Input validation edge cases
Phase 2: Important Features (Priority: High Impact + Low Effort)
āāā api/users.js:120-135 - User creation edge cases
āāā utils/format.js:45-60 - Edge cases in formatting
āāā services/email.js:89-95 - Email send failure handling
Phase 3: Code Quality (Priority: Medium Impact + Low Effort)
āāā helpers/string.js:12-25 - String utility edge cases
āāā models/user.js:67-72 - Model validation
āāā config/parser.js:34-40 - Config parsing errors
Phase 4: Comprehensive Coverage (Priority: Low Risk + Low Effort)
āāā utils/constants.js - Getter coverage
āāā types/validators.js - Type checking edge cases
āāā formatters/date.js - Date formatting edge cases
Phase 7: Test Generation & Implementation
I'll generate and add tests:
-
Create git checkpoint
git add -A git commit -m "Pre test-coverage-improvement checkpoint" || echo "No changes" -
Generate tests for uncovered code:
- Write comprehensive test cases
- Cover all branches and edge cases
- Include descriptive test names
- Add comments explaining edge cases
-
Verify coverage improvement:
# Run tests with coverage npm test -- --coverage # Compare before/after coverage echo "Coverage improved from X% to Y%" -
Validate test quality:
- Tests actually assert behavior
- No false positives (tests that pass without testing)
- Good test organization and naming
- Proper setup/teardown
Integration with Existing Skills
Workflow Integration:
- After
/testruns ā Check coverage with/test-coverage - After
/scaffoldā Ensure new code has tests - Before
/commitā Verify coverage thresholds met - During
/reviewā Include coverage analysis - With
/test-asyncā Cover async edge cases - With
/test-antipatternsā Quality + coverage together
Skill Suggestions:
- Low coverage in complex async ā
/test-async - Coverage but poor quality ā
/test-antipatterns - New feature needs tests ā
/tdd-red-green - Complex uncovered logic ā
/explain-like-senior
Reporting
I'll provide comprehensive coverage analysis:
TEST COVERAGE ANALYSIS REPORT
==============================
CURRENT COVERAGE:
āāā Lines: 78.5% (target: 80%)
āāā Branches: 65.2% (target: 75%)
āāā Functions: 82.1% (target: 85%)
āāā Statements: 77.8% (target: 80%)
FILES BELOW THRESHOLD:
āāā src/auth/login.js: 45.2% (critical!)
āāā src/payment/process.js: 52.8% (critical!)
āāā src/api/users.js: 71.3%
āāā src/utils/validate.js: 68.9%
āāā src/helpers/format.js: 74.5%
UNCOVERED CRITICAL CODE:
āāā Error handling paths: 23 instances
āāā Edge case branches: 18 instances
āāā Validation logic: 12 instances
āāā Async error paths: 8 instances
TESTS SUGGESTED:
āāā Authentication error scenarios: 8 tests
āāā Payment failure handling: 6 tests
āāā Input validation edge cases: 12 tests
āāā API error responses: 10 tests
āāā Utility function edge cases: 15 tests
AFTER IMPROVEMENTS:
āāā Lines: 78.5% ā 87.3% (+8.8%)
āāā Branches: 65.2% ā 81.7% (+16.5%)
āāā Functions: 82.1% ā 91.2% (+9.1%)
āāā Statements: 77.8% ā 86.9% (+9.1%)
TESTS ADDED: 51 new tests
FILES MODIFIED: 12 test files
EXECUTION TIME: +2.3s (acceptable)
NEXT STEPS:
āāā Run tests to verify all pass
āāā Review generated tests for quality
āāā Update CI/CD coverage thresholds
āāā Document testing coverage requirements
Coverage Tools Configuration
I can help set up coverage tools:
Jest (JavaScript/TypeScript):
{
"jest": {
"collectCoverage": true,
"coverageDirectory": "coverage",
"coverageReporters": ["text", "lcov", "json-summary"],
"coverageThreshold": {
"global": {
"lines": 80,
"branches": 75,
"functions": 85,
"statements": 80
}
},
"collectCoverageFrom": [
"src/**/*.{js,ts}",
"!src/**/*.d.ts",
"!src/**/*.test.{js,ts}"
]
}
}
pytest-cov (Python):
[tool.pytest.ini_options]
addopts = "--cov=src --cov-report=term-missing --cov-report=html --cov-fail-under=80"
[tool.coverage.run]
omit = ["*/tests/*", "*/test_*.py"]
[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
"def __repr__",
"raise NotImplementedError"
]
Safety Guarantees
What I'll NEVER do:
- Generate tests that don't actually test
- Lower coverage thresholds to "pass"
- Test implementation details instead of behavior
- Add AI attribution to commits or code
- Create tests that give false confidence
What I WILL do:
- Generate meaningful, behavior-focused tests
- Cover edge cases and error paths
- Improve actual code quality, not just metrics
- Maintain test maintainability
- Create clear commit messages (no AI attribution)
Credits
This skill is based on:
- Istanbul/nyc - JavaScript coverage tool standards
- Jest - Built-in coverage capabilities
- pytest-cov - Python coverage testing
- JaCoCo - Java code coverage library
- SimpleCov - Ruby coverage analysis
- Testing Best Practices - Community-driven coverage guidelines
Token Optimization
Current Budget: 3,000-5,000 tokens (unoptimized) Optimized Budget: 1,200-2,000 tokens (60% reduction)
This skill implements aggressive token optimization while maintaining comprehensive coverage analysis through strategic tool usage and intelligent reporting.
Optimization Patterns Applied
1. Bash-Based Coverage Tool Execution (90% savings)
# PATTERN: Use direct bash/tool commands, parse JSON, avoid HTML reports
# Savings: 90% vs reading HTML coverage reports
# Generate coverage with minimal output
case "$COVERAGE_TOOL" in
"jest --coverage")
# Only JSON summary, skip HTML (saves 4,000+ tokens)
npm test -- --coverage --silent \
--coverageReporters=json-summary \
--no-coverage-reporters=html 2>&1 | tail -20
;;
"pytest-cov")
# Only JSON, skip HTML and terminal report
pytest --cov=. --cov-report=json \
--cov-report=term-missing:skip-covered -q 2>&1 | tail -20
;;
esac
# Parse JSON summary directly (50 tokens vs 5,000+ for HTML)
COVERAGE_SUMMARY=$(jq -c '.total' coverage/coverage-summary.json 2>/dev/null)
# Extract just the metrics we need
LINE_PCT=$(echo "$COVERAGE_SUMMARY" | jq -r '.lines.pct')
BRANCH_PCT=$(echo "$COVERAGE_SUMMARY" | jq -r '.branches.pct')
FUNC_PCT=$(echo "$COVERAGE_SUMMARY" | jq -r '.functions.pct')
echo "Coverage: ${LINE_PCT}% lines, ${BRANCH_PCT}% branches"
2. Coverage Tool Detection Caching (saves 500 tokens per run)
# Cache coverage tool detection
CACHE_FILE=".claude/cache/test-coverage/tool.json"
if [ -f "$CACHE_FILE" ]; then
COVERAGE_TOOL=$(cat "$CACHE_FILE" | jq -r '.tool')
COVERAGE_COMMAND=$(cat "$CACHE_FILE" | jq -r '.command')
echo "ā Using cached coverage tool: $COVERAGE_TOOL"
else
# Detect tool (first run only)
if grep -q "jest" package.json 2>/dev/null; then
COVERAGE_TOOL="jest"
COVERAGE_COMMAND="npm test -- --coverage --silent --coverageReporters=json-summary"
elif grep -q "vitest" package.json 2>/dev/null; then
COVERAGE_TOOL="vitest"
COVERAGE_COMMAND="vitest --coverage --reporter=json"
elif grep -q "pytest-cov" pyproject.toml requirements.txt 2>/dev/null; then
COVERAGE_TOOL="pytest"
COVERAGE_COMMAND="pytest --cov=. --cov-report=json -q"
fi
# Cache result
mkdir -p .claude/cache/test-coverage
cat > "$CACHE_FILE" <<EOF
{
"tool": "$COVERAGE_TOOL",
"command": "$COVERAGE_COMMAND",
"timestamp": "$(date -Iseconds)"
}
EOF
fi
3. Early Exit (85% savings when coverage is good)
# PATTERN: Quick validation before deep analysis
# Phase 1: Check if coverage exists (100 tokens)
if [ ! -f "coverage/coverage-summary.json" ] && [ ! -f "coverage.json" ]; then
echo "Running coverage analysis..."
$COVERAGE_COMMAND
fi
# Phase 2: Quick coverage check (300 tokens)
LINE_COVERAGE=$(jq -r '.total.lines.pct' coverage/coverage-summary.json 2>/dev/null)
BRANCH_COVERAGE=$(jq -r '.total.branches.pct' coverage/coverage-summary.json 2>/dev/null)
# Check against thresholds
THRESHOLD=${THRESHOLD:-80}
if [ "$(echo "$LINE_COVERAGE >= $THRESHOLD" | bc)" -eq 1 ] && \
[ "$(echo "$BRANCH_COVERAGE >= 75" | bc)" -eq 1 ]; then
echo "ā Coverage meets thresholds:"
echo " Lines: ${LINE_COVERAGE}% (target: ${THRESHOLD}%)"
echo " Branches: ${BRANCH_COVERAGE}% (target: 75%)"
exit 0 # Early exit: 400 tokens total (saves 2,500+)
fi
# Phase 3: Deep gap analysis (2,000+ tokens)
# Continue with uncovered code analysis...
4. Progressive Disclosure (75% savings on reporting)
# PATTERN: Tiered reporting based on severity
# Parse verbosity from arguments
VERBOSE=$(echo "$ARGUMENTS" | grep -q "\-\-verbose" && echo "true" || echo "false")
GENERATE=$(echo "$ARGUMENTS" | grep -q "\-\-generate" && echo "true" || echo "false")
# Extract low-coverage files (below threshold)
LOW_COV_FILES=$(jq -r "to_entries[] |
select(.value.lines.pct < $THRESHOLD) |
\"\(.key): \(.value.lines.pct)%\"" \
coverage/coverage-summary.json | head -10)
LOW_COUNT=$(echo "$LOW_COV_FILES" | wc -l)
# Level 1 (Default): Critical gaps only
if [ "$VERBOSE" != "true" ]; then
echo "COVERAGE ANALYSIS:"
echo "āāā Lines: ${LINE_COVERAGE}% (target: ${THRESHOLD}%)"
echo "āāā Branches: ${BRANCH_COVERAGE}% (target: 75%)"
echo "āāā Files below threshold: $LOW_COUNT"
echo ""
echo "Critical gaps (top 5):"
echo "$LOW_COV_FILES" | head -5
echo ""
echo "ā¹ Run with --verbose to see all gaps"
echo "ā¹ Run with --generate to create tests"
# Output: ~600 tokens vs 3,000 tokens for full analysis
exit 0
fi
# Level 2 (--verbose): Detailed gap analysis
if [ "$GENERATE" != "true" ]; then
echo "DETAILED COVERAGE ANALYSIS:"
echo ""
echo "All files below threshold ($LOW_COUNT files):"
echo "$LOW_COV_FILES"
echo ""
echo "Uncovered code patterns:"
# Show patterns without generating tests
echo " - Error handling: X instances"
echo " - Edge cases: Y instances"
echo " - Validation: Z instances"
echo ""
echo "Run with --generate to create suggested tests"
# Output: ~1,500 tokens
exit 0
fi
# Level 3 (--verbose --generate): Full test generation
# Generate comprehensive test suggestions (3,000+ tokens)
5. Focus Areas / Scope Limiting (80% savings)
# PATTERN: Default to changed files, allow full coverage analysis
# Parse arguments
FOCUS_PATH="${ARGUMENTS%% *}"
FULL_COVERAGE=$(echo "$ARGUMENTS" | grep -q "\-\-full" && echo "true" || echo "false")
CRITICAL_ONLY=$(echo "$ARGUMENTS" | grep -q "\-\-critical" && echo "true" || echo "false")
if [ "$FULL_COVERAGE" != "true" ]; then
if [ -n "$FOCUS_PATH" ] && [ -d "$FOCUS_PATH" ]; then
# Specific path provided
echo "š Coverage analysis for: $FOCUS_PATH"
COVERAGE_FILTER="$FOCUS_PATH"
else
# Default: Git diff (changed files only)
CHANGED_FILES=$(git diff --name-only HEAD | \
grep -v "\.test\." | \
grep -E "\.(js|ts|py|go)$" || echo "")
if [ -n "$CHANGED_FILES" ]; then
FILE_COUNT=$(echo "$CHANGED_FILES" | wc -l)
echo "š Coverage analysis for changed files only ($FILE_COUNT files)"
echo " Use --full flag for complete coverage analysis"
# Filter coverage report to changed files only
LOW_COV_FILES=$(echo "$LOW_COV_FILES" | \
grep -F "$(echo "$CHANGED_FILES" | tr '\n' '|')")
if [ -z "$LOW_COV_FILES" ]; then
echo "ā All changed files have adequate coverage"
exit 0 # Early exit: no work needed
fi
else
echo "ā No changed source files detected"
exit 0
fi
fi
else
echo "š Full project coverage analysis"
fi
# Critical path filtering
if [ "$CRITICAL_ONLY" = "true" ]; then
echo " Filtering for critical paths only (auth, payment, security)"
LOW_COV_FILES=$(echo "$LOW_COV_FILES" | \
grep -E "auth|payment|security|login|billing")
fi
# Token savings:
# - Changed files only: ~1,000 tokens (5-10 files)
# - Critical paths only: ~800 tokens (focused analysis)
# - Specific path: ~1,200 tokens (directory focus)
# - Full coverage: ~5,000 tokens (entire project)
# Average savings: 80% (most users analyze changes only)
6. Low-Coverage File Filtering (85% savings)
# PATTERN: Only analyze files below coverage threshold
# Extract files with coverage < threshold from JSON
LOW_COV_FILES=$(jq -r "to_entries[] |
select(.value.lines.pct < $THRESHOLD) |
.key" \
coverage/coverage-summary.json)
LOW_COV_COUNT=$(echo "$LOW_COV_FILES" | wc -l)
if [ "$LOW_COV_COUNT" -eq 0 ]; then
echo "ā All files meet coverage threshold (${THRESHOLD}%)"
exit 0 # Early exit: no files to analyze
fi
# Only read files that need attention
echo "Analyzing $LOW_COV_COUNT low-coverage files..."
# Convert to glob pattern for Grep
LOW_COV_PATTERN=$(echo "$LOW_COV_FILES" | head -10 | \
sed 's/^/{/' | sed 's/$/,/' | tr '\n' ' ' | \
sed 's/,$/}/')
# Now use Grep only on low-coverage files
Grep pattern="throw new|raise "
glob="$LOW_COV_PATTERN"
output_mode="content"
head_limit=15
# Savings: 85% by skipping well-covered files
7. JSON Parsing (95% savings vs HTML)
# PATTERN: Parse JSON coverage reports, never read HTML
# Bad: Read HTML coverage report (5,000+ tokens)
# Read coverage/index.html
# Parse with complex regex...
# Good: Parse JSON summary (200 tokens)
COVERAGE_DATA=$(cat coverage/coverage-summary.json)
# Extract exactly what we need with jq
TOTAL_COVERAGE=$(echo "$COVERAGE_DATA" | jq -c '.total')
LOW_FILES=$(echo "$COVERAGE_DATA" | \
jq -r "to_entries[] |
select(.value.lines.pct < 80) |
\"\(.key): \(.value.lines.pct)%\"")
# Just the metrics, no HTML parsing overhead
# Savings: 95% (200 tokens vs 5,000+)
8. Grep-Before-Read for Uncovered Patterns (90% savings)
# PATTERN: Use Grep to find uncovered code patterns in low-coverage files
# Only search in files with low coverage (already filtered)
# Find error handling (likely uncovered)
ERROR_PATTERNS=$(Grep pattern="throw new|raise |catch \(|except "
glob="$LOW_COV_PATTERN"
output_mode="content"
head_limit=10
-n=true)
# Find validation logic (often uncovered)
VALIDATION_PATTERNS=$(Grep pattern="if \(!|if \w+ === null|if \w+ === undefined"
glob="$LOW_COV_PATTERN"
output_mode="content"
head_limit=10)
# Find edge case branches (commonly missed)
EDGE_CASES=$(Grep pattern="else if|elif |default:|otherwise"
glob="$LOW_COV_PATTERN"
output_mode="content"
head_limit=10)
# Only read files if we need detailed context for test generation
# Most of the time, patterns are enough for suggestions
# Savings: 90% by avoiding full file reads
Token Budget Breakdown
Optimized Execution Flow:
Phase 1: Coverage Metrics Check (400 tokens)
āā Tool detection from cache (50 tokens)
āā Run coverage tool (100 tokens)
āā Parse JSON summary (100 tokens)
āā Check against thresholds (150 tokens)
ā Total: 400 tokens (75% of runs exit here - coverage is good)
Phase 2: Low-Coverage File Identification (800 tokens)
āā Extract files below threshold (200 tokens)
āā Filter to changed files (200 tokens)
āā Critical path filtering (100 tokens)
āā Report top gaps (300 tokens)
ā Total: 1,200 tokens (15% of runs exit here)
Phase 3: Deep Gap Analysis (1,800 tokens)
āā Grep for uncovered patterns (600 tokens)
āā Suggest tests (800 tokens)
āā Generate improvement plan (400 tokens)
ā Total: 3,000 tokens (10% of runs need deep analysis)
Phase 4: Test Generation (only with --generate)
āā Read low-coverage files (1,000 tokens)
āā Generate comprehensive tests (1,500 tokens)
āā Validate and report (500 tokens)
ā Total: 6,000 tokens (rare, only when explicitly requested)
Average: (0.75 Ć 400) + (0.15 Ć 1,200) + (0.10 Ć 3,000) = 780 tokens
Worst case (no --generate): 3,000 tokens
Full generation: 6,000 tokens (explicit opt-in)
Comparison:
| Scenario | Unoptimized | Optimized | Savings |
|---|---|---|---|
| Coverage above threshold | 4,000 | 400 | 90% |
| Changed files, minor gaps | 4,500 | 1,200 | 73% |
| Critical path focus | 5,000 | 800 | 84% |
| Deep gap analysis | 5,000 | 3,000 | 40% |
| Full test generation | 8,000 | 6,000 | 25% |
| Average | 5,000 | 2,000 | 60% |
Cache Strategy
Cache Location: .claude/cache/test-coverage/
Cached Data:
{
"tool": "jest|vitest|pytest-cov|go-test|simplecov",
"command": "npm test -- --coverage --coverageReporters=json-summary",
"thresholds": {
"lines": 80,
"branches": 75,
"functions": 85,
"statements": 80
},
"timestamp": "2026-01-27T10:30:00Z",
"last_coverage": {
"lines": 78.5,
"branches": 65.2,
"functions": 82.1,
"statements": 77.8,
"low_coverage_files": 12,
"file_checksums": {
"src/auth.js": "abc123...",
"src/payment.js": "def456..."
}
}
}
Cache Invalidation:
- Time-based: 30 minutes for coverage tool detection
- Source file changes: Re-run coverage if files modified
- Manual:
--no-cacheflag to force fresh coverage run
Cache Benefits:
- Tool detection: 500 token savings (99% cache hit rate)
- Previous coverage: 2,000 token savings (when source unchanged)
- Overall: 70% savings on repeated runs
Real-World Token Usage
Scenario 1: Daily TDD workflow (most common)
# Developer adds tests, runs /test-coverage
Result:
- Tool: cached (50 tokens)
- Run coverage (100 tokens)
- Coverage: 85% lines, 78% branches (100 tokens)
- Check thresholds: PASS (100 tokens)
- Early exit with ā message (50 tokens)
Total: ~400 tokens (92% savings vs 5,000 unoptimized)
Scenario 2: Pre-commit check with gaps
# Developer checks coverage before committing
Result:
- Tool: cached (50 tokens)
- Coverage: 76% lines (below 80% threshold) (100 tokens)
- Changed files: 3 files analyzed (500 tokens)
- Found 2 uncovered error paths (300 tokens)
- Test suggestions for 2 gaps (400 tokens)
Total: ~1,350 tokens (73% savings vs 5,000 unoptimized)
Scenario 3: Critical path audit
# Team lead checks critical auth/payment coverage
Result:
- Tool: cached (50 tokens)
- Full coverage run (200 tokens)
- Filter: --critical flag (100 tokens)
- Found 3 critical files with gaps (400 tokens)
- Detailed uncovered patterns (500 tokens)
Total: ~1,250 tokens (75% savings vs 5,000 unoptimized)
Scenario 4: New feature comprehensive coverage
# Developer wants full test generation
Result:
- Tool: cached (50 tokens)
- Coverage for new feature path (300 tokens)
- Deep pattern analysis (800 tokens)
- Generate tests: --generate flag (2,500 tokens)
- 12 new tests created (1,000 tokens)
Total: ~4,650 tokens (7% savings but complete test suite)
Performance Improvements
Benefits of Optimization:
- Instant Feedback: 400 tokens when coverage is good (most common)
- Lower Costs: 60% average token reduction = 60% cost savings
- Focused Analysis: Only analyze files that need attention
- Scalability: JSON parsing works on any project size
- Smart Defaults: Changed files only, expandable to full project
Quality Maintained:
- ā Zero functionality regression
- ā All coverage metrics still reported
- ā Uncovered code patterns still detected
- ā Test suggestions remain comprehensive
- ā Progressive disclosure improves UX
Additional Optimizations:
- Shared cache with
/testand/test-antipatternsskills - Incremental coverage (only re-run for changed files)
- Parallel test generation (multiple files at once)
- Smart threshold detection from project config
This ensures thorough coverage analysis and meaningful test generation while delivering fast, cost-effective results with actionable insights.