Skip to content

Commit

Permalink
assert: add default operator to assert.fail()
Browse files Browse the repository at this point in the history
This makes sure `assert.fail()` contains an operator instead of being
undefined.

On top of that it also fixes the `err.generatedMessage` property.
Before, it was not always set correct.

PR-URL: #22694
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
BridgeAR authored and targos committed Sep 19, 2018
1 parent dfef9a9 commit 5cb642e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
21 changes: 16 additions & 5 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ function innerFail(obj) {
function fail(actual, expected, message, operator, stackStartFn) {
const argsLen = arguments.length;

let internalMessage;
if (argsLen === 0) {
message = 'Failed';
internalMessage = 'Failed';
} else if (argsLen === 1) {
message = actual;
actual = undefined;
Expand All @@ -105,13 +106,23 @@ function fail(actual, expected, message, operator, stackStartFn) {
operator = '!=';
}

innerFail({
if (message instanceof Error) throw message;

const errArgs = {
actual,
expected,
message,
operator,
operator: operator === undefined ? 'fail' : operator,
stackStartFn: stackStartFn || fail
});
};
if (message !== undefined) {
errArgs.message = message;
}
const err = new AssertionError(errArgs);
if (internalMessage) {
err.message = internalMessage;
err.generatedMessage = true;
}
throw err;
}

assert.fail = fail;
Expand Down
8 changes: 5 additions & 3 deletions test/parallel/test-assert-fail-deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ assert.throws(() => {
message: '\'first\' != \'second\'',
operator: '!=',
actual: 'first',
expected: 'second'
expected: 'second',
generatedMessage: true
});

// Three args
Expand All @@ -29,9 +30,10 @@ assert.throws(() => {
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
message: 'another custom message',
operator: undefined,
operator: 'fail',
actual: 'ignored',
expected: 'ignored'
expected: 'ignored',
generatedMessage: false
});

// Three args with custom Error
Expand Down
10 changes: 6 additions & 4 deletions test/parallel/test-assert-fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ assert.throws(
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
message: 'Failed',
operator: undefined,
operator: 'fail',
actual: undefined,
expected: undefined
expected: undefined,
generatedMessage: true
}
);

Expand All @@ -23,9 +24,10 @@ assert.throws(() => {
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
message: 'custom message',
operator: undefined,
operator: 'fail',
actual: undefined,
expected: undefined
expected: undefined,
generatedMessage: false
});

// One arg = Error
Expand Down

0 comments on commit 5cb642e

Please sign in to comment.