Skip to content

Commit

Permalink
fix(jest-environment-node): Fix non-configurable globals (#13687)
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Dec 31, 2022
1 parent c95de87 commit 5c08f4f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
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

0 comments on commit 5c08f4f

Please sign in to comment.