Skip to content

Commit 4197480

Browse files
authored
Issue #15865 : Fixed the change to switch to the fallback (#15867)
1 parent a785b90 commit 4197480

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- `[jest-reporters]` Fix issue where console output not displayed for GHA reporter even with `silent: false` option ([#15864](https://github.com/jestjs/jest/pull/15864))
1111
- `[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))
1212
- `[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))
13+
- `[jest-util]` Make sure `process.features.require_module` is `false` ([#15867](https://github.com/jestjs/jest/pull/15867))
1314

1415
### Chore & Maintenance
1516

packages/jest-util/src/__tests__/installCommonGlobals.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,16 @@ it('turns a V8 global object into a Node global object', () => {
4848

4949
expect(fake).toHaveBeenCalledTimes(1);
5050
});
51+
52+
it('overrides process.features.require_module to false when present', () => {
53+
const myGlobal = installCommonGlobals(getGlobal(), {});
54+
55+
// Some Node versions may not expose the flag; only assert if present
56+
const features = (myGlobal.process as any).features;
57+
if (
58+
features &&
59+
Object.prototype.hasOwnProperty.call(features, 'require_module')
60+
) {
61+
expect(features.require_module).toBe(false);
62+
}
63+
});

packages/jest-util/src/createProcessObject.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,32 @@ export default function createProcessObject(): typeof Process {
117117
},
118118
});
119119

120+
// Ensure feature flags reflect Jest's capabilities inside the VM.
121+
// Node may expose `process.features.require_module` which signals that
122+
// requiring ESM via `require()` is supported. Jest's runtime does not
123+
// support requiring ESM modules through CJS `require`, so we override
124+
// the flag to false to allow defensive code paths to behave correctly.
125+
//
126+
const features: unknown = (newProcess as any).features;
127+
if (features && typeof features === 'object') {
128+
// Only override if the host process exposes the flag
129+
if ('require_module' in (features as Record<string, unknown>)) {
130+
try {
131+
Object.defineProperty(features as object, 'require_module', {
132+
configurable: true,
133+
enumerable: true,
134+
get: () => false,
135+
});
136+
} catch {
137+
// If redefining fails for any reason, fall back to direct assignment
138+
try {
139+
(features as any).require_module = false;
140+
} catch {
141+
// ignore if we cannot override
142+
}
143+
}
144+
}
145+
}
146+
120147
return newProcess;
121148
}

0 commit comments

Comments
 (0)