diff --git a/doc/api.markdown b/doc/api.markdown index 6afc4661af..c2467a15bc 100644 --- a/doc/api.markdown +++ b/doc/api.markdown @@ -2546,6 +2546,9 @@ Expects `block` to throw an error. Expects `block` not to throw an error. +### assert.ifError(value) + +Tests if value is not a false value, throws if it is a true value. Useful when testing the first argument, `error` in callbacks. ## Path diff --git a/lib/assert.js b/lib/assert.js index da9198a8d8..2d1fc08001 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -284,3 +284,4 @@ assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) { _throws.apply(this, [false].concat(pSlice.call(arguments))); }; +assert.ifError = function (err) { if (err) {throw err;}}; diff --git a/test/simple/test-assert.js b/test/simple/test-assert.js index 610236856a..10aeb5cc58 100644 --- a/test/simple/test-assert.js +++ b/test/simple/test-assert.js @@ -154,3 +154,6 @@ try { } assert.equal(true,threw,'a.doesNotThrow is not catching type matching errors'); +assert.throws(function () {assert.ifError(new Error('test error'))}); +assert.doesNotThrow(function(){assert.ifError(null)}); +assert.doesNotThrow(function(){assert.ifError()});