Why Jest freezes after tests run? (Jest hangs indefinitely)
If you noticed that Jest (npm / yarn) test hang at the end of the test execution while you run tests in parallel on your CI server with
@knapsack-pro/jest client then you probably have open handles preventing Jest from exiting cleanly.
You can run tests with a flag
$(npm bin)/knapsack-pro-jest --detectOpenHandles
--detectOpenHandles - it attempts 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. This option has a significant performance penalty and should only be used for debugging.
You can also use
--forceExit - it forces Jest to exit after all tests have completed running. This is useful when resources set up by test code cannot be adequately cleaned up. Note: This feature is an escape-hatch. If Jest doesn't exit at the end of a test run, it means external resources are still being held on to or timers are still pending in your code. It is advised to tear down external resources after each test to make sure Jest can shut down cleanly. You can use
--detectOpenHandles to help track it down.
Another tip: you can also verify if you have enough memory on CI server.