GitHub Actions Certification

Continuous Integration with GitHub Actions

Workflow Configure Code Coverage

Extend your GitHub Actions CI/CD pipeline to include code coverage analysis alongside unit testing. This guide shows how to:

  • Mirror your existing unit testing setup
  • Add a parallel code-coverage job
  • Archive coverage reports even on failures

1. Reference: Unit Testing Job

Here’s the baseline unit-testing job in .github/workflows/ci.yml:

env:
  MONGO_URI: "mongodb+srv://supercluster.d83jj.mongodb.net/superData"
  MONGO_USERNAME: ${{ vars.MONGO_USERNAME }}
  MONGO_PASSWORD: ${{ secrets.MONGO_PASSWORD }}

jobs:
  unit-testing:
    name: Unit Testing
    strategy:
      matrix:
        nodejs_version: [18, 19, 20]
        operating_system: [ubuntu-latest, macos-latest]
      exclude:
        - nodejs_version: 18
          operating_system: macos-latest
    runs-on: ${{ matrix.operating_system }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Node.js ${{ matrix.nodejs_version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.nodejs_version }}

      - name: Install dependencies
        run: npm install

      - name: Run unit tests
        run: npm test

      - name: Archive test results
        uses: actions/upload-artifact@v3
        with:
          name: Mocha-Test-Result
          path: test-results.xml
          retention-days: 5

2. Add a Parallel Code Coverage Job

Below is the code-coverage job that runs side by side with your unit tests:

  code-coverage:
    name: Code Coverage
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Node.js 18
        uses: actions/setup-node@v3
        with:
          node-version: 18

      - name: Install dependencies
        run: npm install

      - name: Run coverage
        run: npm run coverage

      - name: Archive coverage report
        uses: actions/upload-artifact@v3
        with:
          name: Code-Coverage-Result
          path: coverage
          retention-days: 5

3. Job Comparison

JobNode.js VersionOS MatrixCLI CommandArtifact
Unit Testing18, 19, 20 (excl macOS 18)ubuntu-latest, macos-latestnpm testtest-results.xml
Code Coverage18ubuntu-latestnpm run coveragecoverage/ directory

4. Observe Workflow Runs

After pushing your changes, both jobs appear in the GitHub Actions UI:

The image shows a GitHub Actions page for a project called "Solar System Workflow," displaying a list of workflow runs with their statuses and details.

5. Handling Coverage Threshold Failures

If coverage falls below your defined threshold, the npm run coverage step will fail:

> [email protected] coverage
> nyc --reporter=text --reporter=lcov mocha "test/**/*.js"

ERROR: Coverage for lines (88.88%) does not meet global threshold (90%)
File                     % Stmts % Branch % Funcs % Lines Uncovered Line #
-----------------------------------------------------------------------
All files                88.88    87.5     87.5   88.88  21,47-48,56
app.js                   88.88    87.5     87.5   88.88  21,47-48,56
Error: Process completed with exit code 1.

The image shows a GitHub Actions workflow summary with successful unit tests but a failed code coverage check, as the coverage did not meet the required threshold.

Warning

By default, a failing coverage step halts the workflow and skips artifact upload.
To ensure your coverage report is always archived, consider:

  • Adding continue-on-error: true to the coverage step
  • Lowering your coverage thresholds in nyc configuration

6. Next Steps

Watch Video

Watch video content

Previous
Run Unit Testing using Matrix Strategy