From e5173408a4d22e6f027906ea97b5e17ab3f2ab14 Mon Sep 17 00:00:00 2001 From: Pietro Marchini Date: Mon, 22 Jul 2024 11:29:55 +0200 Subject: [PATCH] assert: use isError instead of instanceof in innerOk to define message type 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\')');