diff --git a/lib/assert.js b/lib/assert.js index 19f645265..c367126c2 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -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); }); @@ -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)); } diff --git a/readme.md b/readme.md index fbce07a1d..0dceb6298 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/test/assert.js b/test/assert.js index ed3b06c86..c02b46d36 100644 --- a/test/assert.js +++ b/test/assert.js @@ -1,5 +1,6 @@ 'use strict'; var test = require('tap').test; +var Promise = require('bluebird'); var assert = require('../lib/assert'); test('.pass()', function (t) { @@ -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 () {});