Skip to content

Commit 171e211

Browse files
dornyjuergba
andauthored
feat(reporter): add output option to 'JSON' (#4607)
Co-authored-by: Juerg B <44573692+juergba@users.noreply.github.com>
1 parent bbf0c11 commit 171e211

File tree

6 files changed

+219
-103
lines changed

6 files changed

+219
-103
lines changed

docs/index.md

+2
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,8 @@ Alias: `JSON`, `json`
19331933

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

1936+
By default, it will output to the console. To write directly to a file, use `--reporter-option output=filename.json`.
1937+
19361938
![json reporter](images/reporter-json.png?withoutEnlargement&resize=920,9999){:class="screenshot" loading="lazy"}
19371939

19381940
### JSON Stream

example/config/.mocharc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = {
3333
parallel: false,
3434
recursive: false,
3535
reporter: 'spec',
36-
'reporter-option': ['foo=bar', 'baz=quux'],
36+
'reporter-option': ['foo=bar', 'baz=quux'], // array, not object
3737
require: '@babel/register',
3838
retries: 1,
3939
slow: '75',

example/config/.mocharc.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ package: './package.json'
3535
parallel: false
3636
recursive: false
3737
reporter: 'spec'
38-
reporter-option:
38+
reporter-option: # array, not object
3939
- 'foo=bar'
4040
- 'baz=quux'
4141
require: '@babel/register'

lib/reporters/base.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ exports.colors = {
9090

9191
exports.symbols = {
9292
ok: symbols.success,
93-
err: symbols.err,
93+
err: symbols.error,
9494
dot: '.',
9595
comma: ',',
9696
bang: '!'

lib/reporters/json.js

+28-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
*/
88

99
var Base = require('./base');
10+
var fs = require('fs');
11+
var path = require('path');
12+
const createUnsupportedError = require('../errors').createUnsupportedError;
13+
const utils = require('../utils');
1014
var constants = require('../runner').constants;
1115
var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
16+
var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
1217
var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
1318
var EVENT_TEST_END = constants.EVENT_TEST_END;
1419
var EVENT_RUN_END = constants.EVENT_RUN_END;
15-
var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
1620

1721
/**
1822
* Expose `JSON`.
@@ -30,14 +34,22 @@ exports = module.exports = JSONReporter;
3034
* @param {Runner} runner - Instance triggers reporter actions.
3135
* @param {Object} [options] - runner options
3236
*/
33-
function JSONReporter(runner, options) {
37+
function JSONReporter(runner, options = {}) {
3438
Base.call(this, runner, options);
3539

3640
var self = this;
3741
var tests = [];
3842
var pending = [];
3943
var failures = [];
4044
var passes = [];
45+
var output;
46+
47+
if (options.reporterOption && options.reporterOption.output) {
48+
if (utils.isBrowser()) {
49+
throw createUnsupportedError('file output not supported in browser');
50+
}
51+
output = options.reporterOption.output;
52+
}
4153

4254
runner.on(EVENT_TEST_END, function(test) {
4355
tests.push(test);
@@ -66,7 +78,20 @@ function JSONReporter(runner, options) {
6678

6779
runner.testResults = obj;
6880

69-
process.stdout.write(JSON.stringify(obj, null, 2));
81+
var json = JSON.stringify(obj, null, 2);
82+
if (output) {
83+
try {
84+
fs.mkdirSync(path.dirname(output), {recursive: true});
85+
fs.writeFileSync(output, json);
86+
} catch (err) {
87+
console.error(
88+
`${Base.symbols.err} [mocha] writing output to "${output}" failed: ${err.message}\n`
89+
);
90+
process.stdout.write(json);
91+
}
92+
} else {
93+
process.stdout.write(json);
94+
}
7095
});
7196
}
7297

0 commit comments

Comments
 (0)