diff --git a/lib/assert.js b/lib/assert.js index b4de551e28019f..7fc9e0c342646a 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -330,7 +330,14 @@ function _throws(shouldThrow, block, expected, message) { fail(actual, expected, 'Missing expected exception' + message); } - if (!shouldThrow && expectedException(actual, expected)) { + const userProvidedMessage = typeof message === 'string'; + const isUnwantedException = !shouldThrow && util.isError(actual); + const isUnexpectedException = !shouldThrow && actual && !expected; + + if ((isUnwantedException && + userProvidedMessage && + expectedException(actual, expected)) || + isUnexpectedException) { fail(actual, expected, 'Got unwanted exception' + message); } diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 2f4b757f4ec43e..e537bc3c1d747e 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -321,6 +321,11 @@ assert.throws(function() {assert.ifError(new Error('test error'));}); assert.doesNotThrow(function() {assert.ifError(null);}); assert.doesNotThrow(function() {assert.ifError();}); +assert.throws(() => { + assert.doesNotThrow(makeBlock(thrower, Error), 'user message'); +}, /Got unwanted exception. user message/, + 'a.doesNotThrow ignores user message'); + // make sure that validating using constructor really works threw = false; try {