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

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?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 37
  • Comments: 27 (6 by maintainers)

Most upvoted comments

Hi there

We have the same issue in our project and i first openend https://github.com/thymikee/jest-preset-angular/issues/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 https://github.com/angular/angular/issues/45776.

Lets see if they have some insights.

Same here. The issue as far I can see is related to Jest and not angular nor jest-preset-angular. https://github.com/angular/angular/issues/45776#issuecomment-1143552218

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 }

@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.

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.