Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate multiple reports with mocha?

I want to have the following reports:

  • coverage
  • spec
  • xunit

all running in a single mocha execution from my grunt

Currently - I have to run the tests 3 times, each time to generate a different report(!).

So I use grunt-mocha-test with 2 configuration where only the reporter is different (once xunit-file and once spec).

And then I have grunt-mocha-istanbul that runs the tests yet again,and generates the coverage report.

I tried using

{ 
   options: {
        reporters : ['xunit-file', 'spec']
   }
}

for grunt-mocha-test at least to bring it down to 2, but that doesn't work as well.

reading grunt-mocha-istanbul documentation, i can't seem to find any info about reporter configuration.

How can I resolve this?

like image 233
guy mograbi Avatar asked Dec 30 '14 16:12

guy mograbi


3 Answers

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

like image 148
György Balássy Avatar answered Oct 27 '22 07:10

György Balássy


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

like image 44
Sayan Pal Avatar answered Oct 27 '22 08:10

Sayan Pal


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',
          ...
        },
        ...
      }
    }
    ...
  });
like image 35
Philip Bijker Avatar answered Oct 27 '22 09:10

Philip Bijker