Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest tests leaking due to improper teardown

While doing testing with Jest I am getting a warning saying "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 --detectOpenHandles to find leaks." I realize that this is coming because inside of one of functions I use Bull https://github.com/OptimalBits/bull which uses Redis. So when adding a task to the queue it results in this warning. I use default Bull configuration (no configuration). I do have a mockup for the add function on the queue which is used by Jest, however it didn't help.

const notificationQueue = {
  add: jest.fn().mockImplementation((data: any, opts?: JobOptions) => {}),
};

I'd like to know if there is a way to avoid this warning. If it helps I use in memory mongo for testing but redis is an actual one. As a side note when I run each test suite separately I am not seeing this warning, only when I run all tests.

like image 487
Sterlin V Avatar asked Apr 08 '21 04:04

Sterlin V


People also ask

What causes memory leaks in jest?

The reason why jest consider the testing code is leaked is because QueryEngine is not been recycled in the test suit complete.

How do you stop a jest test?

By default, Jest runs all your tests and shows their result in the console. Yet, Jest comes with a bail option allowing you to stop running tests after n failed tests. You may configure the bail option to a boolean value or a number. Using a boolean for bail will either stop or not stop after the first failed test.

How do I set jest timeout?

Use jest. setTimeout(newTimeout) to increase the timeout value, if this is a long-running test." 1)Increased jest. setTimeout(30000) from 30000 to 60000.

What is a jest error?

jest A worker process has failed to exit gracefully and has been force exited. This is lik... Code Example jest 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 --detectOpenHandles to find leaks. Are there any code examples left?

Do jest tests leak memory?

So, due to no code that you wrote yourself, your Jest tests can start leaking memory. Of course, you could also monkey-patch a native module somewhere in your own code and also trigger a memory leak. How can I tell if Jest is leaking memory? Fortunately it’s easy to tell if your tests are leaking.

What does this jest run report mean?

jest run reports "A worker process has failed to exit gracefully ..." #524 See above. Jest (?) is reporting the warning 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. at the end of a test run

What is the purpose of jest setup and teardown?

It may help to illustrate the order of execution of all hooks. Jest executes all describe handlers in a test file before it executes any of the actual tests. This is another reason to do setup and teardown inside before* and after* handlers rather than inside the describe blocks.


1 Answers

As suggested in the warning, add --detectOpenHandles option to jest's script in package.json file:

"scripts": {
    "test": "jest --watchAll --detectOpenHandles"
  }

Dont forget to stop then start the server !

This solution can work whatever your problem. But, according to your case, your problem is coming from the redis connection. You need to close redis at the end of the test:

import { redis } from "redis_file_path";

afterAll(async () => {
    await redis.quit();
});
like image 169
Ahmad MOUSSA Avatar answered Oct 19 '22 07:10

Ahmad MOUSSA