Open yashsway opened 1 year ago
Thank you for reporting @yashsway! 🙂
The stack trace doesn't seem to say anything about what caused it inside Happy DOM, so it's impossible to reproduce based on that. I don't think the amount of tests is the reason.
Do you have a repo or some reproducible example?
@capricorn86 I'm glad to help!
ah bummer :( I can't share the repo unfortunately. Hmm I'll have to figure out how to reproduce this separately. The only parameter I changed between running the tests one way or another was swapping out jsdom
for happy-dom
so I was sure that was the issue but maybe it's something else. I'll get back to you!
@yashsway I'm a bit worried about the memory leak.
Would it be possible to run a profiler on your tests?
node --inspect-brk ./node_modules/.bin/jest --env=@happy-dom/jest-environment --runInBand
Another thing you can try is to look at tests that was running when it happened and try to run them in isolation.
I don't remember how it looks in Jest, but Vitest shows a list of all current tests and which ones that are running.
@capricorn86 Hi there, facing the same issue, I've made a screenshot of the JS heap. This is after about 10 test suites I guess. It was always rising and never had the chance to drop as it hit 4GB.
@capricorn86 I've created a repo to reproduce a leak: https://github.com/illandril/happy-dom-leak
My reproduction uses @testing-library/react
, so it might only be some interaction between happy-dom and testing-library that leads to the leak (I haven't yet had time to investigate deeply to figure out specifically what is causing the leak, only that the exact same setup but swapping out happy-dom with jsdom works w/o a leak).
npm run test:d:happy
and npm run test:d:jsdom
runs the tests with happy-dom and jsdom respectively with the --detectLeaks
flag - jest's leak detector complains for happy-dom but not jsdom.
npm run test:l:happy
and npm run test:l:jsdom
runs the tests with happy-dom and jsdom respectively with the --logHeapUsage
flag - on my device, heap size starts at 38 MB for happy-dom and grows by ~2MB per test file, ending at 83 MB; for jsdom, heap starts higher (46 MB), but it levels out quickly at a peak of ~55 MB.
% npm run test:d:jsdom
> happy-dom-leak@1.0.0 test:d:jsdom
> node --expose-gc ./node_modules/.bin/jest --runInBand --detectLeaks --test-environment=jest-environment-jsdom
PASS src/z.test.tsx
...
PASS src/q.test.tsx
Test Suites: 26 passed, 26 total
Tests: 26 passed, 26 total
Snapshots: 0 total
Time: 6.724 s
Ran all test suites.
% npm run test:d:happy
> happy-dom-leak@1.0.0 test:d:happy
> node --expose-gc ./node_modules/.bin/jest --runInBand --detectLeaks --test-environment=@happy-dom/jest-environment
FAIL src/z.test.tsx
● Test suite failed to run
EXPERIMENTAL FEATURE!
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.
at onResult (../node_modules/@jest/core/build/TestScheduler.js:150:18)
at ../node_modules/@jest/core/build/TestScheduler.js:254:19
at ../node_modules/emittery/index.js:363:13
at Array.map (<anonymous>)
at Emittery.emit (../node_modules/emittery/index.js:361:23)
...
Test Suites: 26 failed, 26 total
Tests: 0 total
Snapshots: 0 total
Time: 6.139 s
It looks like this has been fixed (at least for the project I was seeing this in) somewhere between 14.12.3
and 15.7.3
(most likely in all the refactoring that was done in 15.0.0
, but I didn't go back and check each version in between to verify which one specifically fixed the issue)
Describe the bug When running jest with
--env
set to@happy-dom/jest-environment
to run 812 tests (144 test suites), at the 81st test suite mark, the process runs out of memory. This issue does not occur in the previous configuration when running tests withjsdom
.To Reproduce Steps to reproduce the behavior:
jest test --env=@happy-dom/jest-environment --maxWorkers=2 --silent
Expected behavior All the tests should run with a pass/fail.
Device:
Additional context n/a