Jest: Your test suite is leaking memory. Please ensure all references are cleaned

avatar
Borislav Hadzhiev

Last updated: Mar 7, 2024
4 min

banner

# Jest: Your test suite is leaking memory. Please ensure all references are cleaned

Here is the complete error message:

shell
Your test suite is leaking memory. Please ensure all references are cleaned. There is a number of things that can leak memory: - Async operations that have not finished (e.g. fs.readFile). - Timers not properly mocked (e.g. setInterval, setTimeout). - Keeping references to the global scope. jest: failed to cache transform results in: Failure message: ENOMEM: not enough memory, write

The Jest error "Your test suite is leaking memory. Please ensure all references are cleaned" occurs when you have a memory leak in your tests.

To resolve the issue:

  1. Open your terminal in your project's root directory (where your package.json file) and run the jest command with the --logHeapUsage flag.
shell
npx jest --logHeapUsage

The --logHeapUsage flag logs the heap usage after every test.

The command is useful when debugging memory leaks.

The output of the command will likely show that your memory consumption increases from test to test.

shell
PASS __tests__/folder/file.test.js (156 MB heap size) PASS __tests__/folder/file2.test.tsx (196 MB heap size) PASS __tests__/folder/file3.test.tsx (201 MB heap size) PASS __tests__/folder/file4.test.tsx (208 MB heap size)
  1. Run the jest command with the --runInBand and --expose-gc flags through node.
shell
node --expose-gc ./node_modules/.bin/jest --runInBand --logHeapUsage

run jest command with expose gc and run in band flags

If you are on Windows, you might have to run the following command instead.

shell
node --expose-gc node_modules\.bin\jest --runInBand --logHeapUsage

The command should lower the memory consumption of your tests.

Update the test command in your package.json file to not have to remember to run the command every time.

package.json
{ "scripts": { "test": "node --expose-gc ./node_modules/.bin/jest --runInBand --logHeapUsage" } }

Make sure to set the correct value of the test command depending on your operating system.

You can now run the command with npm run test.

shell
npm run test

Or with yarn test if you use yarn.

shell
yarn test

The command should be able to resolve the leaky garbage collector issue in Jest.

# Setting the --maxWorkers flag to 1

If the issue persists, try to set the --maxWorkers flag to 1 when issuing the jest command.

shell
npx jest --maxWorkers 1

set max workers flag to 1

The --maxWorkers flag is used to specify the maximum number of works that are spawned for running tests.

Setting the flag to 1 often helps in resource-limited environments like CI servers.

# Try to set the workerIdleMemoryLimit property

If the issue persists, try to set the workerIdleMemoryLimit configuration property in your jest.config.js file.

index.js
/** @type {import('jest').Config} */ const config = { // ... rest workerIdleMemoryLimit: '512MB', }; module.exports = config;

The workerIdleMemoryLimit property is used to specify the memory limit for workers before they are recycled.

After the worker has run a test, its memory usage is checked.

If it exceeds the workerIdleMemoryLimit value, the worker is stopped and restarted.

You can read more about the configuration property in this section of the Jest docs.

Try to run your tests after making the change.

shell
npx jest

# Update jest to the latest version

If the issue persists, try to update jest to the latest version.

Open your terminal in your project's root directory (where your package.json) file is and run the following command.

shell
# with NPM npm install jest@latest jest-environment-jsdom@latest --save-dev # or with YARN yarn add jest@latest jest-environment-jsdom@latest --dev

update jest to latest version

Try to rerun your tests after updating jest.

shell
npx jest

# Downgrade Node.js to version 16.10

If none of the suggestions helped, try to downgrade Node.js to version 16.10.

You can use nvm to manage your Node.js version.

If you already have nvm installed issue to following 2 commands to switch to the LTS version.

shell
nvm install 16.10 nvm use 16.10

nvm install and use 16 10

If you don't have nvm installed, click on the link that relates to your operating system:

After you install nvm, run the following commands.

shell
nvm install 16.10 nvm use 16.10

And check if the Jest issue is resolved.

Downgrading Node.js to version 16.10 often solves the issue as shown in this and this GitHub comments.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.