Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lazily define node env globals #12696

Merged
merged 2 commits into from Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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