From a3417b54ff93375dbf4e6cc95ac41d0d19b9c61d Mon Sep 17 00:00:00 2001 From: manangarg21 Date: Thu, 16 Oct 2025 19:46:22 +0530 Subject: [PATCH 1/3] Fixed the change to switch to the fallback --- .../__tests__/installCommonGlobals.test.ts | 10 +++++++ packages/jest-util/src/createProcessObject.ts | 27 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/packages/jest-util/src/__tests__/installCommonGlobals.test.ts b/packages/jest-util/src/__tests__/installCommonGlobals.test.ts index 251dce275d35..b7526c84cdd5 100644 --- a/packages/jest-util/src/__tests__/installCommonGlobals.test.ts +++ b/packages/jest-util/src/__tests__/installCommonGlobals.test.ts @@ -48,3 +48,13 @@ it('turns a V8 global object into a Node global object', () => { expect(fake).toHaveBeenCalledTimes(1); }); + +it('overrides process.features.require_module to false when present', () => { + const myGlobal = installCommonGlobals(getGlobal(), {}); + + // Some Node versions may not expose the flag; only assert if present + const features = (myGlobal.process as any).features; + if (features && Object.prototype.hasOwnProperty.call(features, 'require_module')) { + expect(features.require_module).toBe(false); + } +}); diff --git a/packages/jest-util/src/createProcessObject.ts b/packages/jest-util/src/createProcessObject.ts index 59d67c0000c0..598fa672976c 100644 --- a/packages/jest-util/src/createProcessObject.ts +++ b/packages/jest-util/src/createProcessObject.ts @@ -117,5 +117,32 @@ export default function createProcessObject(): typeof Process { }, }); + // Ensure feature flags reflect Jest's capabilities inside the VM. + // Node may expose `process.features.require_module` which signals that + // requiring ESM via `require()` is supported. Jest's runtime does not + // support requiring ESM modules through CJS `require`, so we override + // the flag to false to allow defensive code paths to behave correctly. + // + const features: unknown = (newProcess as any).features; + if (features && typeof features === 'object') { + // Only override if the host process exposes the flag + if ('require_module' in (features as Record)) { + try { + Object.defineProperty(features as object, 'require_module', { + configurable: true, + enumerable: true, + get: () => false, + }); + } catch { + // If redefining fails for any reason, fall back to direct assignment + try { + (features as any).require_module = false; + } catch { + // ignore if we cannot override + } + } + } + } + return newProcess; } From 579c4b228471e1da9eb81d26760251b0201767cd Mon Sep 17 00:00:00 2001 From: manangarg21 Date: Thu, 16 Oct 2025 20:06:51 +0530 Subject: [PATCH 2/3] Changed the linting --- .../jest-util/src/__tests__/installCommonGlobals.test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/jest-util/src/__tests__/installCommonGlobals.test.ts b/packages/jest-util/src/__tests__/installCommonGlobals.test.ts index b7526c84cdd5..1ef24160ebf1 100644 --- a/packages/jest-util/src/__tests__/installCommonGlobals.test.ts +++ b/packages/jest-util/src/__tests__/installCommonGlobals.test.ts @@ -54,7 +54,10 @@ it('overrides process.features.require_module to false when present', () => { // Some Node versions may not expose the flag; only assert if present const features = (myGlobal.process as any).features; - if (features && Object.prototype.hasOwnProperty.call(features, 'require_module')) { + if ( + features && + Object.prototype.hasOwnProperty.call(features, 'require_module') + ) { expect(features.require_module).toBe(false); } }); From bba85d769b258e1277a0797950423d4f190cf593 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 19 Nov 2025 15:17:19 +0100 Subject: [PATCH 3/3] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 564e7585c84f..9c33ef7080d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - `[jest-runtime]` Fix issue where user cannot utilize dynamic import despite specifying `--experimental-vm-modules` Node option ([#15842](https://github.com/jestjs/jest/pull/15842)) - `[jest-test-sequencer]` Fix issue where failed tests due to compilation errors not getting re-executed even with `--onlyFailures` CLI option ([#15851](https://github.com/jestjs/jest/pull/15851)) +- `[jest-util]` Make sure `process.features.require_module` is `false` ([#15867](https://github.com/jestjs/jest/pull/15867)) ### Chore & Maintenance