Skip to content

Commit

Permalink
check for runtime support
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Aug 16, 2021
1 parent 4668312 commit 94d1903
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
18 changes: 10 additions & 8 deletions packages/jest-runtime/src/__tests__/runtime_require_module.test.js
Expand Up @@ -188,15 +188,17 @@ describe('Runtime requireModule', () => {
}).not.toThrow();
});

it('finds node core built-in modules with node:prefix', async () => {
const runtime = await createRuntime(__filename);
onNodeVersions('^16.0.0', () => {
it('finds node core built-in modules with node:prefix', async () => {
const runtime = await createRuntime(__filename);

expect(runtime.requireModule(runtime.__mockRootPath, 'fs')).toBe(
runtime.requireModule(runtime.__mockRootPath, 'node:fs'),
);
expect(runtime.requireModule(runtime.__mockRootPath, 'module')).toBe(
runtime.requireModule(runtime.__mockRootPath, 'node:module'),
);
expect(runtime.requireModule(runtime.__mockRootPath, 'fs')).toBe(
runtime.requireModule(runtime.__mockRootPath, 'node:fs'),
);
expect(runtime.requireModule(runtime.__mockRootPath, 'module')).toBe(
runtime.requireModule(runtime.__mockRootPath, 'node:module'),
);
});
});

it('finds and loads JSON files without file extension', async () => {
Expand Down
45 changes: 37 additions & 8 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -149,6 +149,28 @@ const supportsTopLevelAwait =
}
})();

const supportsNodeColonModulePrefixInRequire = (() => {
try {
require('node:fs');

return true;
} catch {
return false;
}
})();

const supportsNodeColonModulePrefixInImport = (async () => {
try {
// @ts-expect-error
// eslint-disable-next-line import/no-unresolved
await import('node:fs');

return true;
} catch {
return false;
}
})();

export default class Runtime {
private readonly _cacheFS: Map<string, string>;
private readonly _config: Config.ProjectConfig;
Expand Down Expand Up @@ -411,7 +433,7 @@ export default class Runtime {
);

if (this._resolver.isCoreModule(modulePath)) {
const core = this._importCoreModule(modulePath, context);
const core = await this._importCoreModule(modulePath, context);
this._esmoduleRegistry.set(cacheKey, core);

transformResolve();
Expand Down Expand Up @@ -645,7 +667,10 @@ export default class Runtime {
}

if (moduleName && this._resolver.isCoreModule(moduleName)) {
return this._requireCoreModule(moduleName);
return this._requireCoreModule(
moduleName,
supportsNodeColonModulePrefixInRequire,
);
}

if (!modulePath) {
Expand Down Expand Up @@ -1331,10 +1356,11 @@ export default class Runtime {
}
}

private _requireCoreModule(moduleName: string) {
const moduleWithoutNodePrefix = moduleName.startsWith('node:')
? moduleName.slice('node:'.length)
: moduleName;
private _requireCoreModule(moduleName: string, supportPrefix: boolean) {
const moduleWithoutNodePrefix =
supportPrefix && moduleName.startsWith('node:')
? moduleName.slice('node:'.length)
: moduleName;

if (moduleWithoutNodePrefix === 'process') {
return this._environment.global.process;
Expand All @@ -1347,8 +1373,11 @@ export default class Runtime {
return require(moduleWithoutNodePrefix);
}

private _importCoreModule(moduleName: string, context: VMContext) {
const required = this._requireCoreModule(moduleName);
private async _importCoreModule(moduleName: string, context: VMContext) {
const required = this._requireCoreModule(
moduleName,
await supportsNodeColonModulePrefixInImport,
);

const module = new SyntheticModule(
['default', ...Object.keys(required)],
Expand Down

0 comments on commit 94d1903

Please sign in to comment.