Skip to content

Commit

Permalink
assert: accommodate ES6 classes that extend Error
Browse files Browse the repository at this point in the history
`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 <[email protected]>
  • Loading branch information
Trott authored and Myles Borins committed Jan 19, 2016
1 parent f61412c commit 87181cd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,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;
}

Expand Down
21 changes: 20 additions & 1 deletion test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,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;

Expand Down

0 comments on commit 87181cd

Please sign in to comment.