From c1d82ac2ff15594840e2a1b9531b506ae067ed27 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 b4de551e28..7fc9e0c342 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 2f4b757f4e..e537bc3c1d 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 {