From e56350dc93ec1503f17f9b3972e505d2468d8ad3 Mon Sep 17 00:00:00 2001 From: Josh Eversmann Date: Fri, 28 Oct 2016 16:04:19 -0500 Subject: [PATCH 1/3] fix circular objects in json reporter. Closes #2433 --- lib/reporters/json.js | 13 ++++++++++++- test/reporters/json.spec.js | 27 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/reporters/json.js b/lib/reporters/json.js index 97989ccbb3..aefde256b1 100644 --- a/lib/reporters/json.js +++ b/lib/reporters/json.js @@ -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)); }); } diff --git a/test/reporters/json.spec.js b/test/reporters/json.spec.js index d34105ff89..28f8eb3457 100644 --- a/test/reporters/json.spec.js +++ b/test/reporters/json.spec.js @@ -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(); + }); + }); }); From efd750cb006f577677cd875ad43b2045f9f9a648 Mon Sep 17 00:00:00 2001 From: Josh Eversmann Date: Thu, 3 Nov 2016 09:43:33 -0500 Subject: [PATCH 2/3] move circular object logic into clean --- lib/reporters/json.js | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/lib/reporters/json.js b/lib/reporters/json.js index aefde256b1..c0f808dbe3 100644 --- a/lib/reporters/json.js +++ b/lib/reporters/json.js @@ -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)); }); } @@ -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 From 96a28465e364de8d85c29911978226f73516d680 Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Sat, 7 Apr 2018 21:12:26 -0700 Subject: [PATCH 3/3] fix more assertions Signed-off-by: Christopher Hiller --- lib/reporters/json.js | 1 + test/reporters/json.spec.js | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/reporters/json.js b/lib/reporters/json.js index c0f808dbe3..efc79b4b02 100644 --- a/lib/reporters/json.js +++ b/lib/reporters/json.js @@ -80,6 +80,7 @@ function clean (test) { err: cleanCycles(err) }; } + /** * Replaces any circular references inside `obj` with '[object Object]' * diff --git a/test/reporters/json.spec.js b/test/reporters/json.spec.js index 28f8eb3457..a40eead75f 100644 --- a/test/reporters/json.spec.js +++ b/test/reporters/json.spec.js @@ -72,15 +72,15 @@ describe('json reporter', function () { })); 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); + expect(failureCount).to.equal(1); + expect(runner).to.have.property('testResults'); + expect(runner.testResults).to.have.property('failures'); + expect(runner.testResults.failures).to.be.an(Array); + expect(runner.testResults.failures).to.have.length(1); var failure = runner.testResults.failures[0]; - failure.should.have.property('title', testTitle); - failure.should.have.properties('err'); + expect(failure).to.have.property('title', testTitle); + expect(failure).to.have.property('err'); done(); });