jest: --detectOpenHandles prints nothing, just hangs
š Bug Report
When I run my test suite I get one of these 2 messages:
A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --runInBand --detectOpenHandles to find leaks.
or
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
--detectOpenHandles
to troubleshoot this issue.
For one, I think the mention of --runInBand
in the first message is probably not necessary. But in any case, when I start running jest with --detectOpenHandles
, I get no output, jest just hangs after finishing the tests.
Expected behavior
I would expect detectOpenHandles to give me actionable output.
envinfo
$ npx envinfo --preset jest
npx: installed 1 in 1.775s
System:
OS: macOS 10.15.2
CPU: (4) x64 Intel(R) Core(TM) i7-7660U CPU @ 2.50GHz
Binaries:
Node: 10.18.0 - ~/.nvm/versions/node/v10.18.0/bin/node
Yarn: 1.21.1 - /usr/local/bin/yarn
npm: 6.13.6 - ~/.nvm/versions/node/v10.18.0/bin/npm
npmPackages:
jest: ^25.1.0 => 25.1.0
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 147
- Comments: 54 (5 by maintainers)
Commits related to this issue
- Merge #2330 2330: Close all handles during Jest teardown r=bonomat a=luckysori _Waiting for comit-network/comit-js-sdk#176 to be merged and for those changes to be included in a release of `comit-js... — committed to comit-network/comit-rs by bors[bot] 4 years ago
- Force-exit jest after completion This is probably a Jest bug See https://github.com/facebook/jest/issues/9473 — committed to rubensworks/rdf-parse.js by rubensworks 4 years ago
- chore: temp fix Waiting for https://github.com/facebook/jest/issues/9473 https://github.com/fsevents/fsevents/issues/345 https://github.com/facebook/jest/issues/10777 — committed to vuejs/router by posva 4 years ago
- chore: temp fix Waiting for https://github.com/facebook/jest/issues/9473 https://github.com/fsevents/fsevents/issues/345 https://github.com/facebook/jest/issues/10777 — committed to vuejs/router by posva 4 years ago
Iām seeing the same issue. Iām not asking for help debugging my code, Iām pointing out a āconcrete problem in jestā. In some code bases, when running jest with
--runInBand --detectOpenHandles
, as recommended by the CLI, the tests complete with no reported issues and jest hangs.I have not been able to isolate a minimal reproduction, but this does not mean the issue isnāt real. Unless this is expected behavior, please reopen in acknowledgement that this is clearly affecting users.
this solves it ātestā: ājest --watch --runInBand --detectOpenHandlesā
@jeysal hello. Please check my repo - umbress. You can find all my tests and configs in there. Also you can go to pull requests tab and see there an ongoing/failed tests for my latest PR - full GitHub Actions logs is there for you to explore š
In my PC when I start
npm test
it saysBut in Github Actions message is changing to
Edit:
--forceExit
and--detectOpenHandles
has fixed this issue for me, but once again it feels like Iām fighting with only symptoms.@Prestaul donāt bother. They rather ask you to provide minimal repro than actually try to investigate after a bunch of complaints.
@laurent22 is exactly right. This thread is not about posting workarounds, which is what this has become. Nothing against everyone sharing info. Without any official response that is actually useful, thatās what happens.
Given that thereās clearly no intention to fix this, I guess itās time for me to unsubscribe from my own bug report. Sigh.
Why does the library have
--detectOpenHandles
if you have to install another library to detect the open handles?Of course you also can find the leaked handles manually by isolating the leaking component, but this does not change the fact that
--detectOpenHandles
is broken and this ticket should be re-opened.confirmed. I saw the same message in the console, ran the command with --runInBand and --detectOpenHandles but it was left hanging
same version of mac os, node 12.13.0, yarn 1.21.1, npm 6.13.6, jest 25.1.0
jest --forceExit --detectOpenHandles
this solves the problem for meWhat is expected behaviour of --detectOpenHandles? The documentation says that it shall print something. But when I run JEST with this parameter, the test never finishes and there is nothing written at all when I kill the process with Ctrl-C. Am I missing something?
I have the same issue.
npm test
leads toThen
npm test -- --detectOpenHandles
leads to no errors but a slow test run and probably due to a force quit of jest after some timeoutThen
npm test -- --forceExit
leads to the messageSo executing
npm test -- --forceExit --detectOpenHandles
leads to no warning and messages and a quick end of the test process.This makes sense but I would rather like to know which handle of mine is still open to close it in a clean way. So I say this is a bug and should be reopened.
Edit 2020-04-23:
I was able to strip down my project and reproduce the error. Have an assumption what is the reason in my code which causes the issue. If this assumption turns out to be true I will link a repo here to reproduce it which might help the Jest devs to debug the issue.
Edit 2020-04-24:
Registered timeouts are not detected by the jest ādetectOpenHandlesā option Here a repo to reproduce the issue and a fix on client side https://github.com/TarSzator/jest-open-handle-test
I hope it helps to find the error in Jest or Jest is intended like that then it might help users to find and fix issues on their side.
Iāve also hit this a number of times and again today, so I made a minimal repro from my most recent encounter:
https://github.com/levilansing/jest-9473
Iām reasonably certain Iāve encountered this with timers and promises in the past, but couldnāt repro just now. In this repo, Iāve left a connection open to redis during tests, which should obviously be mocked during tests, but demonstrates that jest does not provide any hint to what might be causing the tests to hang when using --detectOpenHandles in this case.
Without a repro, this is not actionable. Feel free to reopen if you can provide a minimal (= not just a project with lots of tests and config that may or may not be related to the issue) one.
--detectOpenHandles
without--runInBand
does not really make sense (unless your tests are already being run in band anyway), since you will be detecting open handles in the main process, while your tests are running in child processes. We should amend all messages mentioning--detectOpenHandles
to also include--runInBand
, Iāve noted to do that at some point.Adding leaked-handles package to my Jest setup code and then
--runInBand
allowed to find which test suit is leaking.The problem is not really what triggers this log, as that varies from user to user. The problem is that --detectOpenHandles makes Jest freezes instead of printing information.
Shouldnāt this issue be re-opened, now that someone has provided a minimal repro?
We see this problem after updating to jest 26.6.0 and did not see it before that version.
For anyone still struggling with this, I managed to narrow down my issue by using a combination of suggestions above. Hereās what I did:
npx jest --runInBand --detectOpenHandles src/tests
to validate that something was indeed hangingnpx jest --runInBand --detectOpenHandles src/tests/someDir
on several dirs to figure out which one was the culprit (if it doesnāt hang and exits cleanly, move on to another dir)npx jest --runInBand --detectOpenHandles src/tests/someDir/someFile
to figure out exactly which file was causing the problemFor anyone using NEXT.js, this line was the problem for me:
const nextApp = next({ dev: process.env.NODE_ENV !== 'production' });
. Iām ripping it out tomorrow and migrating to serving an app directly from Express š¬Hope this helps someone else out there!
For anyone struggling with this issue and using
ioredis
package: A simple fix would be to quit the redis connection in your afterEach or afterAll hooks:Referring from: https://github.com/luin/ioredis/issues/1088#issuecomment-732490888
Hi, I am facing the similar issue. I just write this simple test.
But jest is not returning to the terminal after running the test. --detectOpenHandles is also not showing any output.
I had same issue but I noticed that it works fine when I specify a path when running my tests
Jest outputs correctly open handles:
jest --runInBand --detectOpenHandles src/
Where here I get nothing:jest --runInBand --detectOpenHandles
This was the issue for meā¦ I applied the workaround suggested in that issue to get jest to exit again.
I am seeing these errors too (even after
.skip
) all of my tests. However, When adding both--forceExit --detectOpenHandles
, the test exists ācleanlyā (no warnings). I suspect there is a red herring somewhere that is causing this since I could not find any leaks in my tests (although they may exist). Iām not of the opinion that the combination of those flags listed is a real fix, but the tests did exit properly and without warnings or errors.Using
jest@next
detects 2 handles:stack trace of the first one isnāt suuuuper useful, but itās something pointing to redis at least š
Same here
As far as I can see, these are the only 2 reproductions posted, which have both been fixed (in #11429 š), so Iāll close this one as itās suuuuper long and full of workarounds and "+1"s.
One improvement I want to make is print āsorry, we found no open handlesā or some such so it doesnāt seems like Jest didnāt react to the flag.
If somebody using Jest 27 or newer (try out
jest@next
, stable will come next week) still have this issue, please open up a new one with a minimal reproduction (like the two I checked here).FWIW, I have this same issue on Linux which isnāt installing
fsevents
at all.None of the previous instructions helped me, I still get it hanging. Here is a work-around that worked for me:
Install why-is-node-running:
npm i why-is-node-running
Edit jest entry point to use it, in my case the file is located in./node_modules/.bin/jest
I made it to look exactly like this:
Then running jest:
./node_modules/.bin/jest (my params) --runInBand
Got it printing some helpful information every 1 second.Jest code says it is based on why-is-node-running: https://github.com/facebook/jest/blob/1cc205c274defb24f8b827b8a0c53c161c79d106/packages/jest-core/src/collectHandles.ts#L38
In my case there was a setInterval, and clear interval in a react component
For example -
I used
jest.useFakeTimers();
on the test https://jestjs.io/docs/en/timer-mocks.html and it was fixedBut the original problem --detectOpenHandles should be fixed
Yes, I tried that, and it didnāt give me any valuable information. But after all I figured out the root cause of my issue: I was using TypeORM to manage a DB and I had to explicitly close the connection to the DB.
I have experienced the same problem with a
ioredis
using test, solved as proposed. It looks like the test process hangs with the redis open connectionI had a similar issue to @Jule-, but it was how I was making a connection to Mongo via mongoose. It seems like mongooseās
mongoose.connection.close()
in a top-levelafterAll
within asetupTests
file isnāt consistently closing the connection.My approach was to set up a single connection before all tests within a
setupTests.ts
file to register mongoose models, then close the single connection after all test suites have finished. This appears to cause issues with Jest.For example: database/index.ts
setupTests.ts (this works about 50% of the time unless I used
--runInBand
, then it worked fine)Instead, removing the
setupTests.ts
file and moving the connection within each test suite seems to have fixed the warning:Same here.
envInfo:
Jest cmd:
jest --testTimeout=20000 --runInBand --detectOpenHandles
Code example:
Process hangs. No open handles are logged.
EDIT: I think it might be connected to https://github.com/facebook/jest/issues/10777
Hello all. My issue is that running
jest
produces the āa worker process failed to exit gracefullyā but when i try to isolate with the provided CLI args--runInBand and --detectOpenHandles
, I am no longer able to produce the error. soā¦ just wanted to throw that out there. i also found this open issue in testing-library:https://github.com/testing-library/dom-testing-library/issues/524
not sure itās related, but itās something.
I havenāt had a chance to take a closer look at any of the comments/repros posted yet, but yeah can reopen even though not sure whether I can look at it myself
Same problem here, but I am just starting a new project. Should I check other test libraries? any suggestion? thanks
Iām having the same problem. Tried all suggestions here but to no avail. @jeysal are you willing to re-open now since a minimal repro is provided?
I come here to share my experience.
TL;DR: in my case it was my unclosed database connections that was triggering this logā¦
Long version: I use Jest to run backend tests, so in my tests I use
supertest
which starts myexpress
app which starts MongoDB connections withmongoose
ā¦ And obviously after my tests are finished, I did not close my connections, pretty far from my thoughts when I firstly saw this issueā¦ Then for me this is what fixed my issue:In order to reproduce:
HTH
Hi All, Iām having a similar issue, but only when collecting coverage. All tests runs fine without the
--collectCoverage
at the moment I turn this flag on, I start getting some weird issues, including the 2 messages stated on the bug report.@jeysal Hi, I can see above that two minimal repros were provided. Is it possible to reopen this?
Thanks!