-
Notifications
You must be signed in to change notification settings - Fork 12k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@angular-devkit/build-angular:jest Memory Leak and slower test execution speed on CI #26823
Comments
Taking a quick look at these:
You shouldn't need
I'm guessing Jest doesn't implement https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia I think you want something like: window.matchMedia = jest.fn();
This sounds like
I'm not very familiar with forms, but this also sounds like an import issue. I think you might need to import
Jest support is still experimental, and these just aren't features we've gotten to yet. You can file separate issues for these (I don't think either of these have been filed already) to make sure they're looked at before the builder is marked stable. |
@dgp1130 thanks. I successfully resolved the errors, but I still have some questions. Initially,
This setup file is referenced in
However, the main challenge is that Jest and I've encountered a new issue that wasn't present with Karma – a memory leak and slower test execution speed on CI. Observation:
Questions:
These concerns weren't present with Karma, indicating a potential issue with Jest or its integration with Angular. EDIT: |
Currently we're exploring not exposing the underlying Jest configuration, so this shouldn't be possible. There's a bug right now where it does load config files, which is incorrect and I need to fix that at some point. I don't think mocking describe('my-test', () => {
beforeEach(() => {
Object.defineProperty(window, 'matchMedia', { /* ... */ });
});
afterEach(() => {
delete window.matchMedia;
});
it('uses matchMedia', () => {
window.matchMedia(/* ... */);
});
});
Where are you seeing this error? Is it the Jest process or Angular CLI process? If you can provide a minimal reproduction I'd be interested in taking a look. Can you share the full output where you're seeing the test identified by
Regarding comparison with Karma, Jest is not intended to be a 1:1 replacement for Karma. Jest will have a very different performance profile and may be faster or slower in some instances. I don't think the experiment is far enough along to know how they actually compare in practice. Web Test Runner is intended to be a replacement for Karma and is more likely to have directly improved performance.
Not currently, we're still early in this experiment. You're welcome to file issues more directly focused on those options. Though I don't think |
I share your thoughts on the approach, but unfortunately, using mocks in As for the memory leaks, I enabled 'detectLeaks' in Jest, and it flagged all my tests with leak issues. This output highlights a significant problem:
and error:
I'm aware that the I mean, some options to speed up process would be nice. Now each test run file by file and not in parallel. |
That's surprising to me and makes me think your test might be calling
I'm able to reproduce this one. However I'm also able to reproduce this for an empty test. describe('test', () => {
it('runs', () => {
expect(true).toBeTrue();
});
}); This fails with
Maybe I'm misunderstanding but I'm pretty sure our Jest usage is already parallelized? If you create two test files which wait 10 seconds, the total execution time is ~10 seconds, not 20, so they are running parallel.
I was under the impression our Karma builder bundles with Webpack, which should support ESM and CommonJS dependencies. I feel like this should be feasible with our Karma setup. |
Yes, as mentioned earlier, the issue with
Regarding the memory usage with Angular's builder, my experience showed it consuming significantly more memory compared to Jest. A notable difference is that the Angular builder doesn't run tests in parallel, which might contribute to this issue.
The moment when I upgraded Angular from version 15 to 16, our tests started failing with the error: "cannot use import statement outside a module". If you have any insights or examples of how Karma should be configured in Angular to address this, it would be immensely helpful. I didn’t find specific instructions in the Angular upgrade guide, but there's a growing trend of moving towards Jest in the community. |
Hi @katedoctor,
When using the Angular the Jest builder, tests are executed in parallel, The difference in consumption in memory can be attributed to various things, it would definitely help if you can provide a reproduction so that we can take a look and compare it with a plain Jest setup that you mentioned. In the CLI builder, we do use
This typically indicates that one or more files are not being processed by Webpack, unfortunately without a minimal reproduction it's hard to provide a solution. |
I guess, maybe. But nothing was changed except angular version. I couldn't find anything to change, as tests were successful but in the end got error: Chrome Headless 120.0.6099.216 (Mac OS 10.15.7) ERROR
My apologies for any confusion earlier. I have a hypothesis about the slower performance. When running tests with Jest, I've noticed it processes files in chunks. However, with Angular, the process is different – it handles files one by one. Then it ends with: "FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory". This leads me to believe that the way Angular processes test files might be different. |
I don't think we can help more here without minimal reproductions. If you can provide a reproduction of the Karma problem (would be better in a separate issue), or of the out of memory error then we can look at those individually. |
While working on a step-by-step project with Karma, I identified the root cause of Karma problem. It turns out the issue was linked to scripts included in |
It sounds like the relevant issues have been resolved so I'm going to close this issue. If you find some broken behavior and are able to create a minimal reproduction, please file a new issue and we can investigate further. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Command
test
Is this a regression?
The previous version in which this bug was not present was
15
Description
Would someone be able to help me?
I migrated to Angular 16, and to use es modules, I migrated all tests to Jest.
After hundreds of attempts to configure tests ( by the way, @angular-devkit/build-angular:jest + 'jest-preset-angular' doesn't work) I faced 3 main errors in a unit test.
№ 1
TypeError: window.matchMedia is not a function
component.spec
№ 2
NG0303: Can't bind to 'ngIf' since it isn't a known property of 'div' (used in the '_a' component template).
component.html:
and component.spec:
№ 3
NG0201: No provider for NgControl found in NodeInjector
component.spec
test config:
angular.json
package.json:
Minimal Reproduction
I have Tinymce lib which is using window.matchMedia function. I tried to mock it with jest, but it didn't work.
Exception or Error
No response
Your Environment
Anything else relevant?
Also, there is a big problem that @angular-devkit/build-angular:jest doesn't allow to run single test!
Also I can't run tests when I have symlink.
The text was updated successfully, but these errors were encountered: