Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Issue 1294 - JSON reporter missing error reason

Diogo Monteiro6e8058f54de2
diff --git a/lib/reporters/json.js b/lib/reporters/json.js
index e202692f..2b4896d0 100644
--- a/lib/reporters/json.js
+++ b/lib/reporters/json.js
@@ -36,8 +36,12 @@ function JSONReporter(runner) {
passes.push(test);
});
- runner.on('fail', function(test){
+ runner.on('fail', function(test, err){
failures.push(test);
+ if (err === Object(err)) {
+ test.errMsg = err.message;
+ test.errStack = err.stack;
+ }
});
runner.on('end', function(){
@@ -47,6 +51,7 @@ function JSONReporter(runner) {
failures: failures.map(clean),
passes: passes.map(clean)
};
+ runner.testResults = obj;
process.stdout.write(JSON.stringify(obj, null, 2));
});
@@ -66,6 +71,8 @@ function clean(test) {
title: test.title,
fullTitle: test.fullTitle(),
duration: test.duration,
- err: test.err
+ err: test.err,
+ errStack: test.err.stack,
+ errMessage: test.err.message
}
}
diff --git a/lib/runnable.js b/lib/runnable.js
index 388b1f31..b0bb1a9b 100644
--- a/lib/runnable.js
+++ b/lib/runnable.js
@@ -182,7 +182,8 @@ Runnable.prototype.run = function(fn){
, finished
, emitted;
- if (ctx) ctx.runnable(this);
+ // Some times the ctx exists but it is not runnable
+ if (ctx && ctx.runnable) ctx.runnable(this);
// called multiple times
function multiple(err) {
diff --git a/mocha.js b/mocha.js
index 80ee52cb..079aa583 100644
--- a/mocha.js
+++ b/mocha.js
@@ -3111,8 +3111,12 @@ function JSONReporter(runner) {
passes.push(test);
});
- runner.on('fail', function(test){
+ runner.on('fail', function(test, err){
failures.push(test);
+ if (err === Object(err)) {
+ test.errMsg = err.message;
+ test.errStack = err.stack;
+ }
});
runner.on('end', function(){
@@ -3122,6 +3126,7 @@ function JSONReporter(runner) {
failures: failures.map(clean),
passes: passes.map(clean)
};
+ runner.testResults = obj;
process.stdout.write(JSON.stringify(obj, null, 2));
});
@@ -3141,7 +3146,9 @@ function clean(test) {
title: test.title,
fullTitle: test.fullTitle(),
duration: test.duration,
- err: test.err
+ err: test.err,
+ errStack: test.err.stack,
+ errMessage: test.err.message
}
}
@@ -4314,7 +4321,8 @@ Runnable.prototype.run = function(fn){
, finished
, emitted;
- if (ctx) ctx.runnable(this);
+ // Some times the ctx exists but it is not runnable
+ if (ctx && ctx.runnable) ctx.runnable(this);
// called multiple times
function multiple(err) {
diff --git a/test/reporters/json.js b/test/reporters/json.js
new file mode 100644
index 00000000..2991869f
--- /dev/null
+++ b/test/reporters/json.js
@@ -0,0 +1,40 @@
+
+var Mocha = require('../../')
+ , Suite = Mocha.Suite
+ , Runner = Mocha.Runner
+ , Test = Mocha.Test;
+
+describe('Runner', function(){
+ var suite, runner;
+
+ beforeEach(function(){
+ var mocha = new Mocha({
+ reporter: 'json'
+ });
+ suite = new Suite('JSON suite', 'root');
+ runner = new Runner(suite);
+ var mochaReporter = new mocha._reporter(runner);
+ })
+
+ it('should have 1 test failure', function(done){
+ var testTitle = 'json test 1';
+ suite.addTest(new Test(testTitle, function (done) {
+ done(testTitle);
+ }));
+
+ 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', 'errStack', 'errMessage');
+ failure.errMessage.should.endWith(testTitle);
+
+ done();
+ });
+ })
+
+})