diff --git a/CHANGELOG.md b/CHANGELOG.md index d9a05f43407a..7aaa13347453 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ - `[jest-environment-node]` [**BREAKING**] Add default `node` and `node-addon` conditions to `exportConditions` for `node` environment ([#11924](https://github.com/facebook/jest/pull/11924)) - `[jest-environment-node]` [**BREAKING**] Pass global config to Jest environment constructor for `node` environment ([#12461](https://github.com/facebook/jest/pull/12461)) - `[jest-environment-node]` [**BREAKING**] Second argument `context` to constructor is mandatory ([#12469](https://github.com/facebook/jest/pull/12469)) +- `[jest-environment-node]` Add all available globals to test globals, not just explicit ones ([#12642](https://github.com/facebook/jest/pull/12642)) - `[@jest/expect]` New module which extends `expect` with `jest-snapshot` matchers ([#12404](https://github.com/facebook/jest/pull/12404), [#12410](https://github.com/facebook/jest/pull/12410), [#12418](https://github.com/facebook/jest/pull/12418)) - `[@jest/expect-utils]` New module exporting utils for `expect` ([#12323](https://github.com/facebook/jest/pull/12323)) - `[@jest/fake-timers]` [**BREAKING**] Rename `timers` configuration option to `fakeTimers` ([#12572](https://github.com/facebook/jest/pull/12572)) diff --git a/packages/jest-environment-jsdom/src/index.ts b/packages/jest-environment-jsdom/src/index.ts index 30a01b4b3d58..f16e876ecefe 100644 --- a/packages/jest-environment-jsdom/src/index.ts +++ b/packages/jest-environment-jsdom/src/index.ts @@ -111,16 +111,14 @@ export default class JSDOMEnvironment implements JestEnvironment { this.moduleMocker = new ModuleMocker(global as any); - const timerConfig = { - idToRef: (id: number) => id, - refToId: (ref: number) => ref, - }; - this.fakeTimers = new LegacyFakeTimers({ config: projectConfig, global: global as unknown as typeof globalThis, moduleMocker: this.moduleMocker, - timerConfig, + timerConfig: { + idToRef: (id: number) => id, + refToId: (ref: number) => ref, + }, }); this.fakeTimersModern = new ModernFakeTimers({ diff --git a/packages/jest-environment-node/src/index.ts b/packages/jest-environment-node/src/index.ts index f6f8576bd6e6..b6bdd65d895e 100644 --- a/packages/jest-environment-node/src/index.ts +++ b/packages/jest-environment-node/src/index.ts @@ -22,6 +22,22 @@ type Timer = { unref: () => Timer; }; +// some globals we do not want, either because deprecated or we set it ourselves +const denyList = new Set([ + 'GLOBAL', + 'root', + 'global', + 'Buffer', + 'ArrayBuffer', + 'Uint8Array', +]); + +const nodeGlobals = new Set( + Object.getOwnPropertyNames(globalThis).filter( + global => !denyList.has(global), + ), +); + export default class NodeEnvironment implements JestEnvironment { context: Context | null; fakeTimers: LegacyFakeTimers | null; @@ -37,70 +53,23 @@ export default class NodeEnvironment implements JestEnvironment { 'this', Object.assign(this.context, projectConfig.testEnvironmentOptions), )); + + const contextGlobals = new Set(Object.getOwnPropertyNames(global)); + for (const nodeGlobalsKey of nodeGlobals) { + if (!contextGlobals.has(nodeGlobalsKey)) { + // @ts-expect-error + global[nodeGlobalsKey] = globalThis[nodeGlobalsKey]; + } + } + global.global = global; - global.clearInterval = clearInterval; - global.clearTimeout = clearTimeout; - global.setInterval = setInterval; - global.setTimeout = setTimeout; global.Buffer = Buffer; - global.setImmediate = setImmediate; - global.clearImmediate = clearImmediate; global.ArrayBuffer = ArrayBuffer; // TextEncoder (global or via 'util') references a Uint8Array constructor // different than the global one used by users in tests. This makes sure the // same constructor is referenced by both. global.Uint8Array = Uint8Array; - // URL and URLSearchParams are global in Node >= 10 - global.URL = URL; - global.URLSearchParams = URLSearchParams; - - // TextDecoder and TextDecoder are global in Node >= 11 - global.TextEncoder = TextEncoder; - global.TextDecoder = TextDecoder; - - // queueMicrotask is global in Node >= 11 - global.queueMicrotask = queueMicrotask; - - // AbortController is global in Node >= 15 - if (typeof AbortController !== 'undefined') { - global.AbortController = AbortController; - } - // AbortSignal is global in Node >= 15 - if (typeof AbortSignal !== 'undefined') { - global.AbortSignal = AbortSignal; - } - // Event is global in Node >= 15.4 - if (typeof Event !== 'undefined') { - global.Event = Event; - } - // EventTarget is global in Node >= 15.4 - if (typeof EventTarget !== 'undefined') { - global.EventTarget = EventTarget; - } - // MessageChannel is global in Node >= 15 - if (typeof MessageChannel !== 'undefined') { - global.MessageChannel = MessageChannel; - } - // MessageEvent is global in Node >= 15 - if (typeof MessageEvent !== 'undefined') { - global.MessageEvent = MessageEvent; - } - // performance is global in Node >= 16 - if (typeof performance !== 'undefined') { - global.performance = performance; - } - // atob and btoa are global in Node >= 16 - if (typeof atob !== 'undefined' && typeof btoa !== 'undefined') { - global.atob = atob; - global.btoa = btoa; - } - // structuredClone is global in Node >= 17 - // @ts-expect-error type definition for structuredClone is missing - if (typeof structuredClone !== 'undefined') { - // @ts-expect-error type definition for structuredClone is missing - global.structuredClone = structuredClone; - } installCommonGlobals(global, projectConfig.globals); this.moduleMocker = new ModuleMocker(global); @@ -118,16 +87,14 @@ export default class NodeEnvironment implements JestEnvironment { const timerRefToId = (timer: Timer): number | undefined => (timer && timer.id) || undefined; - const timerConfig = { - idToRef: timerIdToRef, - refToId: timerRefToId, - }; - this.fakeTimers = new LegacyFakeTimers({ config: projectConfig, global, moduleMocker: this.moduleMocker, - timerConfig, + timerConfig: { + idToRef: timerIdToRef, + refToId: timerRefToId, + }, }); this.fakeTimersModern = new ModernFakeTimers({