Skip to content

Commit 8bbe2a3

Browse files
authored
feat(jest-environment-node): update NodeEnvironment to only set dispose symbols
1 parent 28d32c6 commit 8bbe2a3

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- `[@jest/core, @jest/test-sequencer]` [**BREAKING**] Exposes `globalConfig` & `contexts` to `TestSequencer` ([#14535](https://github.com/jestjs/jest/pull/14535), & [#14543](https://github.com/jestjs/jest/pull/14543))
1616
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade JSDOM to v22 ([#13825](https://github.com/jestjs/jest/pull/13825))
1717
- `[@jest/environment-jsdom-abstract]` Introduce new package which abstracts over the `jsdom` environment, allowing usage of custom versions of JSDOM ([#14717](https://github.com/jestjs/jest/pull/14717))
18+
- `[jest-environment-node]` Update jest environment with dispose symbols `Symbol` ([#14888](https://github.com/jestjs/jest/pull/14888))
1819
- `[@jest/fake-timers]` [**BREAKING**] Upgrade `@sinonjs/fake-timers` to v11 ([#14544](https://github.com/jestjs/jest/pull/14544))
1920
- `[@jest/fake-timers]` Exposing new modern timers function `advanceTimersToFrame()` which advances all timers by the needed milliseconds to execute callbacks currently scheduled with `requestAnimationFrame` ([#14598](https://github.com/jestjs/jest/pull/14598))
2021
- `[jest-runtime]` Exposing new modern timers function `jest.advanceTimersToFrame()` from `@jest/fake-timers` ([#14598](https://github.com/jestjs/jest/pull/14598))

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

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

75+
it('should configure dispose symbols', () => {
76+
const env = new NodeEnvironment(
77+
{
78+
globalConfig: makeGlobalConfig(),
79+
projectConfig: makeProjectConfig(),
80+
},
81+
context,
82+
);
83+
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+
}
95+
});
96+
7597
it('has modern fake timers implementation', () => {
7698
const env = new NodeEnvironment(
7799
{

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

Lines changed: 19 additions & 0 deletions
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() {
@@ -85,6 +101,9 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
85101
constructor(config: JestEnvironmentConfig, _context: EnvironmentContext) {
86102
const {projectConfig} = config;
87103
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)