Skip to content

Commit

Permalink
fix detection of import()
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Aug 27, 2021
1 parent c879ca9 commit 1865950
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {execSync} from 'child_process';
import * as nativeModule from 'module';
import * as path from 'path';
import {URL, fileURLToPath, pathToFileURL} from 'url';
Expand Down Expand Up @@ -159,16 +160,12 @@ const supportsNodeColonModulePrefixInRequire = (() => {
}
})();

const supportsNodeColonModulePrefixInImport = (async () => {
try {
// @ts-expect-error
// eslint-disable-next-line import/no-unresolved
await import('node:fs');
const supportsNodeColonModulePrefixInImport = (() => {
const res = execSync(
`node --eval 'import("node:fs").then(() => console.log(true), () => console.log(false));'`,
);

return true;
} catch {
return false;
}
return res.toString().trim() === 'true';
})();

export default class Runtime {
Expand Down Expand Up @@ -433,7 +430,7 @@ export default class Runtime {
);

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

transformResolve();
Expand Down Expand Up @@ -1375,10 +1372,10 @@ export default class Runtime {
return require(moduleWithoutNodePrefix);
}

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

const module = new SyntheticModule(
Expand Down

0 comments on commit 1865950

Please sign in to comment.