assert: fix generatedMessage

The generatedMessage was wrong in case simple assert was used and
a message was auto generated. This fixed the TODO.

PR-URL: https://github.com/nodejs/node/pull/18322
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
Ruben Bridgewater 2018-01-31 15:43:09 +01:00
parent 3e910fb8f7
commit a27f48d619
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
2 changed files with 16 additions and 7 deletions

View File

@ -204,7 +204,10 @@ function innerOk(args, fn) {
var [value, message] = args;
if (!value) {
let generatedMessage = false;
if (args.length === 0) {
generatedMessage = true;
message = 'No value argument passed to `assert.ok()`';
} else if (message == null) {
// Use the call as error message if possible.
@ -222,20 +225,22 @@ function innerOk(args, fn) {
const call = err.stack[0];
Error.prepareStackTrace = tmpPrepare;
// TODO(BridgeAR): fix the "generatedMessage property"
// Since this is actually a generated message, it has to be
// determined differently from now on.
// Make sure it would be "null" in case that is used.
message = getErrMessage(call) || message;
generatedMessage = true;
} else if (message instanceof Error) {
throw message;
}
innerFail({
const err = new AssertionError({
actual: value,
expected: true,
message,
operator: '==',
stackStartFn: fn
});
err.generatedMessage = generatedMessage;
throw err;
}
}

View File

@ -919,6 +919,7 @@ common.expectsError(
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
generatedMessage: true,
message: `The expression evaluated to a falsy value:${EOL}${EOL} ` +
`assert.ok(null)${EOL}`
}
@ -928,6 +929,7 @@ common.expectsError(
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
generatedMessage: true,
message: `The expression evaluated to a falsy value:${EOL}${EOL} ` +
`assert(typeof 123 === 'string')${EOL}`
}
@ -1011,7 +1013,8 @@ common.expectsError(
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: '0 == true'
message: '0 == true',
generatedMessage: true
}
);
@ -1020,7 +1023,8 @@ common.expectsError(
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: 'test'
message: 'test',
generatedMessage: false
}
);