From ff320f55d197e8fa3ce9cb61819e6ff93c52dbe6 Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Fri, 26 Jul 2024 21:28:48 +0200 Subject: [PATCH] assert: use isError instead of instanceof in innerOk Co-Authored-By: Ruben Bridgewater Co-Authored-By: Nihar Phansalkar --- lib/assert.js | 2 +- test/parallel/test-assert.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/assert.js b/lib/assert.js index 9dfcf80a913942..eadc3844c20128 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -393,7 +393,7 @@ function innerOk(fn, argLen, value, message) { } else if (message == null) { generatedMessage = true; message = getErrMessage(message, fn); - } else if (message instanceof Error) { + } else if (isError(message)) { throw message; } diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 2964a475c81436..1679edb2941808 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -55,6 +55,16 @@ assert.throws(() => a.ok(false), a.AssertionError, 'ok(false)'); assert.ok(threw, 'Error: ok(false)'); } +// Errors created in different contexts are handled as any other custom error +{ + const context = vm.createContext(); + const error = vm.runInContext('new SyntaxError("custom error")', context); + + assert.throws(() => assert(false, error), { + message: 'custom error', + name: 'SyntaxError' + }); +} a(true); a('test', 'ok(\'test\')');