jest-preset-angular: [Bug]: Slow jest test memory leak
Version
13.1.0
Steps to reproduce
Clone repo https://github.com/vespertilian/jest-preset-angular-mem-usage
npm install
npm run test-jest
Expected behavior
I would expect for simple tests memory to not increase at an alarming rate.
Actual behavior
Testing Angular seem to leak memory which makes the tests run really slow on older computers when you have multiple libraries
Some more info here NX
https://github.com/nrwl/nx/issues/18926
Thanks for all your had work on this!
Additional context
No response
Environment
System:
OS: Windows 10 10.0.22621
CPU: (32) x64 13th Gen Intel(R) Core(TM) i9-13900HX
Binaries:
Node: 18.17.1 - C:\Program Files\nodejs\node.EXE
npm: 9.6.7 - C:\Program Files\nodejs\npm.CMD
npmPackages:
jest: ^29.6.4 => 29.6.4
This is the faster computer the other computers we have are a lot slower
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 7
- Comments: 35
We will try to add type checking to that mode via a new flag and enable by default in the next major version
I am experiencing this problem as well in my nx workspace with a mix of Angular and NestJS apps/libraries. During development it is hardly noticed that the tests leak memory. I am not using the most recent versions of any package used, but I have read multiple times that this issue prevailed through multiple versions of Angular and nx. My investigation is described in the following notes:
The CI pipeline executes all tests in the workspace by running
nx run-many --all --target=test. Eventually, the runners were hitting their memory limits and I began investigating their memory usage.First step is to demonstrate the growing memory usage. We can execute jest directly, make it execute tests one at a time and log the heap usage:
./node_modules/.bin/jest --runInBand --logHeapUsageFor apps with many tests, the memory usage hits the limits of the pipeline runners but not my local machine, so i can verify for sure that the memory usage is way higher than it should be (several gigabytes at some point).
To narrow it down, I have an example app with just a couple of tests. It consistently shows increasing memory usage with some ups and downs of course.
I read about similar problems online and found a couple of approaches that I all tried, most importantly running the garbage collection manually with
--expose-gcand addingafterAll(() => global.gc && global.gc())to the global jest setup. These were good for confirming the issue, but did not help solving it, so I continued to narrow it down.Next I ran the test command for a single test file:
The test runs successfully. However, jest has the experimental feature
--detectLeaks:This is an experimental feature of course, but at this point it is all I have available. For the next step, we will look at the test and try to locate the problem by eliminating code:
All Angular-related code is removed and the memory issue still exists. Next, I removed the
jest-preset-angularimport from thetest-setup.tsfile in the project directory:Now the test succeeds without memory errors:
My result so far is: it seems that
jest-preset-angularinside my quite standard jest project causes the memory leaks.For more information, I also tried debugging the memory usage of node directly with
node --inspect-brk --expose-gc ./node_modules/.bin/jest /apps/error-logging-app-frontend/ --runInBand --logHeapUsageand using the Chrome debugger tools. It revealed large memory allocations for angular source files, compiled code and so on. I was looking for patterns of repeated allocations of the same data, but in the tens of thousands of allocations to scroll through, I haven’t found anything revealing yet.I will gladly go into more detail on each of my steps, any help to reduce the memory usage is greatly appreciated.
@BernhardBehrendt
I have been looking at it again today. No luck yet. I will probably do a bit more on Monday or Tuesday.
Weirdly this
node --no-compilation-cache --expose-gc node_modules/jest/bin/jest --logHeapUsageSeems to fix the issue in my test repo but you need --expose-gc ass well as --no-compilation-cache
Are you using NX or just the Angular CLI?
I’ve tried to run your reproducing repo with chrome inspector and do not see any memory leaks. Yes, jest reports memory growth for each file BUT it is normal behaviour because of the Garbage Collector didn’t triggered yet. If i manually trigger GC then memory footprint becomes ~160MB.
The slowness that i see happens because of for each spec file Jest executes
setup-jest.jsfile that does this:As we can see that for each simple spec file:
we bootstrap the Angulars testing module.
Without extra bootstrap logic tests pass in 13 seconds instead of 40 seconds in my laptop.
Just to confirm my assumptions regarding memory leak we can launch nodejs instance with the hard limit by available memory to force V8 GC to run much frequently. Lets limit heap size to 300MB:
@nb-midwestern there are 2 main AST transformers need to be rewritten in Rust to work with
swcI think the repo you gave only focuses on HTML part. The transformers we need focus on component source code part, mainly processing
tscodes.I’m working with Angular team on this https://github.com/thymikee/jest-preset-angular/pull/2615 which I think will be the recommended way to go.
I believe this is happening for me as well. I’m using the latest Nx release (19) and node (22) and every test that runs seems to get linearly slower. Or if I watch heap usage, it linearly grows. Are we going to see any progress on this anytime soon?
@jpv-os I have narrowed down exactly to the same point before seeing your post actually. jest-preset-angular in the setup-jest will show the memory leak while using --detectLeaks. Did you find any solution?
Thanks!