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 <jasnell@gmail.com>
This commit is contained in:
Ilya Shaisultanov 2015-08-16 20:35:38 -07:00 committed by James M Snell
parent 9a9beefe23
commit c1d82ac2ff
2 changed files with 13 additions and 1 deletions

View File

@ -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);
}

View File

@ -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 {