Skip to main content
In this guide, you’ll learn how to set up a parallel code coverage job alongside unit tests in GitLab CI/CD, archive reports, and surface coverage metrics in the UI.

Base Unit Testing Job

This unit_testing job runs in the test stage, installs dependencies, executes tests, and collects JUnit reports:

Adding a Parallel Code Coverage Job

Create a code_coverage job that reuses the Node.js image and installs dependencies. The npm run coverage command (powered by NYC) generates a Cobertura XML report.

Supported Artifact Report Types

GitLab CI/CD supports multiple report formats under artifacts:reports. Use the table below to choose the appropriate type:
The image shows a GitLab documentation page about CI/CD artifact report types, detailing how to use artifacts:reports for collecting various reports in jobs. The page includes a sidebar with navigation links and a list of report types on the right.

Coverage Report Configuration

To enable GitLab’s built-in coverage display, specify the Cobertura format and the XML path in artifacts:reports:

Extracting Coverage Percentage

GitLab can parse test logs and extract a coverage percentage using a regular expression. For NYC’s “All files” summary line, use:
Make sure your coverage tool prints a summary line matching this pattern. Adjust the regex if your output differs.
The image shows a GitLab documentation page detailing test coverage examples with regex patterns for various programming languages and tools. The sidebar includes navigation links related to code coverage and testing.

Full CI Configuration with a Dependent Sample Job

Combine both jobs and add a sample-job that depends on code_coverage. This ensures downstream work only runs if coverage passes (or you enable allow_failure).
Once you push this .gitlab-ci.yml, the GitLab pipeline graph clearly shows unit_testing, code_coverage, and sample-job in parallel:
The image shows a GitLab Pipeline Editor interface with a successful pipeline status and a visualization of jobs including "unit_testing," "code_coverage," and "sample-job."

Pipeline Execution and Coverage Failure

If the coverage threshold defined in your package.json isn’t met, the code_coverage job fails and downstream jobs are skipped by default.
A failed coverage check will block any jobs that depend on it. To continue the pipeline regardless of coverage, you can set allow_failure: true on the coverage job.
The image shows a GitLab CI/CD pipeline interface for a NodeJS project named "Solar System," where a job named "code_coverage" has failed, while "unit_testing" has succeeded.
Example logs from NYC:
Your package.json might include:

Coverage Percentage in the GitLab UI

With the coverage regex in place, GitLab extracts the percentage (e.g., 88.88%) and displays it in the pipeline view:
The image shows a GitLab pipeline interface for a NodeJS project, indicating a failed pipeline with 11 tests, all of which passed successfully.
In this scenario, coverage is below the 90% threshold, causing code_coverage to fail and skipping sample-job:
The image shows a GitLab CI/CD pipeline interface for a NodeJS project named "Solar System," displaying job statuses including one failed, one passed, and one skipped job. The "code_coverage" job has a coverage percentage of 88.88%.

Next, we’ll look at strategies to allow selective failures and continue pipeline execution even when coverage checks fail.

Watch Video