t.throws and t.doesNotThrow should be able to accept a promise
See original GitHub issueAt the moment, t.throws
and t.doesNotThrow
only accepts a function. It should also be able to accept a promise, like AVA.
I want to be able to test that Koa middleware throws a particular error:
(ctx, next) => {
t.throws(next(), MyError)
}
It would be great if the function return value and promise resolved value would be returned. Here is an AVA example:
const promise = Promise.reject(new TypeError('🦄'));
test('rejects', async t => {
const error = await t.throws(promise);
t.is(error.message, '🦄');
})
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Jasmine: How to expect promise handler to not throw exception
Let reload return the promise it creates. In your test case, attach a catch handler to it which triggers a test failure:
Read more >Reject Vs Throw Promises in JavaScript - GeeksforGeeks
reject (): It is an inbuilt function in Javascript that returns a Promise object which has been rejected for a particular given reason....
Read more >How to Throw Errors From Async Functions in JavaScript?
Async functions and async methods always return a Promise, either resolved or rejected. You must attach then() and catch() , no matter what....
Read more >Top 15 C++ Exception handling mistakes and how to avoid ...
So here's the bottom-line, don't just dismiss using C++ exceptions ... a function can specify that it does not throw exceptions by providing ......
Read more >Node.js v19.3.0 Documentation
Lines that don't start with $ or > character show the output of the previous ... import assert from 'node:assert'; // WARNING: This...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think you are mislead by console.log. As tests run in parallel, the logging sequence (at least through console.log) is not relevant.
However you should be able to test promise error easily
or in action
⚠ warning: the example above will always pass - even if
throwing
does not throw!✔ The correct solution was offered in this thread.
I do think we have a problem with this feature… I fall into this trap myself every time - the issue has been posted twice now, and the track record for someone providing the correct workaround is 1:2. 😉
How about the following solution?
If the callback passed to
throws
returns aPromise
, it should throw an error and abort the test: this will help folks realize thatthrows
is not intended for use with async callbacks - currently this quietly fails, so this will fix that.Add a another assertion
rejected
specifically accepting only aPromise
, which must be rejected rather than resolved: this is more explicit and makes the distinction more clear.When failing in
throws
for passing aPromise
, the error message could explain how to correctly switch torejects
, e.g.:We should tighten the type-hints in TS as well - for
throws
we can use a param typeT extends Promise<unknown> ? never : T
, as covered in this article, and forrejected
of course this would accept solely aPromise<T>
.(The only remaining pitfall then is people may forget to use
await
witht.rejected
- but that’s just Javascript, I’m afraid.)What do you think, would you consider reopening this issue?