Skip to content

Commit 58b5f09

Browse files
committed
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: nodejs#3188 PR-URL: nodejs#4166 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 7e1564d commit 58b5f09

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/assert.js

+4
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,10 @@ function expectedException(actual, expected) {
283283
// Ignore. The instanceof check doesn't work for arrow functions.
284284
}
285285

286+
if (Error.isPrototypeOf(expected)) {
287+
return false;
288+
}
289+
286290
return expected.call({}, actual) === true;
287291
}
288292

test/parallel/test-assert.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,28 @@ a.throws(makeBlock(thrower, TypeError), function(err) {
342342
}
343343
});
344344

345+
// https://github.com/nodejs/node/issues/3188
346+
threw = false;
345347

346-
// GH-207. Make sure deepEqual doesn't loop forever on circular refs
348+
try {
349+
var ES6Error = class extends Error {};
350+
351+
var AnotherErrorType = class extends Error {};
347352

353+
const functionThatThrows = function() {
354+
throw new AnotherErrorType('foo');
355+
};
356+
357+
assert.throws(functionThatThrows, ES6Error);
358+
} catch (e) {
359+
threw = true;
360+
assert(e instanceof AnotherErrorType,
361+
`expected AnotherErrorType, received ${e}`);
362+
}
363+
364+
assert.ok(threw);
365+
366+
// GH-207. Make sure deepEqual doesn't loop forever on circular refs
348367
var b = {};
349368
b.b = b;
350369

0 commit comments

Comments
 (0)