Skip to content

Commit 87181cd

Browse files
TrottMyles Borins
authored andcommitted
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: #3188 PR-URL: #4166 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent f61412c commit 87181cd

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎lib/assert.js‎

Lines changed: 4 additions & 0 deletions
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+
returnfalse;
288+
}
289+
286290
returnexpected.call({},actual)===true;
287291
}
288292

‎test/parallel/test-assert.js‎

Lines changed: 20 additions & 1 deletion
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+
varES6Error=classextendsError{};
350+
351+
varAnotherErrorType=classextendsError{};
347352

353+
constfunctionThatThrows=function(){
354+
thrownewAnotherErrorType('foo');
355+
};
356+
357+
assert.throws(functionThatThrows,ES6Error);
358+
}catch(e){
359+
threw=true;
360+
assert(einstanceofAnotherErrorType,
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
varb={};
349368
b.b=b;
350369

0 commit comments

Comments
 (0)