From 3f7bbdecb27094044e5027c1681b115008fd8c45 Mon Sep 17 00:00:00 2001 From: Rich Trott <rtrott@gmail.com> Date: Sat, 5 Dec 2015 17:29:28 -0800 Subject: [PATCH] assert: accommodate ES6 classes that extend Error `assert.throws()` and `assert.doesNotThrow()` blow up with a `TypeError` if used with an ES6 class that extends Error. Fixes: https://github.com/nodejs/node/issues/3188 --- lib/assert.js | 4 ++++ test/parallel/test-assert.js | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/assert.js b/lib/assert.js index f8e4920cf38959..09353556f51c29 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -278,6 +278,10 @@ function expectedException(actual, expected) { // Ignore. The instanceof check doesn't work for arrow functions. } + if (Error.isPrototypeOf(expected)) { + return false; + } + return expected.call({}, actual) === true; } diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 0b300e737b367d..46678247585243 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -344,9 +344,28 @@ a.throws(makeBlock(thrower, TypeError), function(err) { } }); +// https://github.com/nodejs/node/issues/3188 +threw = false; -// GH-207. Make sure deepEqual doesn't loop forever on circular refs +try { + var ES6Error = class extends Error {}; + + var AnotherErrorType = class extends Error {}; + const functionThatThrows = function() { + throw new AnotherErrorType('foo'); + }; + + assert.throws(functionThatThrows, ES6Error); +} catch (e) { + threw = true; + assert(e instanceof AnotherErrorType, + `expected AnotherErrorType, received ${e}`); +} + +assert.ok(threw); + +// GH-207. Make sure deepEqual doesn't loop forever on circular refs var b = {}; b.b = b;