Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


Build Status Coverage Status Dependency Status devDependency Status

Sonar friendly xunit reporter.

This is a modification of the existing xunit reporter that ships with Mocha. There are 2 problems that become apparent when standard xunit reports are submitted to Sonar using the sonar javascript plugin.

  • Sonar will reject reports that have a classname that mirrors a source file, eg. if you have a source file called MyClass.js then you cannot have a test with a classname of MyClass
  • Sonar interprets the classname field as a filename resulting in hard to read test reports in the Sonar UI (this is probably also the cause of the first issue)

This reporter will generate xunit output that uses the concatenation of the suite and test titles as the test name and set the classname to:

  • if testdir is defined in config, the test file relative path
  • else a configurable constant so that name collisions can be avoided. If no classname is configured it will default to Test.


Install and save to your devDependencies

npm install --save-dev mocha-sonar-reporter

Configure the classname in package.json (optional)

  "config": {
    "mocha-sonar-reporter": {
      "classname": "Test"

Configure the testdir in package.json (optional)

  "config": {
    "mocha-sonar-reporter": {
      "testdir": "tests",
      // Optionally configure a suffix for the extracted class names.
      // It has been reported that some versions of the sonar javascript
      // plugin require class names to have a `.js` suffix
      "classnameSuffix": ".js"

Configure the outputfile in package.json, to generate data in file instead of stdout (optional)

  "config": {
    "mocha-sonar-reporter": {
      "outputfile": "test/TEST-all.xml"

Add the following to your / file


Specify the mocha-sonar-reporter when running mocha

mocha -R mocha-sonar-reporter

NB. feel free to change paths and file names above ;)

NNB. Although not documented here, you may also like to use Grunt and the grunt-mocha-test plugin to do this and get coverage data, etc

A note on use outside of npm

The options for the reporter are added to package.json. This means that they are only loaded if mocha is run via npm (eg. via npm test). If you wish to use the reporter without npm then the options can still be specified in environment variables using the naming conventions that npm uses internally. ie:



Add tests for changes and run

npm test


Copyright © 2015 Peter Halliday
Licensed under the MIT license.

Donate Bitcoins