From 0b3242c3ce0ff2cdf9c740f45f3755a683073c32 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. 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 035a7e1746d127..1beb33265d6cd3 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -571,14 +571,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.