Browse Source

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>
process-exit-stdio-flushing
Ilya Shaisultanov 9 years ago
committed by James M Snell
parent
commit
c1d82ac2ff
  1. 9
      lib/assert.js
  2. 5
      test/parallel/test-assert.js

9
lib/assert.js

@ -330,7 +330,14 @@ function _throws(shouldThrow, block, expected, message) {
fail(actual, expected, 'Missing expected exception' + 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); fail(actual, expected, 'Got unwanted exception' + message);
} }

5
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(null);});
assert.doesNotThrow(function() {assert.ifError();}); 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 // make sure that validating using constructor really works
threw = false; threw = false;
try { try {

Loading…
Cancel
Save