Async testing fails with async/await
See original GitHub issue🐛 Bug Report
Asynchronous testing does not support async
functions.
To Reproduce
await expect(async () => {
throw new Error('Test');
}).rejects.toThrow('Test');
Yields:
expect(received).rejects.toThrow()
Matcher error: received value must be a promise
Received has type: function
Received has value: [Function anonymous]
22 | await expect(async () => {
23 | throw new Error('Test');
> 24 | }).rejects.toThrow('Test');
| ^
25 | });
26 |
27 |
Expected behavior
The test should treat the function as a promise. At the moment, the fix is to chain the function to a Promise
, for example:
await expect( Promise.resolve().then(async () => {
throw new Error('Test');
})).rejects.toThrow('Test');
The solution is quite easy, instead of expecting only an object, expect
should check if the constructor is an AsyncFunction
. For example :
if (value.constructor.name === 'Promise` || value.constructor.name === 'AsyncFunction') {
...
}
envinfo
System:
OS: Linux 5.3 Ubuntu 19.10 (Eoan Ermine)
CPU: (16) x64 AMD Ryzen 7 PRO 2700 Eight-Core Processor
Binaries:
Node: 12.10.0 - ~/.nvm/versions/node/v12.10.0/bin/node
Yarn: 1.19.1 - ~/.nvm/versions/node/v12.10.0/bin/yarn
npm: 6.11.3 - ~/.nvm/versions/node/v12.10.0/bin/npm
npmPackages:
jest: ^24.9.0 => 24.9.0
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:10 (2 by maintainers)
Top Results From Across the Web
Testing Asynchronous Code - Jest
Promises. Return a promise from your test, and Jest will wait for that promise to resolve. If the promise is rejected, the test...
Read more >Why isn't this unit test catching an error from this async/await ...
In this case, we call the fetchItem() function with an argument that is not a string (which our database query will expect). It's...
Read more >Testing async/await exceptions - Artur Gruchała
We have to test our new code. The testing happy path is pretty straightforward. We can mark the test method as async ,...
Read more >Async await test cases failed using jest javascript testing library
I am using Jest testing Library for some simple async/await functions. But it's failing again and again as I am very new to...
Read more >Asynchronous work - Jasmine Documentation
Failing with async / await async / await functions can indicate failure by either returning a rejected promise or by throwing an error....
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
Still exists for
resolves
.Current behavior
Workaround
Hello 👋
Your suggestion would indeed make the API more practical for the use case you present, but it would not work consistently. Relying on the name of the function being passed is error-prone. Indeed, it is not necessarily an arrow-function and it could be a named async function.
I also don’t believe it is a bug. The documentation makes no claim to support functions; it knowingly only supports promises.
To work around this, you can simply call the function. This is similar to your suggestion, but you don’t really need the
Promise.resolve
. The code below will work just as well:Not only does this work fine, but most of the time it is not even needed. Indeed, people tend to test one function call. Say I already have an async function called
myfunction
. This is how I would test it:It doesn’t look too bad.