Skip to content

Commit f73c098

Browse files
authored
fix(runtime): user can use dynamic import in code when running tests (#15842)
1 parent 855864e commit f73c098

File tree

6 files changed

+33
-7
lines changed

6 files changed

+33
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## main
22

3+
## Fixes
4+
5+
- `[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))
6+
37
## 30.2.0
48

59
### Chore & Maintenance

e2e/__tests__/nativeEsmTypescript.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ test('runs TS test with native ESM', () => {
1717

1818
expect(exitCode).toBe(0);
1919

20-
expect(json.numTotalTests).toBe(2);
21-
expect(json.numPassedTests).toBe(2);
20+
expect(json.numTotalTests).toBe(3);
21+
expect(json.numPassedTests).toBe(3);
2222
});

e2e/native-esm-typescript/__tests__/double.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {double} from '../double';
8+
import {double, doubleWithDynamicImport} from '../double';
99

1010
test('test double', () => {
1111
expect(double(2)).toBe(4);
@@ -14,3 +14,8 @@ test('test double', () => {
1414
test('test import.meta', () => {
1515
expect(typeof import.meta.url).toBe('string');
1616
});
17+
18+
// Source: https://github.com/jestjs/jest/issues/15823
19+
test('test double with dynamic import', () => {
20+
expect(doubleWithDynamicImport(2)).toBe(4);
21+
});

e2e/native-esm-typescript/double.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@
88
export function double(num: number): number {
99
return num * 2;
1010
}
11+
12+
export function doubleWithDynamicImport(
13+
num: number,
14+
): typeof import('./doubleType') {
15+
return (num * 2) as typeof import('./doubleType');
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
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 type Double = number;

packages/jest-runtime/src/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import {
6464
} from './helpers';
6565

6666
const esmIsAvailable = typeof SourceTextModule === 'function';
67+
const supportsDynamicImport = esmIsAvailable;
6768

6869
const dataURIRegex =
6970
/^data:(?<mime>text\/javascript|application\/json|application\/wasm)(?:;(?<encoding>charset=utf-8|base64))?,(?<code>.*)$/;
@@ -580,7 +581,7 @@ export default class Runtime {
580581
// @ts-expect-error -- exiting
581582
return;
582583
}
583-
if (this.isInsideTestCode === false) {
584+
if (this.isInsideTestCode === false && !supportsDynamicImport) {
584585
throw new ReferenceError(
585586
'You are trying to `import` a file outside of the scope of the test code.',
586587
);
@@ -742,7 +743,7 @@ export default class Runtime {
742743
process.exitCode = 1;
743744
return;
744745
}
745-
if (this.isInsideTestCode === false) {
746+
if (this.isInsideTestCode === false && !supportsDynamicImport) {
746747
throw new ReferenceError(
747748
'You are trying to `import` a file outside of the scope of the test code.',
748749
);
@@ -1563,7 +1564,7 @@ export default class Runtime {
15631564
process.exitCode = 1;
15641565
return;
15651566
}
1566-
if (this.isInsideTestCode === false) {
1567+
if (this.isInsideTestCode === false && !supportsDynamicImport) {
15671568
throw new ReferenceError(
15681569
'You are trying to `import` a file outside of the scope of the test code.',
15691570
);
@@ -2466,7 +2467,9 @@ export default class Runtime {
24662467
const stackTrace = formatStackTrace(stack, this._config, {
24672468
noStackTrace: false,
24682469
});
2469-
const formattedMessage = `\n${message}${stackTrace ? `\n${stackTrace}` : ''}`;
2470+
const formattedMessage = `\n${message}${
2471+
stackTrace ? `\n${stackTrace}` : ''
2472+
}`;
24702473
if (!this.loggedReferenceErrors.has(formattedMessage)) {
24712474
console.error(formattedMessage);
24722475
this.loggedReferenceErrors.add(formattedMessage);

0 commit comments

Comments
 (0)