From 22c176596c35aed8573c0240f8a5ac0380bc7b0a Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Thu, 22 Dec 2022 13:01:06 +0000 Subject: [PATCH 1/2] fix(jest-environment-node): Fix non-configurable globals --- packages/jest-environment-node/src/index.ts | 74 +++++++++++++-------- 1 file changed, 45 insertions(+), 29 deletions(-) diff --git a/packages/jest-environment-node/src/index.ts b/packages/jest-environment-node/src/index.ts index a48507a99e2c..f6cdf550083d 100644 --- a/packages/jest-environment-node/src/index.ts +++ b/packages/jest-environment-node/src/index.ts @@ -78,35 +78,51 @@ export default class NodeEnvironment implements JestEnvironment { 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 + }); + } } } From 2bc26cb002296d0a45731d2f4f070d046af66063 Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Thu, 22 Dec 2022 13:14:58 +0000 Subject: [PATCH 2/2] Changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07f7984aa970..d7ab5ee3bfba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixes - `[jest-resolve]` add global paths to `require.resolve.paths` ([#13633](https://github.com/facebook/jest/pull/13633)) +- `[jest-environment-node]` fix non-configurable globals ([#13687](https://github.com/facebook/jest/pull/13687)) ### Chore & Maintenance