> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Artifacts Unit Test Reports

> This guide explains how to collect and visualize JUnit-style test results in GitLab CI/CD using Mocha and Node.js.

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

* A GitLab project with CI/CD enabled
* A Node.js test suite configured with [mocha-junit-reporter](https://www.npmjs.com/package/mocha-junit-reporter)

<Callout icon="lightbulb" color="#1CB2FE">
  Install the JUnit reporter locally:

  ```bash theme={null}
  npm install --save-dev mocha-junit-reporter
  ```

  Ensure your `package.json` test script invokes the reporter.
</Callout>

***

## 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:

```yaml theme={null}
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:

```yaml theme={null}
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:

```bash theme={null}
npm test
# Output:
> mocha app-test.js --timeout 10000 --reporter mocha-junit-reporter --exit
```

A sample `test-results.xml`:

```xml theme={null}
<?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:

```yaml theme={null}
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 Type         | Use Case                         | File Format   |
| ------------------- | -------------------------------- | ------------- |
| junit               | Unit test results                | JUnit XML     |
| cobertura           | Code coverage metrics            | Cobertura XML |
| sast                | Static Application Security Test | SARIF         |
| dast                | Dynamic Application Security     | HTML / JSON   |
| codequality         | Code Quality analysis            | JSON          |
| license\_management | Dependency license scanning      | JSON          |

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752877235/notes-assets/images/GitLab-CICD-Architecting-Deploying-and-Optimizing-Pipelines-Artifacts-Unit-Test-Reports/gitlab-cicd-artifact-reports.jpg)
</Frame>

***

## 5. Viewing Failures in a Merge Request

When tests fail, GitLab surfaces a summary of the failures directly in the Merge Request:

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752877236/notes-assets/images/GitLab-CICD-Architecting-Deploying-and-Optimizing-Pipelines-Artifacts-Unit-Test-Reports/gitlab-viewing-failed-tests-report.jpg)
</Frame>

***

## 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

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752877238/notes-assets/images/GitLab-CICD-Architecting-Deploying-and-Optimizing-Pipelines-Artifacts-Unit-Test-Reports/gitlab-unit-test-reports-summary.jpg)
</Frame>

After job completion, artifact upload logs appear:

```bash theme={null}
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:

```bash theme={null}
Browsing artifacts…
test-results.xml
```

Or view it visually:

<Frame>
  ![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.](https://kodekloud.com/kk-media/image/upload/v1752877239/notes-assets/images/GitLab-CICD-Architecting-Deploying-and-Optimizing-Pipelines-Artifacts-Unit-Test-Reports/gitlab-cicd-pipeline-test-report.jpg)
</Frame>

***

## References

* [GitLab CI/CD Pipelines](https://docs.gitlab.com/ee/ci/pipelines/)
* [Artifacts](https://docs.gitlab.com/ee/ci/pipelines/job_artifacts.html)
* [JUnit Test Reports in GitLab](https://docs.gitlab.com/ee/user/project/pipelines/test_reports.html)
* [mocha-junit-reporter on npm](https://www.npmjs.com/package/mocha-junit-reporter)
* [GitLab CI/CD YAML Configuration](https://docs.gitlab.com/ee/ci/yaml/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/gitlab-ci-cd-architecting-deploying-and-optimizing-pipelines/module/3a1c2306-8091-4dfe-b40f-e2ca53918553/lesson/20c65bbd-2d75-42be-b0cc-02bb346d6512" />
</CardGroup>
