Skip to content

Commit da5cdc2

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 dfc8bed commit da5cdc2

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
@@ -278,6 +278,10 @@ function expectedException(actual, expected) {
278278
// Ignore. The instanceof check doesn't work for arrow functions.
279279
}
280280

281+
if (Error.isPrototypeOf(expected)) {
282+
return false;
283+
}
284+
281285
return expected.call({}, actual) === true;
282286
}
283287

test/parallel/test-assert.js

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

347+
// https://github.com/nodejs/node/issues/3188
348+
threw = false;
347349

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

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

0 commit comments

Comments
 (0)