-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Unhandled rejection error on "t.throws()" test of trivial async function #1371
Comments
Simpler test case: import test from 'ava';
test(async t => {
await t.throws(async () => {
throw new Error();
});
}); |
This is correct behavior: the function doesn't throw, so the assertion fails. AVA ignores the return value, so the promise rejection ends up being unhandled and is reported separately. That said I think we should extend |
Is this not effectively the same as the following example in the const promise = Promise.reject(new TypeError('🦄'));
test('rejects', async t => {
const error = await t.throws(promise);
t.is(error.message, '🦄');
}); |
@sdd No it's the same as: import test from 'ava';
const promise = () => Promise.reject(new TypeError('🦄'));
test('rejects', async t => {
const error = await t.throws(promise);
t.is(error.message, '🦄');
}); Note the arrow function. |
👍 |
A valid use-case for this design pattern in case you are on the fence: const someTruthy = true;
const someAsyncThing = await () => {
if (someTruthy) {
throw new TypeError('🦄');
}
await asyncStuff();
await moreAsyncStuff();
}
// currently fails with unhandled rejection
test('rejects', async t => {
await t.throws(someAsyncThing, '🦄');
}); IMO the above design pattern is much nicer than: // passes
test('rejects', async t => {
await someAsyncThing()
.catch(err => {
t.is(err.message, '🦄');
});
}); Also, it took me a while to discover this is actually functioning as designed, since it seems very similar to the use-case documented here: https://github.com/avajs/ava/#throwsfunctionpromise-error-message |
We're not on the fence, see the issue labels 😉 Help most wanted! |
Agree that this would be a lovely feature. As a note to my future self (since I keep making this mistake), a work-around is to use an IIFE to turn the promise-returning function into a promise:
|
This was fixed in #1650. |
Description
Unhandled rejection error on test of async function that throws
Test Source
Error Message & Stack Trace
Config
Command-Line Arguments
Environment
Node.js v7.6.0
darwin 15.6.0
ava 0.19.1
npm 4.1.2
The text was updated successfully, but these errors were encountered: