diff --git a/lib/test.js b/lib/test.js index c0322455..b9d6111b 100644 --- a/lib/test.js +++ b/lib/test.js @@ -17,7 +17,7 @@ var getTestArgs = function (name_, opts_, cb_) { var name = '(anonymous)'; var opts = {}; var cb; - + for (var i = 0; i < arguments.length; i++) { var arg = arguments[i]; var t = typeof arg; @@ -426,11 +426,12 @@ Test.prototype['throws'] = function (fn, expected, msg, extra) { msg = expected; expected = undefined; } + var caught = undefined; + try { fn(); - } - catch (err) { + } catch (err) { caught = { error : err }; var message = err.message; delete err.message; @@ -444,6 +445,11 @@ Test.prototype['throws'] = function (fn, expected, msg, extra) { expected = String(expected); } + if (typeof expected === 'function') { + passed = caught.error instanceof expected; + caught.error = caught.error.constructor; + } + this._assert(passed, { message : defined(msg, 'should throw'), operator : 'throws', diff --git a/readme.markdown b/readme.markdown index 030d5c49..7a8422b5 100644 --- a/readme.markdown +++ b/readme.markdown @@ -194,7 +194,7 @@ Aliases: `t.notLooseEqual()`, `t.notLooseEquals()` ## t.throws(fn, expected, msg) -Assert that the function call `fn()` throws an exception. `expected`, if present, must be a `RegExp`. +Assert that the function call `fn()` throws an exception. `expected`, if present, must be a `RegExp` or `Function`. ## t.doesNotThrow(fn, expected, msg) diff --git a/test/throws.js b/test/throws.js new file mode 100644 index 00000000..ec91fb8d --- /dev/null +++ b/test/throws.js @@ -0,0 +1,20 @@ +var test = require('../'); + +function fn() { + throw new TypeError('RegExp'); +} + +test('throws', function (t) { + t.throws(fn); + t.end(); +}); + +test('throws (RegExp match)', function (t) { + t.throws(fn, /RegExp/); + t.end(); +}); + +test('throws (Function match)', function (t) { + t.throws(fn, TypeError); + t.end(); +});