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

refactor(json-stream.js): Consistent output stream usage #3532

Merged
merged 2 commits into from
Oct 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions lib/reporters/json-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down