Skip to content

Commit 34ef8f5

Browse files
committed
#15638 Fix destruction of core nodejs modules that was introduced in #15215
1 parent 76632c6 commit 34ef8f5

File tree

8 files changed

+35
-2
lines changed

8 files changed

+35
-2
lines changed

.github/workflows/test-nightly.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ jobs:
3232
- name: Get number of CPU cores
3333
id: cpu-cores
3434
uses: SimenB/github-actions-cpu-cores@97ba232459a8e02ff6121db9362b09661c875ab8 # v2.0.0
35+
- name: run node-env tests
36+
run: yarn test-node-env
3537
- name: run tests
3638
uses: nick-fields/retry@ce71cc2ab81d554ebbe88c79ab5975992d79ba08 # v3.0.2
3739
with:

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ jobs:
3535
- name: Get number of CPU cores
3636
id: cpu-cores
3737
uses: SimenB/github-actions-cpu-cores@97ba232459a8e02ff6121db9362b09661c875ab8 # v2.0.0
38+
- name: run node-env tests
39+
run: yarn test-node-env
3840
- name: run tests
3941
uses: nick-fields/retry@ce71cc2ab81d554ebbe88c79ab5975992d79ba08 # v3.0.2
4042
with:

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
"test-ts": "yarn jest --config jest.config.ts.mjs",
113113
"test-types": "yarn tstyche",
114114
"test-with-type-info": "yarn jest e2e/__tests__/jest.config.ts.test.ts",
115+
"test-node-env": "yarn jest packages/jest-environment-node/src/__tests__",
115116
"test": "yarn lint && yarn jest",
116117
"typecheck": "yarn typecheck:examples && yarn typecheck:tests",
117118
"typecheck:examples": "tsc -p examples/expect-extend && tsc -p examples/typescript",

packages/jest-environment-node/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"jest-util": "workspace:*"
2828
},
2929
"devDependencies": {
30-
"@jest/test-utils": "workspace:*"
30+
"@jest/test-utils": "workspace:*",
31+
"clsx": "^2.0.0"
3132
},
3233
"engines": {
3334
"node": "^18.14.0 || ^20.0.0 || ^22.0.0 || >=24.0.0"

packages/jest-environment-node/src/__tests__/node_environment.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import type {EnvironmentContext} from '@jest/environment';
99
import {makeGlobalConfig, makeProjectConfig} from '@jest/test-utils';
1010
import NodeEnvironment from '../';
11+
import {AsyncLocalStorage, createHook} from 'async_hooks';
12+
import {clsx} from 'clsx';
1113

1214
const context: EnvironmentContext = {
1315
console,
@@ -91,4 +93,13 @@ describe('NodeEnvironment', () => {
9193
test('dispatch event', () => {
9294
new EventTarget().dispatchEvent(new Event('foo'));
9395
});
96+
97+
test('set modules on global', () => {
98+
(globalThis as any).AsyncLocalStorage = require('async_hooks');
99+
(globalThis as any).createHook = require('async_hooks').createHook;
100+
(globalThis as any).clsx = require('clsx');
101+
expect(AsyncLocalStorage).toBeDefined();
102+
expect(createHook).toBeDefined();
103+
expect(clsx).toBeDefined();
104+
});
94105
});

packages/jest-environment-node/src/__tests__/node_environment_2.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,20 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {AsyncLocalStorage, createHook} from 'async_hooks';
9+
import {clsx} from 'clsx';
10+
811
describe('NodeEnvironment 2', () => {
912
test('dispatch event', () => {
1013
new EventTarget().dispatchEvent(new Event('foo'));
1114
});
15+
16+
test('set modules on global', () => {
17+
(globalThis as any).AsyncLocalStorage = require('async_hooks');
18+
(globalThis as any).createHook = require('async_hooks').createHook;
19+
(globalThis as any).clsx = require('clsx');
20+
expect(AsyncLocalStorage).toBeDefined();
21+
expect(createHook).toBeDefined();
22+
expect(clsx).toBeDefined();
23+
});
1224
});

packages/jest-runtime/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
deepCyclicCopy,
5656
invariant,
5757
isNonNullable,
58+
protectProperties,
5859
} from 'jest-util';
5960
import {
6061
createOutsideJestVmPath,
@@ -1767,7 +1768,9 @@ export default class Runtime {
17671768
return this._getMockedNativeModule();
17681769
}
17691770

1770-
return require(moduleName);
1771+
const coreModule = require(moduleName);
1772+
protectProperties(coreModule);
1773+
return coreModule;
17711774
}
17721775

17731776
private _importCoreModule(moduleName: string, context: VMContext) {

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13869,6 +13869,7 @@ __metadata:
1386913869
"@jest/test-utils": "workspace:*"
1387013870
"@jest/types": "workspace:*"
1387113871
"@types/node": "*"
13872+
clsx: ^2.0.0
1387213873
jest-mock: "workspace:*"
1387313874
jest-util: "workspace:*"
1387413875
languageName: unknown

0 commit comments

Comments
 (0)