diff --git a/lib/assert.js b/lib/assert.js index 7f69ba0c60e476..4629c883e00091 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -341,8 +341,8 @@ function _throws(shouldThrow, block, expected, message) { actual = _tryBlock(block); - message = (expected && expected.name ? ' (' + expected.name + ').' : '.') + - (message ? ' ' + message : '.'); + message = (expected && expected.name ? ' (' + expected.name + ')' : '') + + (message ? ': ' + message : '.'); if (shouldThrow && !actual) { fail(actual, expected, 'Missing expected exception' + message); diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index 8d0e336aad844d..8178d5dc249981 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -490,6 +490,29 @@ a.throws(makeBlock(a.deepEqual, args, [])); a.doesNotThrow(makeBlock(a.deepEqual, someArgs, sameArgs)); } +// check messages from assert.throws() +{ + assert.throws( + () => { a.throws(() => {}); }, + /^AssertionError: Missing expected exception\.$/ + ); + + assert.throws( + () => { a.throws(() => {}, TypeError); }, + /^AssertionError: Missing expected exception \(TypeError\)\.$/ + ); + + assert.throws( + () => { a.throws(() => {}, 'fhqwhgads'); }, + /^AssertionError: Missing expected exception: fhqwhgads$/ + ); + + assert.throws( + () => { a.throws(() => {}, TypeError, 'fhqwhgads'); }, + /^AssertionError: Missing expected exception \(TypeError\): fhqwhgads$/ + ); +} + const circular = {y: 1}; circular.x = circular; @@ -535,7 +558,7 @@ testAssertionMessage({a: NaN, b: Infinity, c: -Infinity}, }); } catch (e) { threw = true; - assert.strictEqual(e.message, 'Missing expected exception..'); + assert.strictEqual(e.message, 'Missing expected exception.'); } assert.ok(threw); }