summaryrefslogtreecommitdiff
path: root/lib/assert.js
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2019-06-15 13:54:50 +0200
committerRuben Bridgewater <ruben@bridgewater.de>2019-10-01 22:56:01 +0200
commit48d1ea5e7fc918ae9f74d8472785720a13a755e6 (patch)
treefe7b3d2dadacf175b3ca6959b545f02ae347df49 /lib/assert.js
parent97c52ca5dc0b9c4df015dc36ef394ebe06ec00c6 (diff)
downloadnode-new-48d1ea5e7fc918ae9f74d8472785720a13a755e6.tar.gz
assert: special handle identical error names in instance checks
This makes sure that using `assert.throws()` or `assert.rejects()` in combination with Error classes log appropriate error messages in case the expected and received constructor name are identical but not part of the same prototype chain. PR-URL: https://github.com/nodejs/node/pull/28263 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/assert.js')
-rw-r--r--lib/assert.js10
1 files changed, 7 insertions, 3 deletions
diff --git a/lib/assert.js b/lib/assert.js
index 0b256e604e..94104ee881 100644
--- a/lib/assert.js
+++ b/lib/assert.js
@@ -620,10 +620,14 @@ function expectedException(actual, expected, message, fn) {
generatedMessage = true;
message = 'The error is expected to be an instance of ' +
`"${expected.name}". Received `;
- // TODO: Special handle identical names.
if (isError(actual)) {
- const name = actual.constructor && actual.constructor.name;
- message += `"${name || actual.name}"`;
+ const name = actual.constructor && actual.constructor.name ||
+ actual.name;
+ if (expected.name === name) {
+ message += 'an error with identical name but a different prototype.';
+ } else {
+ message += `"${name}"`;
+ }
if (actual.message) {
message += `\n\nError message:\n\n${actual.message}`;
}