How to generate multiple reports with mocha?

Maybe this can help: https://github.com/glenjamin/mocha-multi

AFAIK this is not supported in Mocha yet, but it is on its way: https://github.com/mochajs/mocha/pull/1360

Hope this helps,

György


For simultaneously reporting for spec and x-unit, there's also an NPM package called spec-xunit-file.

In grunt:

grunt.initConfig({
    mochaTest: {
      test: {
        options: {
          reporter: 'spec-xunit-file',
          ...
        },
        ...
      }
    }
    ...
  });

I ran into the same problem recently, and found nothing after looking around SO as well as GH issues. It seems that topic of officially supporting multiple reporters are getting postponed over and over.

Having said that having a custom solution is quite easy, assuming the reporters you want to combine already exist. What I did is to create a small and naive custom reporter, and used the reporter in .mocharc.js config.

// junit-spec-reporter.js
const mocha = require("mocha");
const JUnit = require("mocha-junit-reporter");
const Spec = mocha.reporters.Spec;
const Base = mocha.reporters.Base;

function JunitSpecReporter(runner, options) {
    Base.call(this, runner, options);
    this._junitReporter = new JUnit(runner, options);
    this._specReporter = new Spec(runner, options);
    return this;
}
JunitSpecReporter.prototype.__proto__ = Base.prototype;

module.exports = JunitSpecReporter;
// .mocharc.js
module.exports = {
    reporter: './junit-spec-reporter.js',
    reporterOptions: {
        mochaFile: './tests-results/results.xml'
    }
};

The example above shows how to use both spec and junit reporter.

More info on custom reporter: https://mochajs.org/api/tutorial-custom-reporter.html

Note that this is just a proof of concept and can be made prettier and more robust using more generic approach (and TypeScript).


Update 14.9.2021

I have created a utility package for this: https://www.npmjs.com/package/@netatwork/mocha-utils