Mocha

Mocha is a testing framework for Javascript and Typescript.

Enabling XML Output

Mocha can be configured to produce JUnit XML output by adding the mocha-junit-reporter package to your codebase.

npm install --save-dev mocha-junit-reporter

Now run Mocha from the command line or inside your CI system as:

mocha test --reporter mocha-junit-reporter

Test Suite Naming

The mocha-junit-reporter will automatically fill in values for the and name and class attributes. The test:

describe('addition', () => {
  describe('positive numbers', () => {
    it('should add up', () => {
      expect(1 + 2).toBe(3);
    });
  });
});

will produce output that looks like this:

<testsuites name="Mocha Tests">
  <testsuite name="addition">
    <testcase classname="addition positive numbers should add up" 
              name="addition positive numbers should add up"
              file="/somepath/tests/mocha/mocha.test.js"
              >
    </testcase>
  </testsuite>
</testsuites>

The default attributes can be configured with the reporterOptions argument in the .mocharc.js or similar config file.

var mocha = new Mocha({
    reporter: 'mocha-junit-reporter',
    reporterOptions: {
        testsuitesTitle: true,
        // suites separator, default is space (' ')
        suiteTitleSeparatedBy: '.' 
    }
});

By default Mocha will include the file attribute.

Further Information

See an example of MochaJS invoked form a GitHub action here.

Last updated