☕️ Classic, reliable, trusted test framework for Node.js and the browser
refactor(json-stream.js): Consistent output stream usage (#3532)
P. Roebuckb9b3ac012234
diff --git a/lib/reporters/json-stream.js b/lib/reporters/json-stream.jsindex 0edd0cbf..5b4cfbce 100644--- a/lib/reporters/json-stream.js+++ b/lib/reporters/json-stream.js@@ -9,55 +9,68 @@var Base = require('./base');/**- * Expose `List`.+ * Expose `JSONStream`.*/-exports = module.exports = List;+exports = module.exports = JSONStream;/**- * Initialize a new `JSONStream` test reporter.+ * Constructs a new `JSONStream` reporter instance.** @public- * @name JSONStream- * @class JSONStream- * @memberof Mocha.reporters+ * @class* @extends Mocha.reporters.Base- * @api public- * @param {Runner} runner+ * @memberof Mocha.reporters+ * @param {Runner} runner - Instance triggers reporter actions.*/-function List(runner) {+function JSONStream(runner) {Base.call(this, runner);var self = this;var total = runner.total;- runner.on('start', function() {- console.log(JSON.stringify(['start', {total: total}]));+ runner.once('start', function() {+ writeEvent(['start', {total: total}]);});runner.on('pass', function(test) {- console.log(JSON.stringify(['pass', clean(test)]));+ writeEvent(['pass', clean(test)]);});runner.on('fail', function(test, err) {test = clean(test);test.err = err.message;test.stack = err.stack || null;- console.log(JSON.stringify(['fail', test]));+ writeEvent(['fail', test]);});runner.once('end', function() {- process.stdout.write(JSON.stringify(['end', self.stats]));+ writeEvent(['end', self.stats]);});}/**- * Return a plain-object representation of `test`- * free of cyclic properties etc.+ * Mocha event to be written to the output stream.+ * @typedef {Array} JSONStream~MochaEvent+ */++/**+ * Writes Mocha event to reporter output stream.+ *+ * @private+ * @param {JSONStream~MochaEvent} event - Mocha event to be output.+ */+function writeEvent(event) {+ process.stdout.write(JSON.stringify(event) + '\n');+}++/**+ * Returns an object literal representation of `test`+ * free of cyclic properties, etc.*- * @api private- * @param {Object} test- * @return {Object}+ * @private+ * @param {Test} test - Instance used as data source.+ * @return {Object} object containing pared-down test instance data*/function clean(test) {return {