Skip to content

show error message if literal is thrown - fixes #661 #683

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

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
20 changes: 14 additions & 6 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,21 @@ x.throws = function (fn, err, msg) {

var result;

assert.throws(function () {
try {
fn();
} catch (error) {
result = error;
throw error;
try {
fn();
} catch (error) {
if (error && typeof error !== 'object') {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if this check is good enough in every case though. Should I use something like is-error instead? Because now someone can still throw {foo: 'bar'}.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 for is-error.

var e = new TypeError('Error `' + error + '` is not of type `object` but of type `' + (typeof error) + '`.');
e.actual = typeof error;
e.expected = 'object';
throw e;
}

result = error;
}

assert.throws(function () {
throw result;
}, err, msg);

return result;
Expand Down
38 changes: 38 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,44 @@ test('throws and notThrows work with promises', function (t) {
});
});

test('throws does not work with literals', function (t) {
var result = ava(function (a) {
a.plan(2);
a.throws(function () {
// eslint-disable-next-line
throw 'foo';
});

a.throws(function () {
// eslint-disable-next-line
throw 5;
});
}).run();

t.is(result.passed, false);
t.is(result.reason.message, 'Error `foo` is not of type `object` but of type `string`.');
t.is(result.reason.actual, 'string');
t.is(result.reason.expected, 'object');
t.is(result.result.planCount, 2);
t.is(result.result.assertCount, 2);
t.end();
});

test('throws does not work with promises rejecting literals', function (t) {
ava(function (a) {
a.plan(1);
a.throws(Promise.reject(5));
}).run().then(function (result) {
t.is(result.passed, false);
t.is(result.reason.message, 'Error `5` is not of type `object` but of type `number`.');
t.is(result.reason.actual, 'number');
t.is(result.reason.expected, 'object');
t.is(result.result.planCount, 1);
t.is(result.result.assertCount, 1);
t.end();
});
});

test('waits for t.throws to resolve after t.end is called', function (t) {
ava.cb(function (a) {
a.plan(1);
Expand Down