jest: JavaScript heap out of memory after upgrade to Jest 26
š Bug Report
I upgraded from 24.X to 26.0.0 but now test that was passing is not Running test takes long time to complete then I get this error
To Reproduce
My test:
describe('when item ids are in sessionStorage', () => {
const itemIds = [333, 222, 111];
beforeEach(() => {
parseLocationToQueries.mockImplementation(() => ({
queue_id: testQueueId
}));
isAdHocReviewByItemId.mockReturnValue(false);
isAdHocReviewByObjId.mockReturnValue(false);
setItemsToBeReviewed(itemIds);
});
it('initial fetch', () => {
const wrapper = tf.render();
expect(wrapper.state('itemIds')).toEqual([]);
expect(axios.post).toBeCalledWith('/review/items', { item_ids: itemIds });
});
it('fetch more while no more', () => {
const wrapper = tf.render();
axios.post.mockClear();
wrapper.instance().fetchMoreItems();
expect(axios.post).not.toBeCalled();
});
it('fetch more while more', () => {
const wrapper = tf.render();
axios.post.mockClear();
wrapper.setState({ itemIds: [555] });
wrapper.instance().fetchMoreItems();
expect(axios.post).toBeCalledWith('/review/items', { item_ids: [555] });
});
});
code:
export function setItemsToBeReviewed(itemIds) {
sessionStorage.setItem(ITEMS_TO_BE_REVIEWED_KEY, JSON.stringify(itemIds));
}
fetchMoreItems = () => {
this.setState({ loadingMoreItems: true });
return this.fetchItems(true)
.then(res => {
this.loadData(res.data);
})
.catch(error => {
console.log('FetchmoreError', error);
});
};
fetchItems = (excludeAssigned: boolean = false) => {
let request;
if (this.state.itemIds) {
request = this.fetchItemsByIds();
} else {
request = this.fetchItemsFIFO(excludeAssigned);
}
return request;
};
fetchItemsFIFO = (excludeAssigned: boolean = false) => {
const { isAlignment, queueIdFromURL } = this.state;
const url = '/review/assign';
const params = {
alignment: isAlignment,
queue_id: queueIdFromURL,
exclude_assigned: excludeAssigned
};
return axios.get<any>(url, { params });
};
fetchItemsByIds = () => {
if (_.isEmpty(this.state.itemIds)) {
return Promise.resolve({ data: [] });
}
const url = '/review/items';
const data = {
item_ids: _.slice(this.state.itemIds, 0, FETCH_BATCH_SIZE)
};
this.setState(state => ({
itemIds: _.slice(state.itemIds, FETCH_BATCH_SIZE)
}));
return axios.post<any, any>(url, data);
};
jest.config:
module.exports = {
timers: 'fake',
moduleDirectories: ['node_modules'],
moduleFileExtensions: ['js', 'jsx'],
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/__mocks__/fileMock.js',
'\\.(css|less)$': '<rootDir>/__mocks__/styleMock.js',
'^Root(.*)$': '<rootDir>$1',
'^Utils(.*)$': '<rootDir>/src/utils$1',
'^Hoc(.*)$': '<rootDir>/src/hoc$1',
'^Components(.*)$': '<rootDir>/src/components$1'
},
testRegex: 'test\\.jsx?$',
testURL: 'http://localhost:3000',
collectCoverageFrom: [
'src/**/*.js',
'src/**/*.jsx',
'!**/node_modules/**',
'!src/components/bulk_review/columns/**',
'!src/components/v2/**'
],
coverageReporters: ['html', 'text'],
coverageThreshold: {
global: {
branches: 90,
functions: 90,
lines: 90,
statements: 90
}
},
coverageDirectory: 'coverage',
snapshotSerializers: ['enzyme-to-json/serializer'],
testEnvironment: '<rootDir>/jest-environment.js',
setupFilesAfterEnv: ['<rootDir>/enzyme.setup.js'],
setupFiles: [
'<rootDir>/__mocks__/localStorageMock.js',
'<rootDir>/__mocks__/consoleMock.js'
],
globals: {
ENVIRONMENT: 'TESTING'
},
testPathIgnorePatterns: ['<rootDir>/src/components/v2'],
reporters: [
'default',
[
'jest-html-reporter',
{
pageTitle: 'Test Report',
statusIgnoreFilter: 'passed',
includeFailureMsg: 'true'
}
]
]
};
envinfo
System: OS: Linux 4.15 Ubuntu 18.04.4 LTS (Bionic Beaver) CPU: (36) x64 IntelĀ® XeonĀ® Platinum 8124M CPU @ 3.00GHz Binaries: Node: 14.1.0 - ~/.nvm/versions/node/v14.1.0/bin/node Yarn: 1.22.4 - /usr/bin/yarn npm: 6.14.4 - ~/.nvm/versions/node/v14.1.0/bin/npm npmPackages: jest: ^26.0.0 => 26.0.0
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 59
- Comments: 62 (6 by maintainers)
Commits related to this issue
- Switch to v8 coverage provider Workaround for https://github.com/facebook/jest/issues/9980 — committed to fb55/entities by fb55 3 years ago
- Update jest config so that js tests don't run out of memory ref: https://github.com/jestjs/jest/issues/9980 — committed to brave/brave-core by diracdeltas 8 months ago
- Update jest config so that js tests don't run out of memory ref: https://github.com/jestjs/jest/issues/9980 — committed to brave/brave-core by diracdeltas 8 months ago
- Update jest config so that js tests don't run out of memory ref: https://github.com/jestjs/jest/issues/9980 — committed to brave/brave-core by diracdeltas 8 months ago
I had the same problem (also while collecting coverage data, on GitHub Actions/CI) and fixed it by limiting the number of workers:
maxWorkers: 2
injest.config.js
--w 2
as a command line parameter.Trying to migrate from 26.6.3 to 27.2.5 and got the same issue.
After doing some research, it seems this memory leak has been an ongoing issue since 2019 (Jest 22) so wanted to consolidate some notes here for posterity. Past issues have been related to graceful-fs and I think some have solved it via a hack/workaround that removes graceful-fs and then re-adds graceful-js after running jest. One troubleshooting thread was looking at compileFunction in the
vm
package as a potential cause. It seems that jest, webpack-dev-server, babel, and create-react-app are using graceful-js as a dependency. The memory leak issue was supposed to be fixed in a newer release of Jest but there may have been a regression since it is popping up again. I can confirm everything was working fine until a substantial amount of Jest tests were created in our environment and then the heap overflows on our CI machine after the heap size grows larger than the allocated memory due to the leak. Iāve tried using 1 worker, runInBand, etc. without success.The common cause of the issues Iāve seen is collecting coverage via collecting coverage and graceful-fs. I havenāt done an in-depth analysis of those issues but seeing that they are both filesystem-related and having solved my own issue which was related to file imports I suspect they are some version of the same issue I was having.
Wanted to provide the solution I found so others may reap benefits:
The cause:
Using imports of the format
import * from 'whatever'
The solution:
Using the format
import { whatINeed } from 'whatever'
instead dramatically reduced the memory accumulationi was running into this issue and it appears that upgrading to jest 27 fixed it
@pedrohamarques
Yes, by migrating to mocha + chai + sinon š
Updating my
jest.config.ts
withcoverageProvider: 'v8'
andmaxWorkers: 2
did the trick for me!I was facing the same error on GitHub actions. I was able to pin down the problem in my case to the currently experimental ESM support #9430.
For comparison here are two profiling screenshots (I followed this article for instructions):
CJS (leaked ~20mb)
ESM (leaked ~1gb š± )
I had a similar problem where I used to run into
Out of Memory
error when Jest started to do coverage on āuntested filesā. Using v8 as coverage provider solved the issue for me. However, its an experimental feature (as per documentation) - https://jestjs.io/blog/2020/01/21/jest-25#v8-code-coverageI suspect that this issue might be related to the way that the objects that are being compared are being printed. Filed an issue with a minimal reproduction here: https://github.com/facebook/jest/issues/12364
In effect, Jest will stop short if itās trying to print a very deeply nested object, but doesnāt account for ātotal nodesā so for objects that are fairly shallow but very wide (such as many React/Enzyme elements) it will try to print the whole object and will run out of memory while constructing the string.
I had what I thought may be a similar error.
Using
--coverage
while running inside a container like the node:alpine docker image was a problem for me. I really didnāt need to be running tests in a container so maybe my comment/solution here https://github.com/facebook/jest/issues/5837#issuecomment-1002239562 may help someone.This solved my issue: https://stackoverflow.com/a/68307839/9331978
I was having a similar issue where random tests would timeout. I added three arguments to my jest.config.js file and it seems to have helped. Seems to have been happening on both windows, mac, and in our gitlab pipeline.
testTimeout: 10000, maxConcurrency: 3, maxWorkers: ā50%ā,
Surprisingly the tests execute faster as well. By ~15-20%.
We see this regularly on our tests ā the first one succeeds then the second fails.
+1 @alexfromapex solution did not worked for me.
Jest 26.6.3 Node 14.15.4
Dump: https://pastebin.com/Mfwi2iiA
It happens after some re-runs on any CI server (my runners are docker containers). Always after a fresh boot it works normally, and after some runs, it breaks again, only comming back after a new reboot. I tried with 1GB RAM and 2GB RAM machines, same result. It seems not happening with 8GB+ RAM hardware (my local machine).
Some other info Iāve gathered, it happens always after ~5m running, everytime the test log has the same size (it might be happening at same spot).
Not sure if it is related. But I get heap leak for simple expect:
Clearing cache doesnāt help
We will need a repro that can be downloaded and analyzed. Also, please make sure to clear cache just in case, e.g with
jest --clear-cache
For me this happened only on CI. Turns out it was because on CI the default is
runInBand
, so adding this locally helped me replicate the issue on a local machine. For me it happened when an exception was thrown inside the callback given to auseLayoutEffect()
. It just ran for ever, consuming more and more memory. Hope this helps someone in this thread šHave no idea why this worked for me but I had accidentally removed
'js'
frommoduleFileExtensions
in myjest.config.ts
and then thatās when my heap issues started going wild. But when I added it back, my heap issues went away.So now I have
moduleFileExtensions: ['js', 'ts']
. Hopefully this helps someone!We see this regularly on our tests at https://github.com/renovatebot/renovate
I tried a few different solutions that didnāt work for me:
--max-old-space-size
--no-compilation-cache
--runInBand
--expose-gc
What did work for me:
Limiting the idle memory per worker using the flag
workerIdleMemoryLimit
Iām also limiting the number of workers so maybe it was a combination of the solutions.
I also had this issue, and I had to change my collectCoverageFrom array.
Before I had:
"collectCoverageFrom": [ "**/*.{js,jsx}", "!**/node_modules/**", ... ]
But for some reason it seemed to still be running over the node_modules folder. I changed this by taking away the exclude node_modules and changing my patterns to include just the directories I needed:"collectCoverageFrom": [ "<rootDir>/*.js", "<rootDir>/components/**",
I donāt like having to do this, because it may be easy to miss future coverage, but all attempts to exclude the node_modules folder either had other errors or reintroduced the āout of memoryā issue.I have very similar issue,
My test:
test should fail within 250 ms (timeout), but it takes 40 sec and it spits out message:
Somehow I believe stack trace points to
printComplexValue
. I also triedtoMatchObject
, but exactly the same.Jest: v26.6.3
Oh
--clear-cache
fixed it.Same problem in Node 16.20.2 and Jest 29.7.0
What did not work for me:
This worked:
or
I ran into the issue with Node 16.19.0 and Jest 29.7.0.
What did not work for me:
What did work for me:
workerIdleMemoryLimit
to1 GB
I tried in on pure Windows and Mac and itās basically the same behaviour. The single test, which virtually does nothing, takes up about 2.5 GB of RAM. For some reasons thatās too much for my WSL, in PS and Mac the max heap seems to be higher for some reason. With
--max-old-space-size=4096
everything is āfineā, it just take 2.5 GB of RAM. But ofc, thatās an absurd amount.In Windows, jest launches as many workers as I have virtual cores (I think), but each one takes up at least 500 MB, even though thereās nothing to do for them (I run 1 test).
Maybe related: https://github.com/jestjs/jest/issues/11956
My stripped-down sample only seems to use aroud 350MB:
It does the same thing, thereās just less code not being executed.
Specifying NODE_OPTIONS=āāmax-old-space-size=2048ā helped us to solve the issue
I can prevent this error by using fake timers:
jest.test.setup.js:
jest.useFakeTimers();
i have the same issue
Same issue here as well. (using ts-jest)