diff --git a/CHANGELOG.md b/CHANGELOG.md index f4060efb0059..807bd102a36e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - `[@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)) - `[jest-environment-jsdom]` [**BREAKING**] Upgrade JSDOM to v22 ([#13825](https://github.com/jestjs/jest/pull/13825)) - `[@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)) +- `[jest-environment-node]` Update jest environment with dispose symbols `Symbol` ([#14888](https://github.com/jestjs/jest/pull/14888)) - `[@jest/fake-timers]` [**BREAKING**] Upgrade `@sinonjs/fake-timers` to v11 ([#14544](https://github.com/jestjs/jest/pull/14544)) - `[@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)) - `[jest-runtime]` Exposing new modern timers function `jest.advanceTimersToFrame()` from `@jest/fake-timers` ([#14598](https://github.com/jestjs/jest/pull/14598)) diff --git a/packages/jest-environment-node/src/__tests__/node_environment.test.ts b/packages/jest-environment-node/src/__tests__/node_environment.test.ts index bd29974e2b8f..c2a79ca2e79d 100644 --- a/packages/jest-environment-node/src/__tests__/node_environment.test.ts +++ b/packages/jest-environment-node/src/__tests__/node_environment.test.ts @@ -72,6 +72,28 @@ describe('NodeEnvironment', () => { } }); + it('should configure dispose symbols', () => { + const env = new NodeEnvironment( + { + globalConfig: makeGlobalConfig(), + projectConfig: makeProjectConfig(), + }, + context, + ); + + if ('asyncDispose' in Symbol) { + expect(env.global.Symbol).toHaveProperty('asyncDispose'); + } else { + expect(env.global.Symbol).not.toHaveProperty('asyncDispose'); + } + + if ('dispose' in Symbol) { + expect(env.global.Symbol).toHaveProperty('dispose'); + } else { + expect(env.global.Symbol).not.toHaveProperty('dispose'); + } + }); + it('has modern fake timers implementation', () => { const env = new NodeEnvironment( { diff --git a/packages/jest-environment-node/src/index.ts b/packages/jest-environment-node/src/index.ts index 5b9342816d78..d7ecd4a06b4d 100644 --- a/packages/jest-environment-node/src/index.ts +++ b/packages/jest-environment-node/src/index.ts @@ -60,6 +60,22 @@ function isString(value: unknown): value is string { return typeof value === 'string'; } +function setDisposeSymbols(context: Context): void { + if ('asyncDispose' in Symbol) { + runInContext( + 'if (!"asyncDispose" in Symbol) { Symbol.asyncDispose = Symbol.for("nodejs.asyncDispose") }', + context, + ); + } + + if ('dispose' in Symbol) { + runInContext( + 'if (!"dispose" in Symbol) { Symbol.dispose = Symbol.for("nodejs.dispose") }', + context, + ); + } +} + const timerIdToRef = (id: number) => ({ id, ref() { @@ -85,6 +101,9 @@ export default class NodeEnvironment implements JestEnvironment { constructor(config: JestEnvironmentConfig, _context: EnvironmentContext) { const {projectConfig} = config; this.context = createContext(); + + setDisposeSymbols(this.context); + const global = runInContext( 'this', Object.assign(this.context, projectConfig.testEnvironmentOptions),