Skip to content

Commit 14fcb1d

Browse files
Ilya ShaisultanovMyles Borins
authored andcommitted
assert: respect assert.doesNotThrow message.
Special handling to detect when user has supplied a custom message. Added a test for user message. When testing if `actual` value is an error use `util.isError` instead of `instanceof`. Fixes: #2385 PR-URL: #2407 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent dc1552e commit 14fcb1d

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎lib/assert.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,14 @@ function _throws(shouldThrow, block, expected, message) {
330330
fail(actual,expected,'Missing expected exception'+message);
331331
}
332332

333-
if(!shouldThrow&&expectedException(actual,expected)){
333+
constuserProvidedMessage=typeofmessage==='string';
334+
constisUnwantedException=!shouldThrow&&util.isError(actual);
335+
constisUnexpectedException=!shouldThrow&&actual&&!expected;
336+
337+
if((isUnwantedException&&
338+
userProvidedMessage&&
339+
expectedException(actual,expected))||
340+
isUnexpectedException){
334341
fail(actual,expected,'Got unwanted exception'+message);
335342
}
336343

‎test/parallel/test-assert.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ assert.throws(function() {assert.ifError(new Error('test error'));});
321321
assert.doesNotThrow(function(){assert.ifError(null);});
322322
assert.doesNotThrow(function(){assert.ifError();});
323323

324+
assert.throws(()=>{
325+
assert.doesNotThrow(makeBlock(thrower,Error),'user message');
326+
},/Gotunwantedexception.usermessage/,
327+
'a.doesNotThrow ignores user message');
328+
324329
// make sure that validating using constructor really works
325330
threw=false;
326331
try{

0 commit comments

Comments
 (0)