Skip to main content

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 prod branch
  • 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](https://github.com/DashClicks-LLC/Back-End/blob/prod/.github/coverage.svg)

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

๐Ÿ’ฌ

Documentation Assistant

Ask me anything about the docs

Hi! I'm your documentation assistant. Ask me anything about the docs!

I can help you with:
- Code examples
- Configuration details
- Troubleshooting
- Best practices

Try asking: How do I configure the API?
09:31 AM