Skip to main content
In this guide, we’ll enhance an existing GitHub Actions workflow by adding a dedicated code coverage job. The new job runs in parallel with unit tests, generates coverage reports using npm run coverage, and archives the results for later inspection.

Environment Variables

Make sure the following variables are defined in your repository settings:

1. Existing Unit Testing Job

This YAML snippet configures a matrix-based unit testing job across multiple Node.js versions and OS environments:

2. Job Overview

3. Adding the Code Coverage Job

Add a second job named code-coverage right below the existing unit-testing definition:
Running npm run coverage outputs coverage data under the coverage folder, which the upload-artifact action then archives for 5 days.

4. Viewing Artifacts in the GitHub Actions UI

Artifacts from each workflow run appear in the Artifacts section of the run summary. You can remove individual artifacts manually or rely on the retention-days setting for automatic cleanup.
The image shows a GitHub Actions page for a repository named "solar-system," displaying a list of workflow runs with their statuses and details.

5. Handling Coverage Threshold Failures

If your global coverage threshold (for example, 90%) isn’t met, the coverage step exits with a non-zero code. Subsequent steps—including artifact uploads—are skipped.
The image shows a GitHub Actions workflow summary for a project named "solar-system," where unit tests have passed, but the code coverage job has failed. The status is marked as "Failure" with a total duration of 58 seconds.
The image shows a GitHub Actions workflow interface with a list of jobs, where "Code Coverage" has failed, while several unit testing jobs have succeeded.
To ensure artifact upload even on failure, consider using continue-on-error: true for the coverage step or placing the upload step in a separate always() conditional.

6. Next Steps

  • Write additional tests to boost coverage above your threshold.
  • Tweak or disable the coverage threshold in your test config.
  • Implement error-handling strategies to archive critical artifacts regardless of step failures.

Watch Video