Skip to content

Commit

Permalink
fix(reporter/base): don't assume error message is first line of stack
Browse files Browse the repository at this point in the history
The current implementation assumes that the first line of the err.stack
is the error message. This is not the case for Safari. When using Mocha
with a different HTML generator (e.g. Consolify) this produces weirdly
formatted output with no error message.
  • Loading branch information
mantoni committed Mar 28, 2015
1 parent f8e2b0f commit 8307cb4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,18 @@ exports.list = function(failures){
var err = test.err
, message = err.message || ''
, stack = err.stack || message
, index = stack.indexOf(message) + message.length
, msg = stack.slice(0, index)
, index = stack.indexOf(message)
, actual = err.actual
, expected = err.expected
, escape = true;
if (index === -1) {
msg = message;
} else {
index += message.length;
msg = stack.slice(0, index);
// remove msg from stack
stack = stack.slice(index + 1);
}

// uncaught
if (err.uncaught) {
Expand All @@ -198,9 +205,8 @@ exports.list = function(failures){
}
}

// indent stack trace without msg
stack = stack.slice(index ? index + 1 : index)
.replace(/^/gm, ' ');
// indent stack trace
stack = stack.replace(/^/gm, ' ');

console.log(fmt, (i + 1), test.fullTitle(), msg, stack);
});
Expand Down
28 changes: 28 additions & 0 deletions test/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,32 @@ describe('Base reporter', function () {
errOut.should.match(/expected/);
});

it('should remove message from stack', function () {
var err = {
message: 'Error',
stack: 'Error\nfoo\nbar',
showDiff: false
};
var test = makeTest(err);

Base.list([test]);

var errOut = stdout.join('\n').trim();
errOut.should.equal('1) test title:\n Error\n foo\n bar')
});

it('should not modify stack if it does not contain message', function () {
var err = {
message: 'Error',
stack: 'foo\nbar',
showDiff: false
};
var test = makeTest(err);

Base.list([test]);

var errOut = stdout.join('\n').trim();
errOut.should.equal('1) test title:\n Error\n foo\n bar')
});

});

0 comments on commit 8307cb4

Please sign in to comment.