Skip to content
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

Jest ignores Errors thrown by async functions in some cases #6043

Closed
bvaughn opened this issue Apr 20, 2018 · 3 comments
Closed

Jest ignores Errors thrown by async functions in some cases #6043

bvaughn opened this issue Apr 20, 2018 · 3 comments

Comments

@bvaughn
Copy link
Contributor

bvaughn commented Apr 20, 2018

I recently noticed that Jest seems to ignore errors thrown by async functions in certain cases.

I discovered this while writing some tests for a React component that calls an async function from componentDidMount. If the async function calls setState- and the component un-mounts before that happens- the resulting warning/error issued by React is silently swallowed.

Here is an annotated example of what I'm describing (but without React):
https://github.com/bvaughn/jest-uncaught-error-repro/blob/master/index.test.js

To repro this false positive:

git clone [email protected]:bvaughn/jest-uncaught-error-repro.git
cd ./jest-uncaught-error-repro
yarn install
yarn test

In my repro, throwing the error within an expect(...).not.toThrow() block "fixes" things so that the test fails, but that is not the case within the test that first uncovered this issue. It's also not a great solution, since errors aren't guaranteed to be thrown within such a block.

Am I missing or overlooking something?

@bvaughn
Copy link
Contributor Author

bvaughn commented Apr 20, 2018

Maybe related to #5311, #3251 - although I added logging (to my local Jasmine Env) to verify that my error is thrown before the uncaught error handler is removed FWIW.

@bvaughn
Copy link
Contributor Author

bvaughn commented Apr 21, 2018

It was pointed out to me that this:

await Promise.resolve(r => setTimeout(r, 200));

...was a bit bogus and should instead be this:

await new Promise(r => setTimeout(r, 200));

...which does indeed seem to fix my repro.

@bvaughn bvaughn closed this as completed Apr 21, 2018
@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant