Skip to content

Commit

Permalink
fix circular objects in json reporter. Closes #2433
Browse files Browse the repository at this point in the history
  • Loading branch information
jeversmann authored and boneskull committed Apr 7, 2018
1 parent 94e7289 commit e56350d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/reporters/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,18 @@ function JSONReporter (runner) {

runner.testResults = obj;

process.stdout.write(JSON.stringify(obj, null, 2));
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));
});
}

Expand Down
27 changes: 27 additions & 0 deletions test/reporters/json.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,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();
});
});
});

0 comments on commit e56350d

Please sign in to comment.