Skip to content

Commit

Permalink
assert: fix generatedMessage property
Browse files Browse the repository at this point in the history
This makes sure the `generatedMessage` property is always set as
expected. This was not the case some `assert.throws` and
`assert.rejects` calls.

Backport-PR-URL: #31431
PR-URL: #28263
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
BridgeAR authored and BethGriggs committed Feb 6, 2020
1 parent a1d36db commit 749bc16
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/assert.js
Expand Up @@ -578,14 +578,21 @@ function expectedException(actual, expected, message, fn) {
if (expected.test(str))
return;

throw new AssertionError({
if (!message) {
generatedMessage = true;
message = 'The input did not match the regular expression ' +
`${inspect(expected)}. Input:\n\n${inspect(str)}\n`;
}

const err = new AssertionError({
actual,
expected,
message: message || 'The input did not match the regular expression ' +
`${inspect(expected)}. Input:\n\n${inspect(str)}\n`,
message,
operator: fn.name,
stackStartFn: fn
});
err.generatedMessage = generatedMessage;
throw err;
}

// Handle primitives properly.
Expand Down

0 comments on commit 749bc16

Please sign in to comment.