Skip to content

Commit 33820ad

Browse files
authored
Globals cleanup: avoid setting protection symbol when feature is off (#15684)
1 parent b00bd3c commit 33820ad

File tree

14 files changed

+178
-78
lines changed

14 files changed

+178
-78
lines changed

.github/workflows/test-nightly.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
id: cpu-cores
3434
uses: SimenB/github-actions-cpu-cores@97ba232459a8e02ff6121db9362b09661c875ab8 # v2.0.0
3535
- name: run node-env tests
36-
run: yarn test-node-env
36+
run: yarn workspace jest-environment-node test
3737
- name: run tests
3838
uses: nick-fields/retry@ce71cc2ab81d554ebbe88c79ab5975992d79ba08 # v3.0.2
3939
with:

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
id: cpu-cores
3737
uses: SimenB/github-actions-cpu-cores@97ba232459a8e02ff6121db9362b09661c875ab8 # v2.0.0
3838
- name: run node-env tests
39-
run: yarn test-node-env
39+
run: yarn workspace jest-environment-node test
4040
- name: run tests
4141
uses: nick-fields/retry@ce71cc2ab81d554ebbe88c79ab5975992d79ba08 # v3.0.2
4242
with:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Fixes
88

99
- `[jest-resolver]` Resolve builtin modules correctly ([#15683](https://github.com/jestjs/jest/pull/15683))
10+
- `[jest-environment-node, jest-util]` Avoid setting globals cleanup protection symbol when feature is off ([#15684](https://github.com/jestjs/jest/pull/15684))
1011

1112
### Chore & Maintenance
1213

jest.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default {
3838
},
3939
snapshotSerializers: [require.resolve('jest-serializer-ansi-escapes')],
4040
testEnvironmentOptions: {
41-
globalsCleanup: 'on',
41+
globalsCleanup: process.env.GLOBALS_CLEANUP ?? 'on',
4242
},
4343
testPathIgnorePatterns: [
4444
'/__arbitraries__/',

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@
113113
"test-ts": "yarn jest --config jest.config.ts.mjs",
114114
"test-types": "yarn tstyche",
115115
"test-with-type-info": "yarn jest e2e/__tests__/jest.config.ts.test.ts",
116-
"test-node-env": "yarn jest packages/jest-environment-node/src/__tests__",
117116
"test": "yarn lint && yarn jest",
118117
"typecheck": "yarn typecheck:examples && yarn typecheck:tests",
119118
"typecheck:examples": "tsc -p examples/expect-extend && tsc -p examples/typescript",

packages/jest-environment-node/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
"@jest/test-utils": "workspace:*",
3232
"clsx": "^2.1.1"
3333
},
34+
"scripts": {
35+
"test:base": "echo GLOBALS_CLEANUP=$GLOBALS_CLEANUP && yarn --cwd='../.' jest --runInBand packages/jest-environment-node/src/__tests__",
36+
"test:globals-cleanup-off": "GLOBALS_CLEANUP=off yarn test:base",
37+
"test:globals-cleanup-soft": "GLOBALS_CLEANUP=soft yarn test:base",
38+
"test:globals-cleanup-on": "GLOBALS_CLEANUP=on yarn test:base",
39+
"test": "yarn test:globals-cleanup-off && yarn test:globals-cleanup-soft && yarn test:globals-cleanup-on"
40+
},
3441
"engines": {
3542
"node": "^18.14.0 || ^20.0.0 || ^22.0.0 || >=24.0.0"
3643
},

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {AsyncLocalStorage, createHook} from 'async_hooks';
99
import {clsx} from 'clsx';
1010
import {onNodeVersions} from '@jest/test-utils';
1111

12-
describe('NodeEnvironment 2', () => {
12+
describe('Globals Cleanup 1', () => {
1313
test('dispatch event', () => {
1414
new EventTarget().dispatchEvent(new Event('foo'));
1515
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {AsyncLocalStorage, createHook} from 'async_hooks';
9+
import {clsx} from 'clsx';
10+
import {onNodeVersions} from '@jest/test-utils';
11+
12+
describe('Globals Cleanup 2', () => {
13+
test('dispatch event', () => {
14+
new EventTarget().dispatchEvent(new Event('foo'));
15+
});
16+
17+
test('set modules on global', () => {
18+
(globalThis as any).async_hooks = require('async_hooks');
19+
(globalThis as any).AsyncLocalStorage =
20+
require('async_hooks').AsyncLocalStorage;
21+
(globalThis as any).createHook = require('async_hooks').createHook;
22+
(globalThis as any).clsx = require('clsx');
23+
expect(AsyncLocalStorage).toBeDefined();
24+
expect(clsx).toBeDefined();
25+
expect(createHook).toBeDefined();
26+
expect(createHook({})).toBeDefined();
27+
expect(clsx()).toBeDefined();
28+
});
29+
30+
onNodeVersions('>=19.8.0', () => {
31+
test('use static function from core module set on global', () => {
32+
expect(AsyncLocalStorage.snapshot).toBeDefined();
33+
expect(AsyncLocalStorage.snapshot()).toBeDefined();
34+
});
35+
});
36+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
function onlyIfGlobalsCleanup(
9+
globalsCleanup: string,
10+
testBody: () => void,
11+
): void {
12+
const describeFunc =
13+
process.env.GLOBALS_CLEANUP === globalsCleanup ? describe : describe.skip;
14+
describeFunc(`GLOBALS_CLEANUP=${globalsCleanup}`, testBody);
15+
}
16+
17+
describe('Globals Cleanup 3', () => {
18+
onlyIfGlobalsCleanup('off', () => {
19+
test('assign Object prototype descriptors to a new empty object', () => {
20+
const descriptors = Object.getOwnPropertyDescriptors(
21+
Object.getPrototypeOf({}),
22+
);
23+
Object.assign({}, descriptors);
24+
});
25+
});
26+
});

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

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,8 @@
66
*/
77

88
import type {EnvironmentContext} from '@jest/environment';
9-
import {
10-
makeGlobalConfig,
11-
makeProjectConfig,
12-
onNodeVersions,
13-
} from '@jest/test-utils';
9+
import {makeGlobalConfig, makeProjectConfig} from '@jest/test-utils';
1410
import NodeEnvironment from '../';
15-
import {AsyncLocalStorage, createHook} from 'async_hooks';
16-
import {clsx} from 'clsx';
1711

1812
const context: EnvironmentContext = {
1913
console,
@@ -93,28 +87,4 @@ describe('NodeEnvironment', () => {
9387
test('TextEncoder references the same global Uint8Array constructor', () => {
9488
expect(new TextEncoder().encode('abc')).toBeInstanceOf(Uint8Array);
9589
});
96-
97-
test('dispatch event', () => {
98-
new EventTarget().dispatchEvent(new Event('foo'));
99-
});
100-
101-
test('set modules on global', () => {
102-
(globalThis as any).async_hooks = require('async_hooks');
103-
(globalThis as any).AsyncLocalStorage =
104-
require('async_hooks').AsyncLocalStorage;
105-
(globalThis as any).createHook = require('async_hooks').createHook;
106-
(globalThis as any).clsx = require('clsx');
107-
expect(AsyncLocalStorage).toBeDefined();
108-
expect(clsx).toBeDefined();
109-
expect(createHook).toBeDefined();
110-
expect(createHook({})).toBeDefined();
111-
expect(clsx()).toBeDefined();
112-
});
113-
114-
onNodeVersions('>=19.8.0', () => {
115-
test('use static function from core module set on global', () => {
116-
expect(AsyncLocalStorage.snapshot).toBeDefined();
117-
expect(AsyncLocalStorage.snapshot()).toBeDefined();
118-
});
119-
});
12090
});

0 commit comments

Comments
 (0)