Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/jest-util/src/__tests__/installCommonGlobals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,16 @@ 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);
}
});
27 changes: 27 additions & 0 deletions packages/jest-util/src/createProcessObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>)) {
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;
}
Loading