Skip to content

Commit

Permalink
Merge pull request #576 from kasperlewau/throws-return
Browse files Browse the repository at this point in the history
Return thrown error/rejected promise from t.throws
  • Loading branch information
novemberborn committed Mar 2, 2016
2 parents c94c6de + e03b82c commit 829f91c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ x.throws = function (fn, err, msg) {
.then(function () {
x.throws(noop, err, msg);
}, function (fnErr) {
x.throws(function () {
return x.throws(function () {
throw fnErr;
}, err, msg);
});
Expand All @@ -92,7 +92,18 @@ x.throws = function (fn, err, msg) {
};
}

assert.throws(fn, err, msg);
var result;

assert.throws(function () {
try {
fn();
} catch (error) {
result = error;
throw error;
}
}, err, msg);

return result;
} catch (err) {
test(false, create(err.actual, err.expected, err.operator, err.message, x.throws));
}
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,8 @@ Assert that `function` throws an error, or `promise` rejects with an error.

`error` can be a constructor, regex, error message or validation function.

Returns the error thrown by `function` or the rejection reason of `promise`.

### .notThrows(function|promise, [message])

Assert that `function` doesn't throw an `error` or `promise` resolves.
Expand Down
21 changes: 21 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var test = require('tap').test;
var Promise = require('bluebird');
var assert = require('../lib/assert');

test('.pass()', function (t) {
Expand Down Expand Up @@ -182,6 +183,26 @@ test('.throws()', function (t) {
t.end();
});

test('.throws() returns the thrown error', function (t) {
var expected = new Error();
var actual = assert.throws(function () {
throw expected;
});

t.is(actual, expected);

t.end();
});

test('.throws() returns the rejection reason of promise', function (t) {
var expected = new Error();

assert.throws(Promise.reject(expected)).then(function (actual) {
t.is(actual, expected);
t.end();
});
});

test('.notThrows()', function (t) {
t.doesNotThrow(function () {
assert.notThrows(function () {});
Expand Down

0 comments on commit 829f91c

Please sign in to comment.