Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce4cd54

Browse files
committed
no need to try require of mts
1 parent f6f6d3d commit ce4cd54

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

e2e/__tests__/readInitialOptions.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ describe('readInitialOptions', () => {
8181
});
8282
});
8383

84+
onNodeVersions('<22.18', () => {
85+
test('should fail to read mts-config/jest.config.mts with a clear error', async () => {
86+
const rootDir = resolveFixture('mts-config');
87+
await expect(
88+
proxyReadInitialOptions(undefined, {cwd: rootDir}),
89+
).rejects.toThrow(/jest\.config\.mts requires native TypeScript support/);
90+
});
91+
});
92+
8493
test('should be able to skip config reading, instead read from cwd', async () => {
8594
const expectedConfigFile = resolveFixture(
8695
'json-config',

packages/jest-config/src/__tests__/readConfigFileAndSetRootDir.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,37 @@ describe('readConfigFileAndSetRootDir', () => {
2525

2626
expect(config).toEqual({notify: true, rootDir});
2727
});
28+
29+
test('throws a clear error when native import fails, without falling back to ts-node', async () => {
30+
jest
31+
.mocked(requireOrImportModule)
32+
.mockRejectedValueOnce(new Error('Unknown file extension ".mts"'));
33+
34+
const configPath = path.join(
35+
path.resolve('some', 'path', 'to'),
36+
'jest.config.mts',
37+
);
38+
await expect(readConfigFileAndSetRootDir(configPath)).rejects.toThrow(
39+
/jest\.config\.mts requires native TypeScript support/,
40+
);
41+
// loadTSConfigFile reads the file for docblock parsing — it must not be called
42+
expect(fs.readFileSync).not.toHaveBeenCalled();
43+
});
44+
45+
test('throws a clear error when native import fails with a SyntaxError', async () => {
46+
jest
47+
.mocked(requireOrImportModule)
48+
.mockRejectedValueOnce(new SyntaxError('Unexpected token'));
49+
50+
const configPath = path.join(
51+
path.resolve('some', 'path', 'to'),
52+
'jest.config.mts',
53+
);
54+
await expect(readConfigFileAndSetRootDir(configPath)).rejects.toThrow(
55+
/jest\.config\.mts requires native TypeScript support/,
56+
);
57+
expect(fs.readFileSync).not.toHaveBeenCalled();
58+
});
2859
});
2960

3061
describe('JavaScript file', () => {

packages/jest-config/src/readConfigFileAndSetRootDir.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ export default async function readConfigFileAndSetRootDir(
5050
// Try native node TypeScript support first.
5151
configObject = await requireOrImportModule<any>(configPath);
5252
} catch (requireOrImportModuleError) {
53-
if (!(requireOrImportModuleError instanceof SyntaxError) && !isMTS) {
53+
if (isMTS) {
54+
// .mts is always ESM and cannot be loaded via require()/ts-node.
55+
throw new Error(
56+
'jest.config.mts requires native TypeScript support. Ensure you are using Node.js 22.18+ or 23.6+.',
57+
{cause: requireOrImportModuleError},
58+
);
59+
}
60+
if (!(requireOrImportModuleError instanceof SyntaxError)) {
5461
if (!hasTsLoaderExplicitlyConfigured(configPath)) {
5562
throw requireOrImportModuleError;
5663
}
@@ -231,6 +238,7 @@ async function registerTsLoader(loader: TsLoaderModule): Promise<TsLoader> {
231238
) {
232239
throw new Error(
233240
`Jest: '${loader}' is required for the TypeScript configuration files. Make sure it is installed\nError: ${error.message}`,
241+
{cause: error},
234242
);
235243
}
236244

0 commit comments

Comments
 (0)