DEV Community

Cover image for Mocha.js - how to enable multiple test runners on CI/CD?
Rafal Hofman
Rafal Hofman

Posted on • Edited on • Originally published at brightinventions.pl

1 1

Mocha.js - how to enable multiple test runners on CI/CD?

One of our projects is running automated tests on CI/CD AzurePipelines.

For the test runner, AzurePipeline supports several test results templates but not the default Mocha spec one.

This is why tests are running on the mocha Junit reporter producing the JUnit XML result.

As there was a difference between running tests locally and on CI/CD environment, I wanted to debug the logs of the job. That what not possible as the Mocha JUnit reporter was not collecting console outputs/errors.

Mocha.js does not support multiple runners right now. The solution for that was to introduce own runner, which combines both Mocha JUnit reporter and default spec Mocha reporter:

'use strict';

const Mocha = require('mocha');
const JUnit = require('mocha-junit-reporter');
const Spec = Mocha.reporters.Spec;
const Base = Mocha.reporters.Base;

// This is combination of spec (mocha normal) + junit reporter so both is displayed on azure
class AzurePipelinesReporter extends Base {
    constructor(runner, options) {
        super(runner, options);
        this._junitReporter = new JUnit(runner, options);
        this._specReporter = new Spec(runner, options);
    }
}
module.exports = AzurePipelinesReporter;
Enter fullscreen mode Exit fullscreen mode

Then, the custom reporter can be used by specyfing the file name and flags to both reporters if needed:

mocha --reporter azurePipelinesReporter.js --reporter-options mochaFile=some_path_to_results

Let me know if you had a similar problem, stay tuned for the next tips & tricks!

This blog post was original posted on https://brightinventions.pl/blog/mocha-js-how-to-enable-multiple-test-runners-on-ci-cd

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay