Skip to content
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

V13 upgrade : Jest has detected the following 2 open handles potentially keeping Jest from exiting #7843

Closed
dean-g opened this issue Nov 22, 2021 · 27 comments
Assignees
Labels
outdated scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx stale type: bug

Comments

@dean-g
Copy link

dean-g commented Nov 22, 2021

Hi
I am getting the following error on JEST test execution after the angular v13 upgrade,

`Jest has detected the following 2 open handles potentially keeping Jest from exiting:

● MESSAGEPORT

> 1 | import 'jest-preset-angular/setup-jest';
    | ^
  2 |

  at startWorkerThreadService (../../../../node_modules/jest-preset-angular/node_modules/esbuild/lib/main.js:1978:48)
  at ScriptTransformer.transformSource (../../../../node_modules/@jest/transform/build/ScriptTransformer.js:612:31)
  at ScriptTransformer._transformAndBuildScript (../../../../node_modules/@jest/transform/build/ScriptTransformer.js:758:40)
  at ScriptTransformer.transform (../../../../node_modules/@jest/transform/build/ScriptTransformer.js:815:19)
  at Object.<anonymous> (../../../../node_modules/jest-preset-angular/setup-jest.js:1:100)
  at Object.<anonymous> (src/test-setup.ts:1:1)

`

Can someone please help to fix this issue?

@dean-g dean-g closed this as completed Nov 22, 2021
@dean-g dean-g reopened this Nov 22, 2021
@tomalaforge
Copy link
Contributor

@dean-g did you find something on your bug? I have exactly the same issue

@AgentEnder AgentEnder added the scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx label Dec 7, 2021
@iKrishnaSahu
Copy link

Facing the same issue.

@dean-g, @tomalaforge Did you guys find any root cause/solution/workaround?

@tomalaforge
Copy link
Contributor

@iKrishnaSahu nice to see that I'm not alone.
No, I cannot find any solution to this issue.

And I'm sure many other are facing this issue.
If you have a large test set and run it with --detectOpenHandles, you will maybe have it.

@ionutVizitiu
Copy link

I've managed to "fix" this issue by changing transformIgnorePatterns: ['node_modules/(?!.*.mjs$)'] to transformIgnorePatterns: ['/node_modules/?!@angular'], "angular" being my problematic import in this case.
I've also removed all the newly added lines with teardown: { destroyAfterEach: false }

@LPCmedia
Copy link

LPCmedia commented Jan 7, 2022

seems like we only have some guesses, random transformIgnorePatterns suggestions and bypassing the newly added automatic teardown.

anyone from Nrwl care to comment about this or what is coming to improve jest or even any active development on the jest tooling that can shed some light on this ?

@Quraian
Copy link

Quraian commented Jan 12, 2022

Just wanted to share that I'm having this issue even though I'm not using Nx.

@tomalaforge
Copy link
Contributor

still no one have found a solution to this issue?

@sbradl
Copy link

sbradl commented Jan 18, 2022

Same for me, not using Nx so it is probably a bug with jest-preset-angular?

@averath
Copy link

averath commented Jan 18, 2022

In docs of jest-preset-angular https://thymikee.github.io/jest-preset-angular/docs/guides/angular-13+/ they write:
Before upgrading to ng13 and switching to ES Modules, your setup-jest.ts file most likely uses the preset setup-jest, like the following:

// setup-jest.ts
import 'jest-preset-angular/setup-jest';
import './jest-global-mocks';

The jest-preset-angular/setup-jest file doesn't work with ESM, because it uses require. For now you should skip using this file, and replace the contents of your setup-jest.ts with:

// setup-jest.ts
import 'zone.js/fesm2015/zone-testing-bundle.min.js';
import './jest-global-mocks';

import { getTestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';

getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());

It works for me, but increase test time.

@njlaw
Copy link

njlaw commented Feb 16, 2022

I was having this issue as well after updating to nx 13.8.1 from 13.1.4 which bumped Angular to v13. I also tried @averath's suggestion from the jest-preset-angular docs, but then jest complains about potential open handles at import { getTestBed } from '@angular/core/testing';. The strange thing is that this is only affecting our CI environment; we haven't been able to reproduce it on our dev machines (we can reproduce the open handle results, but it doesn't hang).

Jest has detected the following 2 open handles potentially keeping Jest from exiting:
790 |  
791 | ●  MESSAGEPORT
792 |  
793 |   48 | // Error.stackTraceLimit = 2;
794 |   49 |
795 | > 50 | import { getTestBed } from '@angular/core/testing';
796 |      | ^
797 |   51 | import {
798 |   52 |   BrowserDynamicTestingModule,
799 |   53 |   platformBrowserDynamicTesting,
800 |  
801 | at startWorkerThreadService (../../node_modules/jest-preset-angular/node_modules/esbuild/lib/main.js:1978:48)
802 | at ScriptTransformer.transformSource (../../node_modules/@jest/transform/build/ScriptTransformer.js:612:31)
803 | at ScriptTransformer._transformAndBuildScript (../../node_modules/@jest/transform/build/ScriptTransformer.js:758:40)
804 | at ScriptTransformer.transform (../../node_modules/@jest/transform/build/ScriptTransformer.js:815:19)
805 | at Object.<anonymous> (src/test-setup.ts:50:1)
806 | at runMicrotasks (<anonymous>)
807 |  
808 |  
809 | ●  WORKER
810 |  
811 |   48 | // Error.stackTraceLimit = 2;
812 |   49 |
813 | > 50 | import { getTestBed } from '@angular/core/testing';
814 |      | ^
815 |   51 | import {
816 |   52 |   BrowserDynamicTestingModule,
817 |   53 |   platformBrowserDynamicTesting,
818 |  
819 | at startWorkerThreadService (../../node_modules/jest-preset-angular/node_modules/esbuild/lib/main.js:1979:16)
820 | at ScriptTransformer.transformSource (../../node_modules/@jest/transform/build/ScriptTransformer.js:612:31)
821 | at ScriptTransformer._transformAndBuildScript (../../node_modules/@jest/transform/build/ScriptTransformer.js:758:40)
822 | at ScriptTransformer.transform (../../node_modules/@jest/transform/build/ScriptTransformer.js:815:19)
823 | at Object.<anonymous> (src/test-setup.ts:50:1)
824 | at runMicrotasks (<anonymous>)
825 |  
826

In the meantime, I've added

{
   forceExit: true
}

to jest.config.js for my Angular projects and that seems to let the tests complete in CI.

I don't think this is necessarily an nx issue, but the particular combination of associated jest and Angular versions seem to be involved. Once we figure this out, a migration script might be nice.

@averath
Copy link

averath commented Feb 16, 2022

@njlaw We have the same problem. Unfortunately forceExit flag with open handless problem increase testing time in our CI environment by 2 minutes. Until the problem is fixed it's the only solution I see at the moment

@njlaw
Copy link

njlaw commented Feb 17, 2022

So, I finally found the issue on our end! It looks like jest memory consumption increased just enough with the ng 13 version update to cause the oom-killer to randomly kill a node process (whichever one made the request that pushed the container over the limit). We were able to resolve this by setting NODE_OPTIONS=--max-old-space-size=<TOTAL_RAM / nCPU> We might do some tweaking to see if it's faster with fewer worker threads but more memory per thread.

The dmesg logs weren't being collected in our build environment, so it took a while to track down. Hopefully this helps someone else!

@barbados-clemens
Copy link
Contributor

barbados-clemens commented Feb 18, 2022

@dean-g can you confirm what @njlaw has said about the potential fix or can you provide a reproducible repo for me to take a look into?

@barbados-clemens barbados-clemens self-assigned this Feb 18, 2022
@njlaw
Copy link

njlaw commented Feb 18, 2022

@dean-g can you confirm what @njlaw has said about the potential fix or can you provide a reproducible repo for me to take a look into?

My resolution (and root cause of the problem in my CI) is very environment specific, so it's definitely not going to be a general fix, but it might help one or two people who are running in containerized build environments and oversubscribing their memory allocation.

@chrizza87
Copy link

any updates on this topic? we are having the same problem

@barbados-clemens
Copy link
Contributor

any updates on this topic? we are having the same problem

@chrizza87 can you see if @njlaw fix worked for you?

@njlaw
Copy link

njlaw commented Mar 25, 2022

I've had this error come up before in other situations, and the solution was always to fix my tests and clean up resources they were using. My fix above was basically a coincidental exception. The nx upgrade pulled in a jest and angular upgrade which bumped up my memory usage past the limit of my build container because I was very close to it before. I.e., it was not an nx issue at all.

Under normal circumstances, I would focus on troubleshooting my tests.

@chrizza87 What have you done for troubleshooting so far?

@chrizza87
Copy link

Hey @njlaw ,

thx for the quick reply. The problem only occurs in the anuglar frontend unit tests. I thought the problem is caused by
import 'jest-preset-angular/setup-jest'; (thats what the open handles outputs)

But i will recheck the unit tests of there are any problems or missing cleanup.

@SimonMueller
Copy link

Hi there

We have the same issue in our project and i first openend thymikee/jest-preset-angular#1426 with a reproducible example but it looks more like a problem with angular itself and jest-preset-angular is only an indirection and thats why we opened angular/angular#45776.

Lets see if they have some insights.

@github-actions
Copy link

This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs.
If we missed this issue please reply to keep it active.
Thanks for being a part of the Nx community! 🙏

@ErickRodrCodes
Copy link

open

@ErickRodrCodes
Copy link

sure there is still active this one:
image

@bendehghan
Copy link

This still seems like a problem. I don't see a solution in the thread and the issue closed automatically. We need to reopen and try to solve it please.

@shaurya2904
Copy link

Hi

Still facing this issue same as @tbogard, exactly the same log, seems like the issue was closed automatically. Should be reopened and reinvestigated.

@nhustak-bc
Copy link

I am also experiencing this after the upgrade. Exact same error as @tbogard

@mehrad-rafigh
Copy link
Contributor

mehrad-rafigh commented Jan 10, 2023

Same here. The issue as far I can see is related to Jest and not angular nor jest-preset-angular.
angular/angular#45776 (comment)

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx stale type: bug
Projects
None yet
Development

No branches or pull requests