From c53db1e8e9bb65779d791046daa39ed88c8f1045 Mon Sep 17 00:00:00 2001 From: Ruslan Bekenev Date: Sun, 2 Apr 2017 20:11:42 +0300 Subject: [PATCH] assert: show thrown message in doesNotThrow() assert.doesNotThrow() should show actual error message instead of "Got unwanted exception" which is not really helpful. PR-URL: https://github.com/nodejs/node/pull/12167 Reviewed-By: Anna Henningsen Reviewed-By: Yuta Hiroto Reviewed-By: James M Snell Reviewed-By: Joyee Cheung Reviewed-By: Refael Ackermann --- lib/assert.js | 5 ++++- test/parallel/test-assert.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/assert.js b/lib/assert.js index b66061b29b0614..a948028501adfc 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -546,7 +546,10 @@ function innerThrows(shouldThrow, block, expected, message) { } else if (actual !== undefined) { if (!expected || expectedException(actual, expected)) { details = message ? `: ${message}` : '.'; - fail(actual, expected, `Got unwanted exception${details}`, fail); + fail(actual, + expected, + `Got unwanted exception${details}\n${actual.message}`, + fail); } throw actual; } diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index d4a1b277bcc195..348927cd5c6a1b 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -468,6 +468,34 @@ assert.throws(() => { }, /Got unwanted exception: user message/, 'a.doesNotThrow ignores user message'); +{ + let threw = false; + try { + assert.doesNotThrow(makeBlock(thrower, Error), 'user message'); + } catch (e) { + threw = true; + common.expectsError({ + code: 'ERR_ASSERTION', + message: /Got unwanted exception: user message\n\[object Object\]/ + })(e); + } + assert.ok(threw); +} + +{ + let threw = false; + try { + assert.doesNotThrow(makeBlock(thrower, Error)); + } catch (e) { + threw = true; + common.expectsError({ + code: 'ERR_ASSERTION', + message: /Got unwanted exception\.\n\[object Object\]/ + })(e); + } + assert.ok(threw); +} + // make sure that validating using constructor really works { let threw = false;