diff --git a/lib/assert.js b/lib/assert.js index d9e107688ad4b2..f94bf63cae7edc 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 d37b179899ca70..c7afb2f841b52d 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 {