Generate Coverage Workflow (generate-coverage.yaml)
The Generate Coverage Workflow automatically generates comprehensive test coverage reports and updates coverage badges when code is pushed ## ๐ Coverage Configuration
Standard Thresholdsโ
DashClicks coverage requirements:
{
"branches": 40,
"functions": 50,
"lines": 50,
"statements": 50
}
Workflow Dependenciesโ
- Node.js 16: Runtime environment
- Chrome browser: For Puppeteer test execution
- MongoDB Memory Server: Test database with binary caching
- COVERAGE_GENERATE_TOKEN: Repository write access for badge commitson branch. It runs the full test suite, processes coverage data, and commits updated badges back to the repository.
๐ Overviewโ
- File:
.github/workflows/generate-coverage.yaml - Purpose: Generate test coverage reports and update badges
- Trigger: Push to
prodbranch - Features: Full test suite execution, badge generation, automatic commits
- Platform: Ubuntu with Node.js 16 and Chrome browser
๐ Trigger Eventsโ
on:
push:
branches:
- prod
Commit Message Filteringโ
if: ${{ !contains(github.event.head_commit.message, 'Update coverage badge') }}
Prevents infinite loops by skipping runs when the workflow itself commits badge updates.
๐๏ธ Environment Setupโ
Node.js Environmentโ
# Node.js 16 setup
uses: actions/setup-node@v3
with:
node-version: '16'
# Dependency caching
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
Chrome Browser Setupโ
# Chrome for Puppeteer tests
uses: browser-actions/setup-chrome@latest
MongoDB Configurationโ
# MongoDB Memory Server setup
sudo mkdir -p /tmp/mongodb-binaries
sudo chmod 777 /tmp/mongodb-binaries
# MongoDB binary caching
uses: actions/cache@v4
with:
path: /tmp/mongodb-binaries
key: ${{ runner.os }}-mongodb-binaries-${{ hashFiles('**/package-lock.json') }}
๐งช Test Execution & Coverageโ
Project Setupโ
# Install dependencies
npm install
# Build project (copy shared files)
npm run build
Coverage Generationโ
# Run comprehensive test suite with coverage
env:
MONGOMS_VERSION: '6.0.9'
MONGOMS_DOWNLOAD_DIR: /tmp/mongodb-binaries
# Execute with error handling
set +e
npm test -- --coverage || echo "Tests or coverage generation failed, but continuing..."
Coverage Processingโ
# Move coverage summary to .github directory
mv ./coverage/coverage-summary.json .github/coverage-summary.json || echo "Failed to move, but continuing..."
๐ Badge Generationโ
Coverage Badge Creationโ
# Generate SVG badge from coverage data
npx coverage-badger -f -d ./.github/
Badge Featuresโ
- Visual representation: Color-coded coverage percentage
- Format: SVG badge compatible with GitHub README
- Location:
.github/coverage.svg - Update frequency: Every push to production
Badge Colorsโ
Coverage badges use standard color coding:
- Red: < 50% coverage
- Orange: 50-70% coverage
- Yellow: 70-80% coverage
- Green: > 80% coverage
๐ Automated Commit Processโ
Git Configurationโ
git config --global user.name 'GitHub Action'
git config --global user.email 'action@github.com'
Commit and Pushโ
# Add coverage artifacts
git add .github/coverage.svg .github/coverage-summary.json
# Commit with skip message to prevent loops
git commit -m "Update coverage badge" -n
# Push only if changes exist
git diff-index --quiet HEAD || git push https://x-access-token:${{ secrets.COVERAGE_GENERATE_TOKEN }}@github.com/${{ github.repository }} HEAD:prod
๐ Security & Authenticationโ
Required Secretsโ
COVERAGE_GENERATE_TOKEN: Personal access token with repo write permissions
Token Permissionsโ
The token requires:
- Contents: Write access for file modifications
- Metadata: Read access for repository information
- Pull requests: Read access for context
Security Best Practicesโ
- Minimal permissions: Token scoped to specific repository
- Limited scope: Only coverage badge updates
- Secure storage: Token stored in GitHub Secrets
- Regular rotation: Periodic token renewal
๐ Coverage Metricsโ
Coverage Typesโ
The workflow generates coverage for:
- Line coverage: Percentage of executed lines
- Branch coverage: Percentage of executed branches
- Function coverage: Percentage of called functions
- Statement coverage: Percentage of executed statements
Coverage Thresholdsโ
Standard DashClicks coverage requirements:
{
"branches": 40,
"functions": 50,
"lines": 50,
"statements": 50
}
Coverage Output Filesโ
coverage/
โโโ coverage-final.json # Raw coverage data
โโโ coverage-summary.json # Summary statistics
โโโ lcov.info # LCOV format
โโโ lcov-report/ # HTML report directory
โโโ index.html # Main report
โโโ ... # Individual file reports
๐ Error Handlingโ
Graceful Failureโ
# Continue execution even if tests fail
set +e
npm test -- --coverage || echo "Tests or coverage generation failed, but continuing..."
File Operation Safetyโ
# Safe file operations with fallbacks
mv ./coverage/coverage-summary.json .github/coverage-summary.json || echo "Failed to move, but continuing..."
Commit Safetyโ
# Only push if there are actual changes
git diff-index --quiet HEAD || git push ...
๐ Cache Configurationโ
Cache Pathsโ
# npm cache
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
# MongoDB binaries cache
path: /tmp/mongodb-binaries
key: ${{ runner.os }}-mongodb-binaries-${{ hashFiles('**/package-lock.json') }}
๐ Environment Variablesโ
Required Variablesโ
MONGOMS_VERSION: '6.0.9'
MONGOMS_DOWNLOAD_DIR: /tmp/mongodb-binaries
NODE_ENV: 'test'
๐ Integration Pointsโ
Repository Integrationโ
The workflow integrates with:
- README badges: Display coverage percentage
- Pull request checks: Reference coverage changes
- Development metrics: Track coverage trends
Badge Usageโ
# README.md integration

Coverage Reportsโ
Generated artifacts can be used for:
- Local development: HTML reports for detailed analysis
- CI/CD pipelines: Coverage thresholds validation
- Code review: Coverage impact assessment
๐ Related Workflowsโ
- unit-tests.yml - Individual test execution with coverage
- build.docker.yaml - Build after coverage validation