Skip to content

Commit

Permalink
feat: add all available globals to test globals, not just explicit on…
Browse files Browse the repository at this point in the history
…es (#12642)
  • Loading branch information
SimenB committed Apr 7, 2022
1 parent c12d444 commit 5247e1f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 68 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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))
Expand Down
10 changes: 4 additions & 6 deletions packages/jest-environment-jsdom/src/index.ts
Expand Up @@ -111,16 +111,14 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {

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({
Expand Down
91 changes: 29 additions & 62 deletions packages/jest-environment-node/src/index.ts
Expand Up @@ -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<Timer> {
context: Context | null;
fakeTimers: LegacyFakeTimers<Timer> | null;
Expand All @@ -37,70 +53,23 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
'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);
Expand All @@ -118,16 +87,14 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
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({
Expand Down

0 comments on commit 5247e1f

Please sign in to comment.