Skip to content

Commit

Permalink
fix: lazily define node env globals (#12696)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Apr 20, 2022
1 parent 58af3c1 commit 78d4088
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -26,7 +26,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-environment-node]` Add all available globals to test globals, not just explicit ones ([#12642](https://github.com/facebook/jest/pull/12642), [#12696](https://github.com/facebook/jest/pull/12696))
- `[@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
53 changes: 46 additions & 7 deletions packages/jest-environment-node/src/index.ts
Expand Up @@ -30,12 +30,27 @@ const denyList = new Set([
'Buffer',
'ArrayBuffer',
'Uint8Array',
// if env is loaded within a jest test
'jest-symbol-do-not-touch',
]);

const nodeGlobals = new Set(
Object.getOwnPropertyNames(globalThis).filter(
global => !denyList.has(global),
),
const nodeGlobals = new Map(
Object.getOwnPropertyNames(globalThis)
.filter(global => !denyList.has(global))
.map(nodeGlobalsKey => {
const descriptor = Object.getOwnPropertyDescriptor(
globalThis,
nodeGlobalsKey,
);

if (!descriptor) {
throw new Error(
`No property descriptor for ${nodeGlobalsKey}, this is a bug in Jest.`,
);
}

return [nodeGlobalsKey, descriptor];
}),
);

export default class NodeEnvironment implements JestEnvironment<Timer> {
Expand All @@ -55,10 +70,34 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
));

const contextGlobals = new Set(Object.getOwnPropertyNames(global));
for (const nodeGlobalsKey of nodeGlobals) {
for (const [nodeGlobalsKey, descriptor] of nodeGlobals) {
if (!contextGlobals.has(nodeGlobalsKey)) {
// @ts-expect-error
global[nodeGlobalsKey] = globalThis[nodeGlobalsKey];
Object.defineProperty(global, nodeGlobalsKey, {
configurable: descriptor.configurable,
enumerable: descriptor.enumerable,
get() {
// @ts-expect-error
const val = globalThis[nodeGlobalsKey];

// override lazy getter
Object.defineProperty(global, nodeGlobalsKey, {
configurable: descriptor.configurable,
enumerable: descriptor.enumerable,
value: val,
writable: descriptor.writable,
});
return val;
},
set(val) {
// override lazy getter
Object.defineProperty(global, nodeGlobalsKey, {
configurable: descriptor.configurable,
enumerable: descriptor.enumerable,
value: val,
writable: true,
});
},
});
}
}

Expand Down

0 comments on commit 78d4088

Please sign in to comment.