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 4c4073699f0548..26d239e8889b26 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\')');