Skip to content

Commit

Permalink
move circular object logic into clean
Browse files Browse the repository at this point in the history
  • Loading branch information
jeversmann authored and boneskull committed Apr 7, 2018
1 parent e56350d commit efd750c
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions lib/reporters/json.js
Expand Up @@ -54,18 +54,7 @@ function JSONReporter (runner) {

runner.testResults = obj;

var cache = [];
process.stdout.write(JSON.stringify(obj, function (key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Instead of going in a circle, we'll print [object Object]
return '' + value;
}
cache.push(value);
}

return value;
}, 2));
process.stdout.write(JSON.stringify(obj, null, 2));
});
}

Expand All @@ -78,17 +67,43 @@ function JSONReporter (runner) {
* @return {Object}
*/
function clean (test) {
var err = test.err || {};
if (err instanceof Error) {
err = errorJSON(err);
}

return {
title: test.title,
fullTitle: test.fullTitle(),
duration: test.duration,
currentRetry: test.currentRetry(),
err: errorJSON(test.err || {})
err: cleanCycles(err)
};
}
/**
* Replaces any circular references inside `obj` with '[object Object]'
*
* @api private
* @param {Object} obj
* @return {Object}
*/
function cleanCycles (obj) {
var cache = [];
return JSON.parse(JSON.stringify(obj, function (key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Instead of going in a circle, we'll print [object Object]
return '' + value;
}
cache.push(value);
}

return value;
}));
}

/**
* Transform `error` into a JSON object.
* Transform an Error object into a JSON object.
*
* @api private
* @param {Error} err
Expand Down

0 comments on commit efd750c

Please sign in to comment.