This document describes the test coverage enforcement system for the Stellar Micro-Donation API.
Test coverage thresholds are automatically enforced on every pull request to prevent code quality regression.
Minimum coverage required:
- Branches: 30%
- Functions: 30%
- Lines: 30%
- Statements: 30%
Run coverage locally:
npm run test:coverageThis generates:
- Console output with coverage summary
coverage/directory with detailed HTML reportcoverage/lcov.infofor CI integration
The coverage workflow (.github/workflows/coverage.yml) runs on:
- Pull requests to
mainordevelop - Pushes to
mainordevelop
Process:
- Checkout code
- Install dependencies
- Run tests with coverage
- Jest automatically fails if thresholds not met
- Upload coverage report as artifact
If coverage drops below thresholds:
- ❌ CI check fails
- PR cannot be merged
- Clear error message shows which metrics failed
- Coverage report available as artifact
Coverage settings in jest.config.js:
coverageThreshold: {
global: {
branches: 30,
functions: 30,
lines: 30,
statements: 30,
},
}Files included in coverage:
collectCoverageFrom: [
'src/**/*.js',
'!src/scripts/**',
'!src/config/**',
]After running npm run test:coverage:
open coverage/lcov-report/index.html- Go to failed workflow run
- Download "coverage-report" artifact
- Extract and open
lcov-report/index.html
npm run test:coverage
# Review console output for uncovered linesPriority for coverage improvement:
- Core business logic (services, models)
- API routes and middleware
- Utility functions
- Error handling paths
Add tests in tests/ directory:
- Unit tests for individual functions
- Integration tests for API endpoints
- Mock external dependencies (Stellar network)
- Don't lower thresholds - Maintain or increase coverage over time
- Test meaningful code - Focus on business logic, not boilerplate
- Use mocks appropriately - Mock external services, not internal logic
- Review coverage reports - Identify untested edge cases
- Add tests with new features - Maintain coverage as code grows
Not included in coverage:
src/scripts/**- Database initialization scriptssrc/config/**- Configuration filestests/**- Test files themselvesnode_modules/- Dependencies
- Clear Jest cache:
npx jest --clearCache - Ensure same Node.js version
- Check for environment-specific code
- Ensure
coverage/directory exists - Check file permissions
- Verify Jest configuration
Some code may be difficult to test:
- Error handling for rare conditions
- External service failures
- Environment-specific code
Consider:
- Adding
/* istanbul ignore next */comments sparingly - Mocking external dependencies
- Refactoring for testability
As test coverage improves, gradually increase thresholds:
- Target: 50% coverage (medium term)
- Goal: 70% coverage (long term)
- Ideal: 80%+ coverage for critical paths
- Statements: Individual executable statements
- Branches: if/else, switch cases, ternary operators
- Functions: Function and method definitions
- Lines: Physical lines of code (may contain multiple statements)
--------------------------------|---------|----------|---------|---------|
File | % Stmts | % Branch | % Funcs | % Lines |
--------------------------------|---------|----------|---------|---------|
All files | 31.12 | 34.3 | 35.33 | 31.02 |
src/services | 30.81 | 35.02 | 23.91 | 31.01 |
MockStellarService.js | 67.77 | 64.95 | 84.61 | 68.47 |
--------------------------------|---------|----------|---------|---------|
✅ Coverage thresholds met!