Skip to content

Commit 8e50bf5

Browse files
committed
feat(jest-environment-node): update NodeEnvironment to only set dispose symbols
1 parent 1c71780 commit 8e50bf5

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
- `[jest-snapshot]` [**BREAKING**] Add support for [Error causes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) in snapshots ([#13965](https://github.com/facebook/jest/pull/13965))
2525
- `[jest-snapshot]` Support Prettier 3 ([#14566](https://github.com/facebook/jest/pull/14566))
2626
- `[pretty-format]` [**BREAKING**] Do not render empty string children (`''`) in React plugin ([#14470](https://github.com/facebook/jest/pull/14470))
27-
- `[jest-environment-node]` Update jest environment with host global `Symbol` ([#14888](https://github.com/jestjs/jest/pull/14888))
27+
- `[jest-environment-node]` Update jest environment with dispose symbols `Symbol` ([#14888](https://github.com/jestjs/jest/pull/14888))
2828

2929
### Fixes
3030

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe('NodeEnvironment', () => {
7272
}
7373
});
7474

75-
it('should configure host global Symbol', () => {
75+
it('should configure dispose symbols', () => {
7676
const env = new NodeEnvironment(
7777
{
7878
globalConfig: makeGlobalConfig(),
@@ -81,8 +81,17 @@ describe('NodeEnvironment', () => {
8181
context,
8282
);
8383

84-
expect(env.global.Symbol).toBeDefined();
85-
expect(env.global.Symbol).toStrictEqual(Symbol);
84+
if ('asyncDispose' in Symbol) {
85+
expect(env.global.Symbol).toHaveProperty('asyncDispose');
86+
} else {
87+
expect(env.global.Symbol).not.toHaveProperty('asyncDispose');
88+
}
89+
90+
if ('dispose' in Symbol) {
91+
expect(env.global.Symbol).toHaveProperty('dispose');
92+
} else {
93+
expect(env.global.Symbol).not.toHaveProperty('dispose');
94+
}
8695
});
8796

8897
it('has modern fake timers implementation', () => {

packages/jest-environment-node/src/index.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ function isString(value: unknown): value is string {
6060
return typeof value === 'string';
6161
}
6262

63+
function setDisposeSymbols(context: Context): void {
64+
if ('asyncDispose' in Symbol) {
65+
runInContext(
66+
'if (!"asyncDispose" in Symbol) { Symbol.asyncDispose = Symbol.for("nodejs.asyncDispose") }',
67+
context,
68+
);
69+
}
70+
71+
if ('dispose' in Symbol) {
72+
runInContext(
73+
'if (!"dispose" in Symbol) { Symbol.dispose = Symbol.for("nodejs.dispose") }',
74+
context,
75+
);
76+
}
77+
}
78+
6379
const timerIdToRef = (id: number) => ({
6480
id,
6581
ref() {
@@ -84,7 +100,10 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
84100
// while `context` is unused, it should always be passed
85101
constructor(config: JestEnvironmentConfig, _context: EnvironmentContext) {
86102
const {projectConfig} = config;
87-
this.context = createContext({Symbol});
103+
this.context = createContext();
104+
105+
setDisposeSymbols(this.context);
106+
88107
const global = runInContext(
89108
'this',
90109
Object.assign(this.context, projectConfig.testEnvironmentOptions),

0 commit comments

Comments
 (0)