I have an integration test that calls production code interacting with MySQL. All was working "fine" until I added --runInBand for debugging purposes. Suddenly, my tests started hanging after they ran and this message was shown:
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with
--detectOpenHandlesto troubleshoot this issue.
I tried adding --detectOpenHandles as advised but that only made the message disappear – the tests still ended up hanging.
Only later I found out that the true reason was that I forgot to close the MySQL connection in my code – adding connection.end() fixed it.
I think it's truly awesome that Jest helped me discover an actual issue but maybe the message could be improved? There seem to be other people confused by this, e.g.:
In my scenario, a message like this would be more helpful:
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests or that your code didn't close network connections. Consider running Jest with
--detectOpenHandlesto troubleshoot this issue.
I'm not sure if this message is factually correct but unclosed connections to MySQL, Redis and similar systems seem to be a relatively common cause to the tests hanging.
I'm OK with updating the message but I feel like "network connections" is a still a little less clear. I tried to write something more clear, but the error message quickly grows into paragraphs.
What might be best is to update the message to something like:
Jest did not exit one second after the test run has completed. For more information, see https://jestjs.io/docs/did-not-exit-immediately
We would then be able to provide a lot more information on what's happening and edit it easily.
@SimenB thoughts?
Yeah, great call. I'm in favour of that
Most helpful comment
I'm OK with updating the message but I feel like "network connections" is a still a little less clear. I tried to write something more clear, but the error message quickly grows into paragraphs.
What might be best is to update the message to something like:
We would then be able to provide a lot more information on what's happening and edit it easily.
@SimenB thoughts?