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

fix circular objects in json reporter. Closes #2433 #2559

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 28 additions & 2 deletions lib/reporters/json.js
Expand Up @@ -67,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
30 changes: 28 additions & 2 deletions mocha.js
Expand Up @@ -3074,17 +3074,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
27 changes: 27 additions & 0 deletions test/reporters/json.spec.js
Expand Up @@ -60,4 +60,31 @@ describe('json reporter', function () {
done();
});
});

it('should handle circular objects in errors', function (done) {
var testTitle = 'json test 1';
function CircleError () {
this.message = 'oh shit';
this.circular = this;
}
var error = new CircleError();

suite.addTest(new Test(testTitle, function (done) {
throw error;
}));

runner.run(function (failureCount) {
failureCount.should.be.exactly(1);
runner.should.have.property('testResults');
runner.testResults.should.have.property('failures');
runner.testResults.failures.should.be.an.instanceOf(Array);
runner.testResults.failures.should.have.a.lengthOf(1);

var failure = runner.testResults.failures[0];
failure.should.have.property('title', testTitle);
failure.should.have.properties('err');

done();
});
});
});