Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest error on upgrading to latest version - a worker process has failed to exit gracefully

Tags:

reactjs

jestjs

I recently updated my jest package to the latest version from v24 and on running my unit tests, I am getting this at the end of test suite run:

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.

I added --detectOpenHandles with my test command and the messages goes in the next run. I am not sure how to take it as detectOpenHandles is intended for finding the open handles and not fixing it so how is the message not coming in next run and no other message to guide me on what could be the issue.

Has anyone came across similar bhaviour? To add, I am on latest version of jest i.e 27.2.0 now.

like image 713
Peter Avatar asked Nov 26 '22 22:11

Peter


1 Answers

From my research, the error occurs when :

  • An async function did not finish
  • A promise function did not finish
  • A Websocket, Database, or anything else that has connect/disconnect methods did not disconnect before the end of the test

The error doesn't mention the fact that the tests will just hang most of the time even with --detectOpenHandles flag, which is probably the reason why you aren't seeing any useful debugging messages.

From the documentation :

Attempt to collect and print open handles preventing Jest from exiting cleanly. Use this in cases where you need to use --forceExit in order for Jest to exit to potentially track down the reason. This implies --runInBand, making tests run serially. Implemented using async_hooks, so it only works in Node 8 and newer. This option has a significant performance penalty and should only be used for debugging.`

As it says in the documentation, you could try running the tests using the --detectOpenHandles and the --forceExit flag. This would mean the tests would run serially and each test would force exit when a test doesn't complete due to improper teardown. Let me know if this works.

like image 151
John Kim Avatar answered Dec 04 '22 12:12

John Kim