Skip to content

Commit

Permalink
Append the cause stacks to the main stack trace
Browse files Browse the repository at this point in the history
It would be great to get the full error stack chain for errors with causes, especially as all current browsers and Node.js >=16 supports it, see eg https://v8.dev/features/error-cause and https://dev.to/voxpelli/pony-cause-1-0-error-causes-2l2o

Eg. `pino` merged support for this as well: pinojs/pino-std-serializers#78
  • Loading branch information
voxpelli committed Feb 27, 2024
1 parent b88978d commit 62671e9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,23 @@ var generateDiff = (exports.generateDiff = function (actual, expected) {
}
});

/**
* This function dumps long stack traces for exceptions having
* an Error cause() method
*
* @public
* @memberof Mocha.reporters.Base
* @param {Error} ex
* @return {string}
*/
exports.getFullErrorStack = function (err, message) {
var ret = err.stack || message || '';
if (err.cause) {
ret += '\n' + ' ' + 'Caused by: ' + exports.getFullErrorStack(err.cause);
}
return ret;
};

/**
* Outputs the given `failures` as a list.
*
Expand Down Expand Up @@ -260,7 +277,7 @@ exports.list = function (failures) {
} else {
message = '';
}
var stack = err.stack || message;
var stack = exports.getFullErrorStack(err, message);
var index = message ? stack.indexOf(message) : -1;

if (index === -1) {
Expand Down
16 changes: 16 additions & 0 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,22 @@ Runner.prototype.fail = function (test, err, force) {
// some environments do not take kindly to monkeying with the stack
}

// Support for error causes
try {
let nextErr = err;

while (nextErr.cause && nextErr.cause.stack) {
nextErr = nextErr.cause;

err.stack +=
'\n' + (this.fullStackTrace || !nextErr.stack)
? nextErr.stack
: stackFilter(nextErr.stack);
}
} catch (ignore) {
// some environments do not take kindly to monkeying with the stack
}

this.emit(constants.EVENT_TEST_FAIL, test, err);
};

Expand Down
26 changes: 26 additions & 0 deletions test/reporters/base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,32 @@ describe('Base reporter', function () {
expect(errOut, 'to be', '1) test title:');
});

it('should append any error cause trail to stack traces', function () {
var err = {
message: 'Error',
stack: 'Error\nfoo\nbar',
showDiff: false,
cause: {
message: 'Cause1',
stack: 'Cause1\nbar\nfoo',
showDiff: false,
cause: {
message: 'Cause2',
stack: 'Cause2\nabc\nxyz',
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\n Caused by: Cause Stack'
);
});

it('should not modify stack if it does not contain message', function () {
var err = {
message: 'Error',
Expand Down

0 comments on commit 62671e9

Please sign in to comment.