-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Async testing fails with async/await #9240
Comments
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. await expect((async () => {
throw new Error('Test');
})()).rejects.toThrow('Test'); 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 await expect(myfunction()).rejects.toThrow('Test')); It doesn't look too bad. |
I think the syntax of However, I also ran in to this situation, where it seemed inconsistent compared to the non-async case, where jest was smart enough to know it needed to invoke the supplied function due to the matchers that were chained on: const fnToTest = (num) => {
throw new Error('Test error ${num}`);
}
// calls the wrapper lambda for me due to the `toThrow` matcher
expect(() => fnToTest(1)).toThrow('Test error'); vs const fnToTest = async (num) => {
throw new Error('Test error ${num}`);
}
// doesn't call the wrapper lambda for me, though it seems like we could if we detect the parameter to `expect` was a function rather than a promise
expect(() => fnToTest(2)).rejects.toThrow('Test error'); So, while I agree that you can achieve the desired test pretty cleanly with the current way of doing things, the inconsistency with the non-async case seems like a gotcha for people learning the library that could be addressed by making jest a little smarter (and we wouldn't have to rely on function names with this solution). |
I'll happily merge a PR which invokes the provided function if it's not a promise, and throws if the return value of that function is not a promise. Anyone up for it? 🙂 |
@SimenB Can I take a look into this? |
Yeah, that'd be wonderful. Go for it! |
@udaypydi Any update on your progress thus far? Otherwise, I might try to have a go as well :) |
@tanettrimas I am working on it. Will raise a PR by tomorrow end of the day. |
I like to take a look into this |
Still exists for Current behaviorawait expect(async () => new Promise(resolve => setTimeout(resolve, 1000))).resolves.toBeUndefined()
// Matcher error: received value must be a promise Workaroundawait expect((async () => new Promise(resolve => setTimeout(resolve, 1000)))()).resolves.toBeUndefined() |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🐛 Bug Report
Asynchronous testing does not support
async
functions.To Reproduce
Yields:
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:The solution is quite easy, instead of expecting only an object,
expect
should check if the constructor is anAsyncFunction
. For example :envinfo
The text was updated successfully, but these errors were encountered: