Last updated: Mar 7, 2024
Reading time·4 min
Here is the complete error message:
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:
package.json
file) and run the jest
command with the --logHeapUsage
flag.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.
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)
jest
command with the --runInBand
and --expose-gc
flags through
node
.node --expose-gc ./node_modules/.bin/jest --runInBand --logHeapUsage
If you are on Windows, you might have to run the following command instead.
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.
{ "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
.
npm run test
Or with yarn test
if you use yarn
.
yarn test
The command should be able to resolve the leaky garbage collector issue in Jest.
--maxWorkers
flag to 1
If the issue persists, try to set the
--maxWorkers flag to 1 when
issuing the jest
command.
npx jest --maxWorkers 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.
workerIdleMemoryLimit
propertyIf the issue persists, try to set the
workerIdleMemoryLimit
configuration property in your jest.config.js
file.
/** @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.
npx jest
jest
to the latest versionIf 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.
# with NPM npm install jest@latest jest-environment-jsdom@latest --save-dev # or with YARN yarn add jest@latest jest-environment-jsdom@latest --dev
Try to rerun your tests after updating jest
.
npx jest
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.
nvm install 16.10 nvm 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.
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.
You can learn more about the related topics by checking out the following tutorials: