From 59a977dd22996e8380b7085eed6a0772156a9769 Mon Sep 17 00:00:00 2001 From: Ilya Shaisultanov Date: Sun, 16 Aug 2015 20:35:38 -0700 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/2385 PR-URL: https://github.com/nodejs/node/pull/2407 Reviewed-By: James M Snell --- lib/assert.js | 9 ++++++++- test/parallel/test-assert.js | 5 +++++ 2 files changed, 13 insertions(+), 1 deletion(-) 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 {