From c629d7263dbb10a58cf843822637e7bd36566355 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. Addresses #2385. 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`. --- lib/assert.js | 6 +++++- test/parallel/test-assert.js | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/assert.js b/lib/assert.js index ea142ed01f8f6e..fcb5c17c5979d2 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -302,7 +302,11 @@ function _throws(shouldThrow, block, expected, message) { fail(actual, expected, 'Missing expected exception' + message); } - if (!shouldThrow && expectedException(actual, expected)) { + if (!shouldThrow && + actual instanceof Error && + typeof message === 'string' && + expectedException(actual, expected) || + !shouldThrow && actual && !expected) { fail(actual, expected, 'Got unwanted exception' + message); } diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index ce84eabc34e1d4..420ac529c27ea8 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -322,6 +322,13 @@ assert.throws(function() {assert.ifError(new Error('test error'));}); assert.doesNotThrow(function() {assert.ifError(null);}); assert.doesNotThrow(function() {assert.ifError();}); +try { + assert.doesNotThrow(makeBlock(thrower, Error), 'user message'); +} catch(e) { + assert.equal(e.message, 'Got unwanted exception. user message', + 'a.doesNotThrow ignores user message'); +} + // make sure that validating using constructor really works threw = false; try {