Title: Add test coverage reporting and thresholds
Description: Introduce coverage reporting to ensure new changes don't reduce overall test quality.
Status: COMPLETED
- Multiple report formats configured (text, HTML, LCOV, JSON)
- Reports generated automatically with
npm run test:coverage - HTML report provides detailed line-by-line coverage visualization
- Terminal output shows immediate coverage summary
- All reports saved to
coverage/directory
Status: COMPLETED
- Jest enforces minimum 30% thresholds for all metrics
- CI/CD workflows automatically fail if thresholds not met
- Pull requests blocked until coverage requirements satisfied
- Clear error messages guide developers to fix issues
- Coverage validation integrated in both dedicated and main CI workflows
Status: COMPLETED
Files Modified:
-
jest.config.js- Enhanced with comprehensive coverage configuration- Added multiple coverage reporters
- Set coverage directory
- Configured collection patterns
- Defined minimum thresholds (30% for all metrics)
-
package.json- Added coverage scriptstest:coverage- Local coverage generationtest:coverage:ci- CI-optimized coverage runcheck-coverage- Pre-commit validation script
Files Created:
scripts/check-coverage.js- Automated coverage validation tool- Reads coverage summary JSON
- Validates against thresholds
- Provides clear pass/fail feedback
- Exit codes for CI/CD integration
Status: COMPLETED
Thresholds Set: 30% minimum for all metrics
| Metric | Threshold | Enforcement |
|---|---|---|
| Branches | 30% | ✅ Active |
| Functions | 30% | ✅ Active |
| Lines | 30% | ✅ Active |
| Statements | 30% | ✅ Active |
Rationale:
- Set at current coverage level to prevent regression
- Allows gradual improvement without blocking development
- Can be increased as coverage improves over time
CI/CD Integration:
-
.github/workflows/coverage.yml- Enhanced dedicated coverage workflow- Runs on all PRs and pushes to main/develop
- Executes tests with coverage collection
- Validates thresholds automatically
- Uploads coverage artifacts (30-day retention)
- Generates coverage summary in GitHub Actions
-
.github/workflows/ci.yml- Updated main CI pipeline- Integrated coverage check alongside other CI jobs
- Proper environment variables for test execution
- Coverage job must pass for CI to succeed
- ✅
jest.config.js- Enhanced coverage configuration - ✅
package.json- Added coverage scripts - ✅
.github/workflows/coverage.yml- Enhanced coverage workflow - ✅
.github/workflows/ci.yml- Updated CI pipeline
- ✅
scripts/check-coverage.js- Coverage validation script
- ✅
docs/COVERAGE_GUIDE.md- Comprehensive coverage guide - ✅
docs/COVERAGE_IMPLEMENTATION_COMPLETE.md- Implementation details - ✅
COVERAGE_QUICK_REFERENCE.md- Quick reference card - ✅
README.md- Updated Testing and Contributing sections - ✅
TASK_COMPLETION_SUMMARY.md- This summary
Local Development:
# Run tests with coverage
npm run test:coverage
# Check if thresholds met
npm run check-coverage
# View detailed HTML report
open coverage/lcov-report/index.htmlPre-Commit Workflow:
- Make code changes
- Add/update tests
- Run
npm run test:coverage - Run
npm run check-coverage - Review HTML report if needed
- Commit and push
Automatic Enforcement:
- Coverage workflow runs on every PR
- Tests execute with coverage collection
- Jest validates thresholds automatically
- Build fails if any metric < 30%
- Coverage reports uploaded as artifacts
PR Workflow:
- Developer creates PR
- Coverage workflow triggers
- Tests run with coverage
- Thresholds validated
- ✅ Pass: PR can merge
- ❌ Fail: PR blocked, developer adds tests
-
Text Summary (Terminal)
- Quick overview after test run
- Shows all metrics with percentages
- Color-coded indicators
-
HTML Report (
coverage/lcov-report/index.html)- Interactive file browser
- Line-by-line coverage visualization
- Uncovered code highlighted
- Branch coverage details
-
LCOV Report (
coverage/lcov.info)- Machine-readable format
- CI/CD integration
- Compatible with coverage services
-
JSON Summary (
coverage/coverage-summary.json)- Programmatic access
- Used by check-coverage script
- Enables custom tooling
-
Coverage Thresholds
- All metrics must be ≥ 30%
- Enforced by Jest automatically
- Validated in CI/CD
-
Build Failure Conditions
- Any metric below threshold
- Test failures
- Coverage report generation errors
-
PR Merge Requirements
- All tests passing
- Coverage thresholds met
- Linting checks passed
- Security checks passed
- ✅ Prevents quality regression
- ✅ Encourages test-driven development
- ✅ Identifies untested code
- ✅ Maintains minimum quality standards
- ✅ Immediate feedback on coverage
- ✅ Clear targets for improvement
- ✅ Visual identification of gaps
- ✅ Fast local validation
- ✅ Automated enforcement
- ✅ No manual review needed
- ✅ Blocks low-quality PRs
- ✅ Historical tracking via artifacts
cd Stellar-Micro-Donation-API
npm run test:coverageExpected Output:
- Tests run successfully
- Coverage summary displayed
- HTML report generated at
coverage/lcov-report/index.html - All thresholds met (✅)
npm run check-coverageExpected Output:
🔍 Checking test coverage...
Coverage Results:
────────────────────────────────────────────────────────────
✅ branches 34.30% (min: 30%)
✅ functions 35.33% (min: 30%)
✅ lines 31.02% (min: 30%)
✅ statements 31.12% (min: 30%)
────────────────────────────────────────────────────────────
✅ All coverage thresholds met!
- Create test PR
- Coverage workflow runs automatically
- Check workflow logs for coverage results
- Download coverage artifact from workflow
- Verify build passes/fails based on coverage
- Add coverage badges to README
- Generate coverage diff reports
- Per-file coverage requirements
- Increase thresholds to 40-50%
- Coverage trend tracking
- Integration with code review tools
- Achieve 70%+ overall coverage
- 90%+ for critical business logic
- Automated improvement suggestions
-
Coverage Guide (
docs/COVERAGE_GUIDE.md)- Complete usage instructions
- Understanding metrics
- Improving coverage
- Troubleshooting
- Best practices
-
Implementation Details (
docs/COVERAGE_IMPLEMENTATION_COMPLETE.md)- Technical implementation
- Configuration details
- CI/CD integration
- Testing procedures
-
Quick Reference (
COVERAGE_QUICK_REFERENCE.md)- Essential commands
- Current thresholds
- Pre-commit checklist
- Report locations
-
Updated README (
README.md)- Enhanced Testing section
- Coverage commands
- Contributing guidelines
This implementation provides a complete, production-ready test coverage reporting and enforcement system that:
- ✅ Generates comprehensive coverage reports on every test run with multiple formats
- ✅ Enforces minimum 30% thresholds across all metrics automatically
- ✅ Fails builds if coverage drops below thresholds in CI/CD
- ✅ Provides clear feedback to developers with actionable guidance
- ✅ Integrates seamlessly with existing CI/CD workflows
- ✅ Includes extensive documentation for developers and maintainers
- ✅ Offers developer-friendly tooling for local validation
The system is fully automated, requires no manual intervention, and effectively prevents code quality regression while encouraging test-driven development practices.
All acceptance criteria met. Coverage reporting and threshold enforcement successfully implemented with comprehensive tooling and documentation.