Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add output option to JSON reporter (#4131) #4607

Merged
merged 2 commits into from Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/index.md
Expand Up @@ -1933,6 +1933,8 @@ Alias: `JSON`, `json`

The JSON reporter outputs a single large JSON object when the tests have completed (failures or not).

By default, it will output to the console. To write directly to a file, use `--reporter-option output=filename.json`.

![json reporter](images/reporter-json.png?withoutEnlargement&resize=920,9999){:class="screenshot" loading="lazy"}

### JSON Stream
Expand Down
2 changes: 1 addition & 1 deletion example/config/.mocharc.js
Expand Up @@ -33,7 +33,7 @@ module.exports = {
parallel: false,
recursive: false,
reporter: 'spec',
'reporter-option': ['foo=bar', 'baz=quux'],
'reporter-option': ['foo=bar', 'baz=quux'], // array, not object
require: '@babel/register',
retries: 1,
slow: '75',
Expand Down
2 changes: 1 addition & 1 deletion example/config/.mocharc.yml
Expand Up @@ -35,7 +35,7 @@ package: './package.json'
parallel: false
recursive: false
reporter: 'spec'
reporter-option:
reporter-option: # array, not object
- 'foo=bar'
- 'baz=quux'
require: '@babel/register'
Expand Down
2 changes: 1 addition & 1 deletion lib/reporters/base.js
Expand Up @@ -90,7 +90,7 @@ exports.colors = {

exports.symbols = {
ok: symbols.success,
err: symbols.err,
err: symbols.error,
dot: '.',
comma: ',',
bang: '!'
Expand Down
31 changes: 28 additions & 3 deletions lib/reporters/json.js
Expand Up @@ -7,12 +7,16 @@
*/

var Base = require('./base');
var fs = require('fs');
var path = require('path');
const createUnsupportedError = require('../errors').createUnsupportedError;
const utils = require('../utils');
var constants = require('../runner').constants;
var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
var EVENT_TEST_END = constants.EVENT_TEST_END;
var EVENT_RUN_END = constants.EVENT_RUN_END;
var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;

/**
* Expose `JSON`.
Expand All @@ -30,14 +34,22 @@ exports = module.exports = JSONReporter;
* @param {Runner} runner - Instance triggers reporter actions.
* @param {Object} [options] - runner options
*/
function JSONReporter(runner, options) {
function JSONReporter(runner, options = {}) {
Base.call(this, runner, options);

var self = this;
var tests = [];
var pending = [];
var failures = [];
var passes = [];
var output;

if (options.reporterOption && options.reporterOption.output) {
if (utils.isBrowser()) {
throw createUnsupportedError('file output not supported in browser');
}
output = options.reporterOption.output;
}

runner.on(EVENT_TEST_END, function(test) {
tests.push(test);
Expand Down Expand Up @@ -66,7 +78,20 @@ function JSONReporter(runner, options) {

runner.testResults = obj;

process.stdout.write(JSON.stringify(obj, null, 2));
var json = JSON.stringify(obj, null, 2);
if (output) {
try {
fs.mkdirSync(path.dirname(output), {recursive: true});
fs.writeFileSync(output, json);
} catch (err) {
console.error(
`${Base.symbols.err} [mocha] writing output to "${output}" failed: ${err.message}\n`
);
process.stdout.write(json);
}
} else {
process.stdout.write(json);
}
});
}

Expand Down