Skip to content

t.throws() returns promise instead of promise's rejection reason #1120

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
dchowitz opened this issue Nov 20, 2016 · 2 comments
Closed

t.throws() returns promise instead of promise's rejection reason #1120

dchowitz opened this issue Nov 20, 2016 · 2 comments

Comments

@dchowitz
Copy link

Description

According to the documentation for .throws(function|promise, [error, [message]]) the method should return the rejection reason of promise, instead the promise itself is returned.

Test Source

test('throws(promise) returns promise', t => {
  var result = t.throws(Promise.reject(new Error('foo')));
  t.is(typeof result.then, 'function');
});

// I've expected this should work, but it fails, since message is undefined
test('rejection reason is foo', t => {
  const error = t.throws(Promise.reject(new Error('foo')));
  t.is(error.message, 'foo');
});

Environment

Node.js v7.1.0
darwin 16.1.0
npm 3.10.9
ava 0.17.0

@sindresorhus
Copy link
Member

Thanks for reporting. It's a documentation bug. It should say that it returns a promise for the rejection reason. I'll fix that up shortly.

You need to await it:

test('rejection reason is foo', async t => {
    const error = await t.throws(Promise.reject(new Error('foo')));
    t.is(error.message, 'foo');
});

That being said. We're planning a better interface for the throws assertion: #1047 (feedback welcome)

@dchowitz
Copy link
Author

Understood. Thanks for the quick reply and clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants