Skip to content

Commit 5247e1f

Browse files
authored
feat: add all available globals to test globals, not just explicit ones (#12642)
1 parent c12d444 commit 5247e1f

File tree

3 files changed

+34
-68
lines changed

3 files changed

+34
-68
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- `[jest-environment-node]` [**BREAKING**] Add default `node` and `node-addon` conditions to `exportConditions` for `node` environment ([#11924](https://github.com/facebook/jest/pull/11924))
2525
- `[jest-environment-node]` [**BREAKING**] Pass global config to Jest environment constructor for `node` environment ([#12461](https://github.com/facebook/jest/pull/12461))
2626
- `[jest-environment-node]` [**BREAKING**] Second argument `context` to constructor is mandatory ([#12469](https://github.com/facebook/jest/pull/12469))
27+
- `[jest-environment-node]` Add all available globals to test globals, not just explicit ones ([#12642](https://github.com/facebook/jest/pull/12642))
2728
- `[@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))
2829
- `[@jest/expect-utils]` New module exporting utils for `expect` ([#12323](https://github.com/facebook/jest/pull/12323))
2930
- `[@jest/fake-timers]` [**BREAKING**] Rename `timers` configuration option to `fakeTimers` ([#12572](https://github.com/facebook/jest/pull/12572))

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,14 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
111111

112112
this.moduleMocker = new ModuleMocker(global as any);
113113

114-
const timerConfig = {
115-
idToRef: (id: number) => id,
116-
refToId: (ref: number) => ref,
117-
};
118-
119114
this.fakeTimers = new LegacyFakeTimers({
120115
config: projectConfig,
121116
global: global as unknown as typeof globalThis,
122117
moduleMocker: this.moduleMocker,
123-
timerConfig,
118+
timerConfig: {
119+
idToRef: (id: number) => id,
120+
refToId: (ref: number) => ref,
121+
},
124122
});
125123

126124
this.fakeTimersModern = new ModernFakeTimers({

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

Lines changed: 29 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ type Timer = {
2222
unref: () => Timer;
2323
};
2424

25+
// some globals we do not want, either because deprecated or we set it ourselves
26+
const denyList = new Set([
27+
'GLOBAL',
28+
'root',
29+
'global',
30+
'Buffer',
31+
'ArrayBuffer',
32+
'Uint8Array',
33+
]);
34+
35+
const nodeGlobals = new Set(
36+
Object.getOwnPropertyNames(globalThis).filter(
37+
global => !denyList.has(global),
38+
),
39+
);
40+
2541
export default class NodeEnvironment implements JestEnvironment<Timer> {
2642
context: Context | null;
2743
fakeTimers: LegacyFakeTimers<Timer> | null;
@@ -37,70 +53,23 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
3753
'this',
3854
Object.assign(this.context, projectConfig.testEnvironmentOptions),
3955
));
56+
57+
const contextGlobals = new Set(Object.getOwnPropertyNames(global));
58+
for (const nodeGlobalsKey of nodeGlobals) {
59+
if (!contextGlobals.has(nodeGlobalsKey)) {
60+
// @ts-expect-error
61+
global[nodeGlobalsKey] = globalThis[nodeGlobalsKey];
62+
}
63+
}
64+
4065
global.global = global;
41-
global.clearInterval = clearInterval;
42-
global.clearTimeout = clearTimeout;
43-
global.setInterval = setInterval;
44-
global.setTimeout = setTimeout;
4566
global.Buffer = Buffer;
46-
global.setImmediate = setImmediate;
47-
global.clearImmediate = clearImmediate;
4867
global.ArrayBuffer = ArrayBuffer;
4968
// TextEncoder (global or via 'util') references a Uint8Array constructor
5069
// different than the global one used by users in tests. This makes sure the
5170
// same constructor is referenced by both.
5271
global.Uint8Array = Uint8Array;
5372

54-
// URL and URLSearchParams are global in Node >= 10
55-
global.URL = URL;
56-
global.URLSearchParams = URLSearchParams;
57-
58-
// TextDecoder and TextDecoder are global in Node >= 11
59-
global.TextEncoder = TextEncoder;
60-
global.TextDecoder = TextDecoder;
61-
62-
// queueMicrotask is global in Node >= 11
63-
global.queueMicrotask = queueMicrotask;
64-
65-
// AbortController is global in Node >= 15
66-
if (typeof AbortController !== 'undefined') {
67-
global.AbortController = AbortController;
68-
}
69-
// AbortSignal is global in Node >= 15
70-
if (typeof AbortSignal !== 'undefined') {
71-
global.AbortSignal = AbortSignal;
72-
}
73-
// Event is global in Node >= 15.4
74-
if (typeof Event !== 'undefined') {
75-
global.Event = Event;
76-
}
77-
// EventTarget is global in Node >= 15.4
78-
if (typeof EventTarget !== 'undefined') {
79-
global.EventTarget = EventTarget;
80-
}
81-
// MessageChannel is global in Node >= 15
82-
if (typeof MessageChannel !== 'undefined') {
83-
global.MessageChannel = MessageChannel;
84-
}
85-
// MessageEvent is global in Node >= 15
86-
if (typeof MessageEvent !== 'undefined') {
87-
global.MessageEvent = MessageEvent;
88-
}
89-
// performance is global in Node >= 16
90-
if (typeof performance !== 'undefined') {
91-
global.performance = performance;
92-
}
93-
// atob and btoa are global in Node >= 16
94-
if (typeof atob !== 'undefined' && typeof btoa !== 'undefined') {
95-
global.atob = atob;
96-
global.btoa = btoa;
97-
}
98-
// structuredClone is global in Node >= 17
99-
// @ts-expect-error type definition for structuredClone is missing
100-
if (typeof structuredClone !== 'undefined') {
101-
// @ts-expect-error type definition for structuredClone is missing
102-
global.structuredClone = structuredClone;
103-
}
10473
installCommonGlobals(global, projectConfig.globals);
10574

10675
this.moduleMocker = new ModuleMocker(global);
@@ -118,16 +87,14 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
11887
const timerRefToId = (timer: Timer): number | undefined =>
11988
(timer && timer.id) || undefined;
12089

121-
const timerConfig = {
122-
idToRef: timerIdToRef,
123-
refToId: timerRefToId,
124-
};
125-
12690
this.fakeTimers = new LegacyFakeTimers({
12791
config: projectConfig,
12892
global,
12993
moduleMocker: this.moduleMocker,
130-
timerConfig,
94+
timerConfig: {
95+
idToRef: timerIdToRef,
96+
refToId: timerRefToId,
97+
},
13198
});
13299

133100
this.fakeTimersModern = new ModernFakeTimers({

0 commit comments

Comments
 (0)