Skip to content

Commit

Permalink
feat(jest-environment-node): update NodeEnvironment to only set disp…
Browse files Browse the repository at this point in the history
…ose symbols
  • Loading branch information
notaphplover authored Feb 9, 2024
1 parent 28d32c6 commit 8bbe2a3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
19 changes: 19 additions & 0 deletions packages/jest-environment-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -85,6 +101,9 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
constructor(config: JestEnvironmentConfig, _context: EnvironmentContext) {
const {projectConfig} = config;
this.context = createContext();

setDisposeSymbols(this.context);

const global = runInContext(
'this',
Object.assign(this.context, projectConfig.testEnvironmentOptions),
Expand Down

0 comments on commit 8bbe2a3

Please sign in to comment.