Skip to content

Commit

Permalink
Close #125 PR: Add string support to throws.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren authored and sindresorhus committed Nov 5, 2015
1 parent 8d84f39 commit 257c414
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ x.throws = function (fn, err, msg) {
}

try {
if (typeof err === 'string') {
var errMsg = err;
err = function (err) {
return err.message === errMsg;
};
}

assert.throws(fn, err, msg);
} catch (err) {
test(false, create(err.actual, err.expected, err.operator, err.message, x.throws));
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ Assert that `value` is not deep equal to `expected`.

Assert that `function` throws an error or `promise` rejects.

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

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

Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,18 @@ test('handle throws with regex', function (t) {
});
});

test('handle throws with string', function (t) {
ava(function (a) {
a.plan(1);

var promise = Promise.reject(new Error('abc'));
a.throws(promise, 'abc');
}).run().then(function (a) {
t.false(a.assertionError);
t.end();
});
});

test('handle throws with false-positive promise', function (t) {
ava(function (a) {
a.plan(1);
Expand Down

0 comments on commit 257c414

Please sign in to comment.