Generate multiple mocha reports in a single mocha execution.
npx @tessl/cli install tessl/npm-mocha-multi-reporters@1.5.0Mocha Multi Reporters is a Mocha reporter plugin that enables generating multiple test reports in a single test execution run. It allows developers to configure and use multiple Mocha reporters simultaneously, such as spec, xunit, json, and other built-in or custom reporters, through a simple configuration file.
npm install mocha-multi-reporters --save-devconst MultiReporters = require('mocha-multi-reporters');ES6 imports:
import MultiReporters from 'mocha-multi-reporters';The most common usage is to configure it as a Mocha reporter with a configuration file:
# Basic usage with default config
./node_modules/.bin/mocha --reporter mocha-multi-reporters
# Using custom configuration file
./node_modules/.bin/mocha --reporter mocha-multi-reporters --reporter-options configFile=config.jsonExample configuration file:
{
"reporterEnabled": "spec, json, xunit",
"jsonReporterOptions": {
"output": "test-results.json"
},
"xunitReporterOptions": {
"output": "test-results.xml"
}
}Mocha Multi Reporters extends Mocha's Base reporter class and acts as a meta-reporter that instantiates and coordinates multiple child reporters. It handles the lifecycle management of multiple reporters, ensuring proper initialization, event handling, and completion coordination.
Creates a MultiReporters instance that manages multiple child reporters.
function MultiReporters(runner, options);Parameters:
runner (object): Mocha test runner instanceoptions (object): Configuration options objectOptions object properties:
execute (boolean, default: true): Whether to execute reporter initializationreporterOptions (object): Reporter configuration optionsCoordinates completion across multiple reporters with done callbacks.
MultiReporters.prototype.done = function(failures, fn);Parameters:
failures (number): Number of test failuresfn (function): Callback function to invoke when all reporters completeMerges default, custom, and runtime options into a single configuration object.
MultiReporters.prototype.getOptions = function(options);Parameters:
options (object): Raw options objectReturns: (object) Merged configuration options
Loads custom configuration from a file or options object.
MultiReporters.prototype.getCustomOptions = function(options);Parameters:
options (object): Options containing configFile path or reporterOptionsReturns: (object) Custom configuration options
Supports both JSON and JavaScript configuration files. The configFile path is resolved relative to the current working directory.
Loads the default configuration from the built-in config.json file.
MultiReporters.prototype.getDefaultOptions = function();Returns: (object) Default configuration options from config.json
Extracts and processes options for a specific reporter by name.
MultiReporters.prototype.getReporterOptions = function(options, name);Parameters:
options (object): Global options objectname (string): Reporter nameReturns: (object) Reporter-specific configuration options
Reporter-specific options follow the pattern {camelCaseName}ReporterOptions. For example, options for the xunit reporter would be specified in xunitReporterOptions.
interface ReporterConfig {
reporterEnabled: string | string[];
reporterOptions?: object;
[reporterName + 'ReporterOptions']?: object;
cmrOutput?: string | string[][];
}The cmrOutput option allows dynamic output file naming:
{
"reporterEnabled": "json, xunit",
"cmrOutput": "json+output+results.json:xunit+output+results.xml"
}Or as an array:
{
"reporterEnabled": "json, xunit",
"cmrOutput": [
["json", "output", "results.json"],
["xunit", "output", "results.xml"]
]
}Path to the default configuration file.
MultiReporters.CONFIG_FILE = '../config.json';The reporter handles several error conditions:
The plugin supports:
Array of instantiated reporter instances.
MultiReporters.prototype._reporters: Array<object>;Each element in the array is an instantiated reporter object that follows Mocha's reporter interface.