Skip to content

Commit

Permalink
assert: fix generatedMessage
Browse files Browse the repository at this point in the history
The generatedMessage was wrong in case simple assert was used and
a message was auto generated. This fixed the TODO.

PR-URL: #18322
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
BridgeAR committed Feb 6, 2018
1 parent 3e910fb commit a27f48d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
15 changes: 10 additions & 5 deletions lib/assert.js
Expand Up @@ -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.
Expand All @@ -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;
}
}

Expand Down
8 changes: 6 additions & 2 deletions test/parallel/test-assert.js
Expand Up @@ -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}`
}
Expand All @@ -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}`
}
Expand Down Expand Up @@ -1011,7 +1013,8 @@ common.expectsError(
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: '0 == true'
message: '0 == true',
generatedMessage: true
}
);

Expand All @@ -1020,7 +1023,8 @@ common.expectsError(
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: 'test'
message: 'test',
generatedMessage: false
}
);

Expand Down

0 comments on commit a27f48d

Please sign in to comment.