mirror of
https://github.com/hanzoai/jest.git
synced 2026-09-24 16:59:57 +00:00
fix(runtime): user can use dynamic import in code when running tests (#15842)
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
## main
|
||||
|
||||
## Fixes
|
||||
|
||||
- `[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))
|
||||
|
||||
## 30.2.0
|
||||
|
||||
### Chore & Maintenance
|
||||
|
||||
@@ -17,6 +17,6 @@ test('runs TS test with native ESM', () => {
|
||||
|
||||
expect(exitCode).toBe(0);
|
||||
|
||||
expect(json.numTotalTests).toBe(2);
|
||||
expect(json.numPassedTests).toBe(2);
|
||||
expect(json.numTotalTests).toBe(3);
|
||||
expect(json.numPassedTests).toBe(3);
|
||||
});
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import {double} from '../double';
|
||||
import {double, doubleWithDynamicImport} from '../double';
|
||||
|
||||
test('test double', () => {
|
||||
expect(double(2)).toBe(4);
|
||||
@@ -14,3 +14,8 @@ test('test double', () => {
|
||||
test('test import.meta', () => {
|
||||
expect(typeof import.meta.url).toBe('string');
|
||||
});
|
||||
|
||||
// Source: https://github.com/jestjs/jest/issues/15823
|
||||
test('test double with dynamic import', () => {
|
||||
expect(doubleWithDynamicImport(2)).toBe(4);
|
||||
});
|
||||
|
||||
@@ -8,3 +8,9 @@
|
||||
export function double(num: number): number {
|
||||
return num * 2;
|
||||
}
|
||||
|
||||
export function doubleWithDynamicImport(
|
||||
num: number,
|
||||
): typeof import('./doubleType') {
|
||||
return (num * 2) as typeof import('./doubleType');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
export type Double = number;
|
||||
@@ -64,6 +64,7 @@ import {
|
||||
} from './helpers';
|
||||
|
||||
const esmIsAvailable = typeof SourceTextModule === 'function';
|
||||
const supportsDynamicImport = esmIsAvailable;
|
||||
|
||||
const dataURIRegex =
|
||||
/^data:(?<mime>text\/javascript|application\/json|application\/wasm)(?:;(?<encoding>charset=utf-8|base64))?,(?<code>.*)$/;
|
||||
@@ -580,7 +581,7 @@ export default class Runtime {
|
||||
// @ts-expect-error -- exiting
|
||||
return;
|
||||
}
|
||||
if (this.isInsideTestCode === false) {
|
||||
if (this.isInsideTestCode === false && !supportsDynamicImport) {
|
||||
throw new ReferenceError(
|
||||
'You are trying to `import` a file outside of the scope of the test code.',
|
||||
);
|
||||
@@ -742,7 +743,7 @@ export default class Runtime {
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
if (this.isInsideTestCode === false) {
|
||||
if (this.isInsideTestCode === false && !supportsDynamicImport) {
|
||||
throw new ReferenceError(
|
||||
'You are trying to `import` a file outside of the scope of the test code.',
|
||||
);
|
||||
@@ -1563,7 +1564,7 @@ export default class Runtime {
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
if (this.isInsideTestCode === false) {
|
||||
if (this.isInsideTestCode === false && !supportsDynamicImport) {
|
||||
throw new ReferenceError(
|
||||
'You are trying to `import` a file outside of the scope of the test code.',
|
||||
);
|
||||
@@ -2466,7 +2467,9 @@ export default class Runtime {
|
||||
const stackTrace = formatStackTrace(stack, this._config, {
|
||||
noStackTrace: false,
|
||||
});
|
||||
const formattedMessage = `\n${message}${stackTrace ? `\n${stackTrace}` : ''}`;
|
||||
const formattedMessage = `\n${message}${
|
||||
stackTrace ? `\n${stackTrace}` : ''
|
||||
}`;
|
||||
if (!this.loggedReferenceErrors.has(formattedMessage)) {
|
||||
console.error(formattedMessage);
|
||||
this.loggedReferenceErrors.add(formattedMessage);
|
||||
|
||||
Reference in New Issue
Block a user