Skip to content

Commit

Permalink
assert: fix assert.fail() stack
Browse files Browse the repository at this point in the history
This makes sure the error message visible in the error stack created
when using `assert.fail()` without any arguments or the message set
to `undefined` or `null` as only argument.
That was masked before due to other changes.

PR-URL: #27525
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
BridgeAR authored and targos committed May 7, 2019
1 parent 678b3be commit e573c99
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
14 changes: 6 additions & 8 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ function innerFail(obj) {
function fail(actual, expected, message, operator, stackStartFn) {
const argsLen = arguments.length;

let internalMessage;
if (argsLen === 0) {
internalMessage = 'Failed';
let internalMessage = false;
if (actual == null && argsLen <= 1) {
internalMessage = true;
message = 'Failed';
} else if (argsLen === 1) {
message = actual;
actual = undefined;
Expand All @@ -118,14 +119,11 @@ function fail(actual, expected, message, operator, stackStartFn) {
actual,
expected,
operator: operator === undefined ? 'fail' : operator,
stackStartFn: stackStartFn || fail
stackStartFn: stackStartFn || fail,
message
};
if (message !== undefined) {
errArgs.message = message;
}
const err = new AssertionError(errArgs);
if (internalMessage) {
err.message = internalMessage;
err.generatedMessage = true;
}
throw err;
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-assert-fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ assert.throws(
operator: 'fail',
actual: undefined,
expected: undefined,
generatedMessage: true
generatedMessage: true,
stack: /Failed/
}
);

Expand Down

0 comments on commit e573c99

Please sign in to comment.