npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

mocha-gitlab-reporter

v2.0.0

Published

A GitLab CI compatible JUnit XML reporter for Mocha. Generates test reports that display correctly in GitLab's merge request and pipeline interfaces.

Readme

mocha-gitlab-reporter

npm npm downloads

A GitLab CI compatible JUnit XML reporter for Mocha. Generates test reports that display correctly in GitLab's merge request and pipeline interfaces.

Why This Reporter?

GitLab CI has specific requirements for JUnit XML format to properly display test results. This reporter is specifically designed to generate XML that GitLab CI expects, ensuring the test results display correctly with:

  • Test names and suite hierarchies properly formatted
  • Relative file paths for easy navigation
  • Proper classname/name structure that GitLab recognizes
  • Full support for attachments (screenshots, logs, etc.)

Requirements

| | Supported | | ------- | ---------------- | | Mocha | 10, 11, 12 | | Node.js | 22.12.0 or later |

Installation

npm install mocha-gitlab-reporter --save-dev

Usage

Run mocha with mocha-gitlab-reporter:

mocha test --reporter mocha-gitlab-reporter

This will output a results file at ./test-results.xml.

GitLab CI Configuration

Example .gitlab-ci.yml configuration:

test:
  stage: test
  script:
    - npm install
    - mocha test --reporter mocha-gitlab-reporter
  artifacts:
    when: always
    reports:
      junit: test-results.xml

Features

Proper Test Hierarchy

The reporter automatically generates XML with the correct structure for GitLab:

  • testcase classname = full suite hierarchy (e.g., "API Tests.UserController")
  • testcase name = individual test name (e.g., "should create user")
  • Suite titles are always separated by . for clean display in GitLab
  • Full suite titles are always included (nested suite hierarchy)
  • File paths are automatically included in test cases and converted to be relative to the current working directory

Console Reporter

By default, this reporter only generates the XML file without console output. To enable console output, set the consoleReporter option:

Using a .mocharc.js configuration file:

module.exports = {
  reporter: "mocha-gitlab-reporter",
  reporterOptions: ["consoleReporter=spec"],
};

Or via command line:

mocha test --reporter mocha-gitlab-reporter --reporter-options consoleReporter=spec

Or via environment variable:

CONSOLE_REPORTER=spec mocha test --reporter mocha-gitlab-reporter

Any built-in Mocha reporter name (spec, dot, nyan, tap, landing, list, progress, json, min, etc.) or a custom reporter module can be used.

Results Report Filename

An alternate location for the results XML file can be specified by setting the mochaFile option:

Using a .mocharc.js configuration file:

module.exports = {
  reporter: "mocha-gitlab-reporter",
  reporterOptions: ["mochaFile=./path_to_your/file.xml"],
};

Or via command line:

mocha test --reporter mocha-gitlab-reporter --reporter-options mochaFile=./path_to_your/file.xml

Or via environment variable:

MOCHA_FILE=./path_to_your/file.xml mocha test --reporter mocha-gitlab-reporter --reporter-options mochaFile=./path_to_your/file.xml

File Path Transformation

If your test files are built/compiled to a different directory, you can use regex to transform the file paths in the report. This is useful when you want the report to reference source files instead of compiled files.

Note: The filePathTransforms value must be a string. Use the pipe-delimited format for easier CLI usage.

Single Transformation

module.exports = {
  reporter: "mocha-gitlab-reporter",
  reporterOptions: ["filePathTransforms={search: '^build/'| replace: 'src/'}"],
};

Multiple Transformations

module.exports = {
  reporter: "mocha-gitlab-reporter",
  reporterOptions: [
    "filePathTransforms=[{search: '^build/'| replace: 'src/'}|{search: '.js$'| replace: '.ts'}]",
  ],
};

This will transform paths like:

  • build/modules/example.spec.js

To:

  • src/modules/example.test.ts

The transformations are applied in order, so the output of the first transformation becomes the input to the second transformation.

Console Output Capture

You can capture console output and errors:

it("should report output", function () {
  this.test.consoleOutputs = ["line 1 of output", "line 2 of output"];
});

it("should report error", function () {
  this.test.consoleErrors = ["line 1 of errors", "line 2 of errors"];
});

Enable outputs in your reporter options:

Using a .mocharc.js configuration file:

module.exports = {
  reporter: "mocha-gitlab-reporter",
  reporterOptions: ["outputs=true"],
};

Attachments Support

You can attach files and screenshots using the JUnit Attachments Plugin format:

it("should display login page", function () {
  // Your test code
  this.test.attachments = ["/absolute/path/to/screenshot.png"];
});

Enable attachments in your reporter options:

module.exports = {
  reporter: "mocha-gitlab-reporter",
  reporterOptions: ["attachments=true"],
};

Configuration Options

| Parameter | Default | Effect | | ------------------ | ------------------ | ---------------------------------------------------------------------------------------------- | | mochaFile | test-results.xml | Configures the file to write reports to | | includePending | false | If set to a truthy value pending tests will be included in the report | | toConsole | false | If set to a truthy value the produced XML will be logged to the console | | consoleReporter | null | Name of a Mocha reporter to also output to console (e.g., "spec", "dot", "nyan") | | outputs | false | If set to truthy value will include console output and console error output | | attachments | false | If set to truthy value will attach files to report in JUnit Attachments Plugin format | | filePathTransforms | null | String with pipe-delimited transformations (e.g., "[{search: '^build/'\| replace: 'src/'}]") |

Results Report Filename Placeholders

Results XML filename can contain placeholders for dynamic values:

| placeholder | output | | ----------------- | ---------------------------- | | [hash] | MD5 hash of test results XML | | [suiteFilename] | Filename of the spec file | | [suiteName] | Name of the first test suite |

Example:

module.exports = {
  reporter: "mocha-gitlab-reporter",
  reporterOptions: ["mochaFile=test-results.[hash].xml"],
};

This enables support of parallel execution of multiple mocha-gitlab-reporter's writing test results in separate files.

Example Output

Here's what the XML output looks like:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Mocha Tests" time="0.050" tests="2" failures="0">
  <testsuite name="Root Suite" timestamp="2025-10-24T09:18:46" tests="0" time="0.000" failures="0">
  </testsuite>
  <testsuite name="API Tests" timestamp="2025-10-24T09:18:46" tests="0" time="0.000" failures="0">
  </testsuite>
  <testsuite name="UserController" timestamp="2025-10-24T09:18:46" tests="2" time="0.050" failures="0">
    <testcase name="should create user" time="0.025" classname="API Tests.UserController" file="test/api/user-test.js">
    </testcase>
    <testcase name="should update user" time="0.025" classname="API Tests.UserController" file="test/api/user-test.js">
    </testcase>
  </testsuite>
</testsuites>

Note how:

  • classname="API Tests.UserController" - This is what GitLab displays as the suite name in the UI
  • name="should create user" - The individual test name shown in GitLab
  • file="test/api/user-test.js" - Relative path for navigation in GitLab

Important: GitLab reads the suite name from the testcase classname attribute, not from the testsuite name attribute.

Differences from mocha-junit-reporter

This package is a simplified, GitLab-focused fork of mocha-junit-reporter. Key differences:

  • Optimized for GitLab CI - Default settings work out of the box with GitLab
  • Simplified configuration - Removed Jenkins-specific and Ant-specific options

Resources

License

MIT