Skip to content

Commit 85cb9bb

Browse files
Ilya ShaisultanovMylesBorins
Ilya Shaisultanov
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 <[email protected]>
1 parent 91001d3 commit 85cb9bb

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/assert.js

+8-1
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+
const userProvidedMessage = typeof message === 'string';
334+
const isUnwantedException = !shouldThrow && util.isError(actual);
335+
const isUnexpectedException = !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

+5
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+
}, /Got unwanted exception. user message/,
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)