Skip to content

Commit

Permalink
add t.throws(fn, Function)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmoore committed Jan 1, 2015
1 parent 00835d8 commit 13efd54
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
12 changes: 9 additions & 3 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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;
Expand Down Expand Up @@ -399,11 +399,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;
Expand All @@ -417,6 +418,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',
Expand Down
2 changes: 1 addition & 1 deletion readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,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)

Expand Down
20 changes: 20 additions & 0 deletions test/throws.js
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit 13efd54

Please sign in to comment.