Skip to content

Commit 47e956f

Browse files
authored
fix: support import cjs from mjs (#9850)
1 parent 5179604 commit 47e956f

File tree

6 files changed

+41
-31
lines changed

6 files changed

+41
-31
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
### Fixes
88

9+
- `[jest-runtime]` Support importing CJS from ESM using `import` statements ([#9850](https://github.com/facebook/jest/pull/9850))
10+
911
### Chore & Maintenance
1012

1113
### Performance
@@ -19,8 +21,8 @@
1921
### Fixes
2022

2123
- `[expect]` Restore support for passing functions to `toHaveLength` matcher ([#9796](https://github.com/facebook/jest/pull/9796))
22-
- `[jest-circus]` Throw on nested test definitions ([#9828](https://github.com/facebook/jest/pull/9828))
2324
- `[jest-changed-files]` `--only-changed` should include staged files ([#9799](https://github.com/facebook/jest/pull/9799))
25+
- `[jest-circus]` Throw on nested test definitions ([#9828](https://github.com/facebook/jest/pull/9828))
2426
- `[jest-each]` `each` will throw an error when called with too many arguments ([#9818](https://github.com/facebook/jest/pull/9818))
2527
- `[jest-runner]` Don't print warning to stdout when using `--json` ([#9843](https://github.com/facebook/jest/pull/9843))
2628

e2e/__tests__/__snapshots__/nativeEsm.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`on node >=12.16.0 runs test with native ESM 1`] = `
44
Test Suites: 1 passed, 1 total
5-
Tests: 8 passed, 8 total
5+
Tests: 9 passed, 9 total
66
Snapshots: 0 total
77
Time: <<REPLACED>>
88
Ran all test suites.

e2e/native-esm/__tests__/native-esm.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {createRequire} from 'module';
1010
import {dirname, resolve} from 'path';
1111
import {fileURLToPath} from 'url';
1212
import staticImportedStateful from '../stateful.mjs';
13+
import staticImportedStatefulFromCjs from '../fromCjs.mjs';
1314
import {double} from '../index';
1415

1516
test('should have correct import.meta', () => {
@@ -78,6 +79,10 @@ test('import from mjs and import(mjs) should share caches', async () => {
7879
expect(staticImportedStateful()).toBe(6);
7980
});
8081

82+
test('import cjs via import statement', () => {
83+
expect(staticImportedStatefulFromCjs(4)).toBe(2);
84+
});
85+
8186
test('handle unlinked dynamic imports', async () => {
8287
const {double: deepDouble} = await import('../dynamicImport');
8388

e2e/native-esm/fromCjs.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export {default} from './commonjs.cjs';

packages/jest-resolve/src/shouldLoadAsEsm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
import {dirname, extname} from 'path';
99
// @ts-ignore: experimental, not added to the types
10-
import {SourceTextModule} from 'vm';
10+
import {SyntheticModule} from 'vm';
1111
import type {Config} from '@jest/types';
1212
import readPkgUp = require('read-pkg-up');
1313

14-
const runtimeSupportsVmModules = typeof SourceTextModule === 'function';
14+
const runtimeSupportsVmModules = typeof SyntheticModule === 'function';
1515

1616
const cachedFileLookups = new Map<string, boolean>();
1717
const cachedDirLookups = new Map<string, boolean>();

packages/jest-runtime/src/index.ts

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const EVAL_RESULT_VARIABLE = 'Object.<anonymous>';
112112

113113
type RunScriptEvalResult = {[EVAL_RESULT_VARIABLE]: ModuleWrapper};
114114

115-
const runtimeSupportsVmModules = typeof SourceTextModule === 'function';
115+
const runtimeSupportsVmModules = typeof SyntheticModule === 'function';
116116

117117
/* eslint-disable-next-line no-redeclare */
118118
class Runtime {
@@ -351,39 +351,15 @@ class Runtime {
351351
const module = new SourceTextModule(transformedFile.code, {
352352
context,
353353
identifier: modulePath,
354-
importModuleDynamically: (
355-
specifier: string,
356-
referencingModule: VMModule,
357-
) => {
358-
const resolved = this._resolveModule(
359-
referencingModule.identifier,
360-
specifier,
361-
);
362-
if (
363-
this._resolver.isCoreModule(resolved) ||
364-
this.unstable_shouldLoadAsEsm(resolved)
365-
) {
366-
return this.loadEsmModule(resolved);
367-
}
368-
369-
return this.loadCjsAsEsm(
370-
referencingModule.identifier,
371-
resolved,
372-
context,
373-
);
374-
},
354+
importModuleDynamically: this.linkModules.bind(this),
375355
initializeImportMeta(meta: ImportMeta) {
376356
meta.url = pathToFileURL(modulePath).href;
377357
},
378358
});
379359

380360
this._esmoduleRegistry.set(cacheKey, module);
381361

382-
await module.link((specifier: string, referencingModule: VMModule) =>
383-
this.loadEsmModule(
384-
this._resolveModule(referencingModule.identifier, specifier),
385-
),
386-
);
362+
await module.link(this.linkModules.bind(this));
387363

388364
await module.evaluate();
389365
}
@@ -395,6 +371,25 @@ class Runtime {
395371
return module;
396372
}
397373

374+
private async linkModules(specifier: string, referencingModule: VMModule) {
375+
const resolved = this._resolveModule(
376+
referencingModule.identifier,
377+
specifier,
378+
);
379+
if (
380+
this._resolver.isCoreModule(resolved) ||
381+
this.unstable_shouldLoadAsEsm(resolved)
382+
) {
383+
return this.loadEsmModule(resolved);
384+
}
385+
386+
return this.loadCjsAsEsm(
387+
referencingModule.identifier,
388+
resolved,
389+
referencingModule.context,
390+
);
391+
}
392+
398393
async unstable_importModule(
399394
from: Config.Path,
400395
moduleName?: string,

0 commit comments

Comments
 (0)