diff --git a/doc/api/assert.markdown b/doc/api/assert.markdown index 24685ebf38..a0607e2d3d 100644 --- a/doc/api/assert.markdown +++ b/doc/api/assert.markdown @@ -65,8 +65,8 @@ Custom error validation: throw new Error("Wrong value"); }, function(err) { - if ( !(err instanceof Error) || !/value/.test(err) ) { - return false; + if ( (err instanceof Error) && /value/.test(err) ) { + return true; } }, "unexpected error" diff --git a/lib/assert.js b/lib/assert.js index 3b18f05cb4..b46f28b862 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -246,13 +246,14 @@ function expectedException(actual, expected) { } if (expected instanceof RegExp) { - if (expected.test(actual)) { - return true; - } - } else if (actual instanceof expected || - expected.call({}, actual) !== false) { + return expected.test(actual); + } else if (actual instanceof expected) { + return true; + } else if ( expected.call({}, actual) === true ) { return true; } + + return false; } function _throws(shouldThrow, block, expected, message) { diff --git a/test/simple/test-assert.js b/test/simple/test-assert.js index 4c32840255..e2aa645d4a 100644 --- a/test/simple/test-assert.js +++ b/test/simple/test-assert.js @@ -165,12 +165,26 @@ assert.throws(function() {assert.ifError(new Error('test error'))}); assert.doesNotThrow(function() {assert.ifError(null)}); assert.doesNotThrow(function() {assert.ifError()}); +// make sure that validating using constructor really works +threw = false; +try { + assert.throws( + function() { + throw {}; + }, + Array + ); +} catch(e) { + threw = true; +} +assert.ok(threw, "wrong constructor validation"); + // use a RegExp to validate error message a.throws(makeBlock(thrower, TypeError), /test/); // use a fn to validate error object a.throws(makeBlock(thrower, TypeError), function(err) { - if (!(err instanceof TypeError) || !/test/.test(err)) { - return false; + if ( (err instanceof TypeError) && /test/.test(err)) { + return true; } });