Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 68c5683

Browse files
plroebuckboneskull
authored andcommitted
refactor(json-stream.js): Consistent output stream usage (mochajs#3532)
### Description of the Change * Made all output directly use `process.stdout`. * `process.stdout` and `console.log` take different routes to get to same place * Corrected ctor name. * Updated documentation. ### Alternate Designs N/A ### Benefits Consistency. [Don't cross the streams](https://www.youtube.com/watch?v=wyKQe_i9yyo)! ### Possible Drawbacks N/A ### Applicable issues Fixes mochajs#3526 Fixes mochajs#3521 semver-patch
1 parent ec3b4de commit 68c5683

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

lib/reporters/json-stream.js

+32-19
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,68 @@
99
var Base = require('./base');
1010

1111
/**
12-
* Expose `List`.
12+
* Expose `JSONStream`.
1313
*/
1414

15-
exports = module.exports = List;
15+
exports = module.exports = JSONStream;
1616

1717
/**
18-
* Initialize a new `JSONStream` test reporter.
18+
* Constructs a new `JSONStream` reporter instance.
1919
*
2020
* @public
21-
* @name JSONStream
22-
* @class JSONStream
23-
* @memberof Mocha.reporters
21+
* @class
2422
* @extends Mocha.reporters.Base
25-
* @api public
26-
* @param {Runner} runner
23+
* @memberof Mocha.reporters
24+
* @param {Runner} runner - Instance triggers reporter actions.
2725
*/
28-
function List(runner) {
26+
function JSONStream(runner) {
2927
Base.call(this, runner);
3028

3129
var self = this;
3230
var total = runner.total;
3331

34-
runner.on('start', function() {
35-
console.log(JSON.stringify(['start', {total: total}]));
32+
runner.once('start', function() {
33+
writeEvent(['start', {total: total}]);
3634
});
3735

3836
runner.on('pass', function(test) {
39-
console.log(JSON.stringify(['pass', clean(test)]));
37+
writeEvent(['pass', clean(test)]);
4038
});
4139

4240
runner.on('fail', function(test, err) {
4341
test = clean(test);
4442
test.err = err.message;
4543
test.stack = err.stack || null;
46-
console.log(JSON.stringify(['fail', test]));
44+
writeEvent(['fail', test]);
4745
});
4846

4947
runner.once('end', function() {
50-
process.stdout.write(JSON.stringify(['end', self.stats]));
48+
writeEvent(['end', self.stats]);
5149
});
5250
}
5351

5452
/**
55-
* Return a plain-object representation of `test`
56-
* free of cyclic properties etc.
53+
* Mocha event to be written to the output stream.
54+
* @typedef {Array} JSONStream~MochaEvent
55+
*/
56+
57+
/**
58+
* Writes Mocha event to reporter output stream.
59+
*
60+
* @private
61+
* @param {JSONStream~MochaEvent} event - Mocha event to be output.
62+
*/
63+
function writeEvent(event) {
64+
process.stdout.write(JSON.stringify(event) + '\n');
65+
}
66+
67+
/**
68+
* Returns an object literal representation of `test`
69+
* free of cyclic properties, etc.
5770
*
58-
* @api private
59-
* @param {Object} test
60-
* @return {Object}
71+
* @private
72+
* @param {Test} test - Instance used as data source.
73+
* @return {Object} object containing pared-down test instance data
6174
*/
6275
function clean(test) {
6376
return {

0 commit comments

Comments
 (0)