From e93878417310b714ba6158d98827863a4095508d Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 14 Jun 2019 10:49:20 +0200 Subject: [PATCH] assert: fix generatedMessage property 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: https://github.com/nodejs/node/pull/31431 PR-URL: https://github.com/nodejs/node/pull/28263 Reviewed-By: Rich Trott Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell --- lib/assert.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 025e5bedc02e27..eb74493c443902 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -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.