Skip to main content
In this guide, you’ll learn how to collect JUnit-style XML results from a Node.js project using Mocha, upload them as artifacts in GitLab CI/CD, and visualize test reports in merge requests and pipelines. We’ll cover:
  1. Basic artifacts configuration
  2. Customizing retention and upload settings
  3. Enabling artifacts:reports for JUnit
  4. Available report types
  5. Viewing failures in a merge request
  6. Analyzing test summaries in the pipeline

Prerequisites

Install the JUnit reporter locally:
npm install --save-dev mocha-junit-reporter
Ensure your package.json test script invokes the reporter.

1. Basic CI/CD Job to Run Tests and Upload Artifacts

Start with a simple CI job that runs tests and uploads all generated files upon success:
stages:
  - test

variables:
  MONGO_URI: 'mongodb+srv://supercluster.d83jj.mongodb.net/superData'
  MONGO_USERNAME: superuser
  MONGO_PASSWORD: $M_DB_PASSWORD

unit_testing:
  stage: test
  image: node:17-alpine3.14
  before_script:
    - npm install
  script:
    - npm test
  artifacts:
    when: on_success
    expire_in: 30 days
This configuration uploads every file created during the job and retains them for 30 days.

2. Always Upload & Customize Artifact Retention

To ensure test results are available even on failure, adjust the artifacts block:
unit_testing:
  stage: test
  image: node:17-alpine3.14
  before_script:
    - npm install
  script:
    - npm test
  artifacts:
    when: always
    expire_in: 3 days
    name: "Mocha-Test-Result"
    paths:
      - test-results.xml
  • when: always – captures artifacts on success or failure
  • expire_in: 3 days – limits storage time to 3 days
  • name – assigns a meaningful archive name
  • paths – points to the JUnit XML file

Generating the JUnit XML

Make sure your test command includes the JUnit reporter:
npm test
# Output:
> mocha app-test.js --timeout 10000 --reporter mocha-junit-reporter --exit
A sample test-results.xml:
<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="Mocha Tests" time="4.968" tests="11" failures="0">
  <testsuite name="Root Suite" timestamp="2024-01-30T13:59:59" tests="0" time="0.000" failures="0">
    <testcase name="Planets API Suite" timestamp="2024-01-30T13:59:59" tests="8">
      <testcase name="it should fetch a planet named Mercury" time="3.383"/>
      <!-- more testcases... -->
    </testcase>
  </testsuite>
</testsuite>

3. Registering JUnit Reports with artifacts:reports

GitLab can parse JUnit XML and display it natively in merge requests and pipeline views. Extend your job:
unit_testing:
  stage: test
  image: node:17-alpine3.14
  before_script:
    - npm install
  script:
    - npm test
  artifacts:
    when: always
    expire_in: 3 days
    name: "Mocha-Test-Result"
    paths:
      - test-results.xml
  reports:
    junit: test-results.xml
This enables the Test Reports tab in merge requests and shows detailed test metrics in the pipeline.

4. Available Artifact Report Types

GitLab supports a variety of built-in report types that surface in Merge Request widgets and the pipeline UI:
Report TypeUse CaseFile Format
junitUnit test resultsJUnit XML
coberturaCode coverage metricsCobertura XML
sastStatic Application Security TestSARIF
dastDynamic Application SecurityHTML / JSON
codequalityCode Quality analysisJSON
license_managementDependency license scanningJSON
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.

5. Viewing Failures in a Merge Request

When tests fail, GitLab surfaces a summary of the failures directly in the Merge Request:
The image shows a GitLab documentation page about viewing failed tests in unit test reports. It includes a test summary panel with details of failed tests and navigation options on the side.

6. Checking Test Summaries in the Pipeline

In the pipeline’s Tests tab, you’ll see:
  • Total tests run
  • Number of failures
  • Success rate (%)
  • Average test duration
The image shows a GitLab documentation page about viewing unit test reports, displaying a summary of test results with 3 tests, 2 failures, and a 33.33% success rate.
After job completion, artifact upload logs appear:
Uploading artifacts...
test-results.xml: found 1 matching artifact files and directories
Uploading artifacts as "archive" to coordinator... 201 Created
Uploading artifacts as "junit" to coordinator... 201 Created
Cleaning up project directory and file based variables
Job succeeded
You can browse or download the raw XML:
Browsing artifacts…
test-results.xml
Or view it visually:
The image shows a GitLab CI/CD pipeline test report with 11 successful tests, each related to fetching planet details, achieving a 100% success rate.

References