Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return thrown error/rejected promise from t.throws #576

Merged
merged 1 commit into from
Mar 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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