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

Add string support to throws #125

Closed
wants to merge 2 commits into from
Closed
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
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 e = err;
err = function (err) {
return err.message === e;
};
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (typeof err === 'string') {
  err = function (e) {
    return e.message === err;
  };
}

assert.throws(fn, err, msg);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely cleaner, but then I guess it should be like this.

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

assert.throws(fn, err, msg);

In your example, at the time the function is executed, err is not the string anymore but it is the function. So it will check if err.message is equal to the function instead of to the original string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup


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 @@ -223,6 +223,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