Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

are there side effects of running jest with --detectOpenHandles --forceExit?

Tags:

I'm using jest for testing, and in a few test scenarios I get the jest message:

Jest did not exit one second after the test run has completed. While taking Jest's recommendation to run with --detectOpenHandles and ending up with hanging test process that never ends, I saw other online suggestions to add the --forceExit option as well. Now the test ends and everything is ok.

It worths mentioning that all the tests are running properly and passing, with or without the --detectOpenHandles --forceExit options.

I wonder if is that considered as best practice in such cases? or is it just serving me as a "first aid"? What are the side effects of doing so?

Cheers,

like image 437
Ziv Levy Avatar asked Nov 29 '18 13:11

Ziv Levy


People also ask

What does -- detectOpenHandles do in jest?

--detectOpenHandles ​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.

Does jest cache test results?

Even distribution of test suites across workers If you're running your test suites in parallel (enabled by default), jest will cache information about how long each of your test suites takes to run.

What is jest watch mode?

By default, Jest will run tests against all changed files since the last commit, but watch mode also exposes many options. By hitting the w key, you can see the different ways to operate Jest in watch mode. The options are dynamic, so it's worth playing around within watch mode to see what's available.

How do you only run one test in jest?

It's in the Jest documentation. Another way is to run tests in watch mode, jest --watch , and then press P to filter the tests by typing the test file name or T to run a single test name.


1 Answers

From the documentation, the detectOpenHandles option is for:

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.

The forceExit option should never be used as a best practice, the only time you have to use is because:

  • An async function did not finish
  • A promise function did not finish
  • A Websocket connection is still open
  • A Database connection is still open
  • Everything having a connect/disconnect methods did not disconnect before the end of the test
like image 110
oktapodia Avatar answered Sep 21 '22 09:09

oktapodia