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(jest-environment-node): Fix non-configurable globals #13687

Merged
merged 3 commits into from Dec 31, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

### Fixes

- `[jest-environment-node]` fix non-configurable globals ([#13687](https://github.com/facebook/jest/pull/13687))
- `[@jest/expect-utils]` `toMatchObject` should handle `Symbol` properties ([#13639](https://github.com/facebook/jest/pull/13639))
- `[jest-resolve]` Add global paths to `require.resolve.paths` ([#13633](https://github.com/facebook/jest/pull/13633))

Expand Down
74 changes: 45 additions & 29 deletions packages/jest-environment-node/src/index.ts
Expand Up @@ -78,35 +78,51 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
const contextGlobals = new Set(Object.getOwnPropertyNames(global));
for (const [nodeGlobalsKey, descriptor] of nodeGlobals) {
if (!contextGlobals.has(nodeGlobalsKey)) {
Object.defineProperty(global, nodeGlobalsKey, {
configurable: descriptor.configurable,
enumerable: descriptor.enumerable,
get() {
// @ts-expect-error: no index signature
const val = globalThis[nodeGlobalsKey] as unknown;

// override lazy getter
Object.defineProperty(global, nodeGlobalsKey, {
configurable: descriptor.configurable,
enumerable: descriptor.enumerable,
value: val,
writable:
descriptor.writable === true ||
// Node 19 makes performance non-readable. This is probably not the correct solution.
nodeGlobalsKey === 'performance',
});
return val;
},
set(val: unknown) {
// override lazy getter
Object.defineProperty(global, nodeGlobalsKey, {
configurable: descriptor.configurable,
enumerable: descriptor.enumerable,
value: val,
writable: true,
});
},
});
if (descriptor.configurable) {
Object.defineProperty(global, nodeGlobalsKey, {
configurable: true,
enumerable: descriptor.enumerable,
get() {
// @ts-expect-error: no index signature
const val = globalThis[nodeGlobalsKey] as unknown;

// override lazy getter
Object.defineProperty(global, nodeGlobalsKey, {
configurable: true,
enumerable: descriptor.enumerable,
value: val,
writable:
descriptor.writable === true ||
// Node 19 makes performance non-readable. This is probably not the correct solution.
nodeGlobalsKey === 'performance',
});
return val;
},
set(val: unknown) {
// override lazy getter
Object.defineProperty(global, nodeGlobalsKey, {
configurable: true,
enumerable: descriptor.enumerable,
value: val,
writable: true,
});
},
});
} else if ('value' in descriptor) {
Object.defineProperty(global, nodeGlobalsKey, {
configurable: false,
enumerable: descriptor.enumerable,
writable: descriptor.writable,
value: descriptor.value
});
} else {
Object.defineProperty(global, nodeGlobalsKey, {
configurable: false,
enumerable: descriptor.enumerable,
get: descriptor.get,
set: descriptor.set
});
}
}
}

Expand Down