
Security News
High Salaries No Longer Enough to Attract Top Cybersecurity Talent
A survey of 500 cybersecurity pros reveals high pay isn't enough—lack of growth and flexibility is driving attrition and risking organizational security.
mocha-multi-reporters
Advanced tools
Generate multiple mocha reports in a single mocha execution.
The mocha-multi-reporters npm package allows you to use multiple reporters for Mocha test results. This is useful for generating different types of reports (e.g., JSON, HTML, etc.) from a single test run.
Multiple Reporters Configuration
This feature allows you to configure multiple reporters in a single configuration file. In this example, the 'spec' and 'mocha-junit-reporter' reporters are enabled, and the JUnit reporter is configured to output results to 'test-results.xml'.
{
"reporterEnabled": "spec, mocha-junit-reporter",
"mochaJunitReporterReporterOptions": {
"mochaFile": "./test-results.xml"
}
}
Custom Reporter Options
This feature allows you to specify custom options for each reporter. In this example, the 'json' reporter is configured to output results to 'test-results.json'.
{
"reporterEnabled": "spec, json",
"jsonReporterOptions": {
"output": "./test-results.json"
}
}
Using with Mocha CLI
This feature allows you to use the mocha-multi-reporters package with the Mocha CLI. The '--reporter-options' flag is used to specify the configuration file that contains the reporter settings.
mocha --reporter mocha-multi-reporters --reporter-options configFile=reporterConfig.json
mocha-parallel-tests is a package that allows you to run Mocha tests in parallel. While it focuses on parallel execution rather than multiple reporters, it can be used in conjunction with other reporter packages to achieve similar results.
mocha-multi is another package that allows you to use multiple reporters with Mocha. It is similar to mocha-multi-reporters but may have different configuration options and features.
mocha-jenkins-reporter is a package that generates Jenkins-compatible XML reports for Mocha tests. While it focuses on a specific type of report, it can be used alongside other reporter packages to provide multiple types of reports.
Generate multiple mocha reports in a single mocha execution.
npm install mocha-multi-reporters --save-dev
https://github.com/stanleyhlng/mocha-multi-reporters-demo
$ ./node_modules/.bin/mocha --reporter mocha-multi-reporters
mocha-test #1
✓ sample test #1.1
✓ sample test #1.2
mocha-test #2
✓ sample test #2.1
- sample test #2.2
3 passing (6ms)
1 pending
<testsuite name="Mocha Tests" tests="4" failures="0" errors="0" skipped="1" timestamp="Sun, 03 Jan 2016 08:15:14 GMT" time="0.005">
<testcase classname="mocha-test #1" name="sample test #1.1" time="0"/>
<testcase classname="mocha-test #1" name="sample test #1.2" time="0"/>
<testcase classname="mocha-test #2" name="sample test #2.1" time="0"/>
<testcase classname="mocha-test #2" name="sample test #2.2" time="0"><skipped/></testcase>
</testsuite>
Set the reporters configuration using --reporter-options configFile=config.json
.
reporterEnabled
as a comma-delimited list
{
"reporterEnabled": "spec, @my-org/custom"
}
reporterOptions
, as key.
For scoped reporters such as example @myorg/custom, remove all special characters.
{
"reporterEnabled": "spec, @my-org/custom",
"myOrgCustomReporterOptions": {
// [...]
}
}
spec
and json
reports.// File: config.json
{
"reporterEnabled": "spec, json"
}
$ ./node_modules/.bin/mocha --reporter mocha-multi-reporters --reporter-options configFile=config.json
mocha-test #1
✓ sample test #1.1
✓ sample test #1.2
mocha-test #2
✓ sample test #2.1
- sample test #2.2
3 passing (6ms)
1 pending
{
"stats": {
"suites": 2,
"tests": 4,
"passes": 3,
"pending": 1,
"failures": 0,
"start": "2015-12-30T22:49:39.713Z",
"end": "2015-12-30T22:49:39.717Z",
"duration": 4
},
"tests": [
{
"title": "sample test #1.1",
"fullTitle": "mocha-test #1 sample test #1.1",
"duration": 1,
"err": {}
},
{
"title": "sample test #1.2",
"fullTitle": "mocha-test #1 sample test #1.2",
"duration": 0,
"err": {}
},
{
"title": "sample test #2.1",
"fullTitle": "mocha-test #2 sample test #2.1",
"duration": 0,
"err": {}
},
{
"title": "sample test #2.2",
"fullTitle": "mocha-test #2 sample test #2.2",
"err": {}
}
],
"pending": [
{
"title": "sample test #2.2",
"fullTitle": "mocha-test #2 sample test #2.2",
"err": {}
}
],
"failures": [],
"passes": [
{
"title": "sample test #1.1",
"fullTitle": "mocha-test #1 sample test #1.1",
"duration": 1,
"err": {}
},
{
"title": "sample test #1.2",
"fullTitle": "mocha-test #1 sample test #1.2",
"duration": 0,
"err": {}
},
{
"title": "sample test #2.1",
"fullTitle": "mocha-test #2 sample test #2.1",
"duration": 0,
"err": {}
}
]
}%
tap
and xunit
reports.// File: config.json
{
"reporterEnabled": "tap, xunit",
"xunitReporterOptions": {
"output": "xunit-custom.xml"
}
}
$ ./node_modules/.bin/mocha --reporter mocha-multi-reporters --reporter-options configFile=config.json
1..4
ok 1 mocha-test 1 sample test 1.1
ok 2 mocha-test 1 sample test 1.2
ok 3 mocha-test 2 sample test 2.1
ok 4 mocha-test 2 sample test 2.2 # SKIP -
# tests 3
# pass 3
# fail 0
$ cat xunit-custom.xml
<testsuite name="Mocha Tests" tests="4" failures="0" errors="0" skipped="1" timestamp="Sun, 03 Jan 2016 08:02:24 GMT" time="0.006">
<testcase classname="mocha-test #1" name="sample test #1.1" time="0.001"/>
<testcase classname="mocha-test #1" name="sample test #1.2" time="0.001"/>
<testcase classname="mocha-test #2" name="sample test #2.1" time="0"/>
<testcase classname="mocha-test #2" name="sample test #2.2" time="0"><skipped/></testcase>
</testsuite>
tap
and junit
reports.To generate junit
report, we are using mocha-junit-reporter.
$ npm install mocha-junit-reporter
// File: config.json
{
"reporterEnabled": "mocha-junit-reporter",
"mochaJunitReporterReporterOptions": {
"mochaFile": "junit-custom.xml"
}
}
$ ./node_modules/.bin/mocha --reporter mocha-multi-reporters --reporter-options configFile=config.json
1..4
ok 1 mocha-test 1 sample test 1.1
ok 2 mocha-test 1 sample test 1.2
ok 3 mocha-test 2 sample test 2.1
ok 4 mocha-test 2 sample test 2.2 # SKIP -
# tests 3
# pass 3
# fail 0
$ cat xunit-custom.xml
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Mocha Tests" time="0.001" tests="4" failures="0" skipped="1">
<testsuite name="Root Suite" timestamp="2016-10-30T02:27:54" tests="0" failures="0" time="0">
</testsuite>
<testsuite name="mocha-test #1" timestamp="2016-10-30T02:27:54" tests="2" failures="0" time="0.001">
<testcase name="mocha-test #1 sample test #1.1" time="0.001" classname="sample test #1.1">
</testcase>
<testcase name="mocha-test #1 sample test #1.2" time="0" classname="sample test #1.2">
</testcase>
</testsuite>
<testsuite name="mocha-test #2" timestamp="2016-10-30T02:27:54" tests="2" failures="0" time="0">
<testcase name="mocha-test #2 sample test #2.1" time="0" classname="sample test #2.1">
</testcase>
</testsuite>
</testsuites>
cmrOutput
optionThis option lets you dynamically replace the output files of reporter options.
In your Mocha --reporterOptions
, specify cmrOutput
with a plug-sign-separated
list of the reporter name, the property on that reporter's options to have replaced, and the dynamic ID you would like substituted. If you need multiple reporters
to have dynamic output, add additional plus-sign-separated lists separated by colons.
mocha --reporter mocha-multi-reporters --reporterOptions configFile=mocha-multi-reporters.json,cmrOutput=@mochajs/json-file-reporter+output+specialID tests
// mocha-multi-reporters.json
{
"mochajsJsonFileReporterReporterOptions": {
"output": "tests/results/file-{id}.json"
},
"reporterEnabled": "spec, @mochajs/json-file-reporter"
}
This will produce an output
for @mochajs/json-file-reporter
reporterOptions
with the value:
tests/results/file-specialID.json
Note that when Mocha is called programmatically, it is passed an options object when created. This object is usually derived from a config file that your mocha test runner reads prior to instantiation. This is the object that must contain a key reporter
with a value of mocha-multi-reporters
for this plugin to be used. You can also pass the key reporterOptions
with a value of any of the above listed config files (including the reporterEnabled
subkey and any other plugin configuration information.) This removes the requirement to have an intermediate configuration file specifically for the multireporter configuration.
const mocha = new Mocha({
reporter: "mocha-multi-reporters",
timeout: config.testTimeout || 60000,
slow: config.slow || 10000,
reporterOptions: {
"reporterEnabled": "mocha-junit-reporter, tap",
"mochaJunitReporterReporterOptions": {
"mochaFile": "junit-custom.xml"
}
}
});
mocha.addFile(...)
mocha.run(...)
Note that it will first check if reporterOptions contains a configFile
key, and if it does, use that. That key must not exist in the reporterOptions
object in order to pass these values in directly.
The MIT License (MIT)
Copyright(c) 2019 Yousaf Nabi Copyright(c) 2017 Stanley Ng
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Generate multiple mocha reports in a single mocha execution.
The npm package mocha-multi-reporters receives a total of 451,350 weekly downloads. As such, mocha-multi-reporters popularity was classified as popular.
We found that mocha-multi-reporters demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A survey of 500 cybersecurity pros reveals high pay isn't enough—lack of growth and flexibility is driving attrition and risking organizational security.
Product
Socket, the leader in open source security, is now available on Google Cloud Marketplace for simplified procurement and enhanced protection against supply chain attacks.
Security News
Corepack will be phased out from future Node.js releases following a TSC vote.