diff --git a/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap b/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap index fa3e71e73fa1..3a51ef8a6068 100644 --- a/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap +++ b/e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap @@ -15,12 +15,20 @@ Ran all test suites." exports[`on node >=24 invalid JS in jest.config.ts (node with native TS support) 1`] = ` "Error: Jest: Failed to parse the TypeScript config file <> - SyntaxError [ERR_INVALID_TYPESCRIPT_SYNTAX]: Expected ';', got 'string literal (ll break this file yo, 'll break this file yo)'" + both with the native node TypeScript support and configured TypeScript loaders. + Errors were: + - SyntaxError [ERR_INVALID_TYPESCRIPT_SYNTAX]: Expected ';', got 'string literal (ll break this file yo, 'll break this file yo)' + - TSError: ⨯ Unable to compile TypeScript: +jest.config.ts(1,16): error TS2304: Cannot find name 'i'. +jest.config.ts(1,17): error TS1005: ';' expected. +jest.config.ts(1,39): error TS1002: Unterminated string literal." `; exports[`on node ^23.6 invalid JS in jest.config.ts (node with native TS support) 1`] = ` "Error: Jest: Failed to parse the TypeScript config file <> - SyntaxError [ERR_INVALID_TYPESCRIPT_SYNTAX]: x Expected ';', got 'string literal (ll break this file yo, 'll break this file yo)' + both with the native node TypeScript support and configured TypeScript loaders. + Errors were: + - SyntaxError [ERR_INVALID_TYPESCRIPT_SYNTAX]: x Expected ';', got 'string literal (ll break this file yo, 'll break this file yo)' ,---- 1 | export default i'll break this file yo : ^^^^^^^^^^^^^^^^^^^^^^ @@ -29,7 +37,12 @@ exports[`on node ^23.6 invalid JS in jest.config.ts (node with native TS support ,---- 1 | export default i'll break this file yo : ^^^^^^^^^^^^^^^^^^^^^^ - \`----" + \`---- + + - TSError: ⨯ Unable to compile TypeScript: +jest.config.ts(1,16): error TS2304: Cannot find name 'i'. +jest.config.ts(1,17): error TS1005: ';' expected. +jest.config.ts(1,39): error TS1002: Unterminated string literal." `; exports[`traverses directory tree up until it finds jest.config 1`] = ` diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index eae78332fc89..f71d1af715ba 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -45,21 +45,29 @@ export default async function readConfigFileAndSetRootDir( try { // Try native node TypeScript support first. configObject = await requireOrImportModule(configPath); - } catch (error) { - if ( - !( - error instanceof SyntaxError && - // Likely ESM in a file interpreted as CJS, which means it needs to be - // compiled. We ignore the error and try to load it with a loader. - /Unexpected token '(export|import)'/.test(error.message) - ) - ) { - throw error; + } catch (requireOrImportModuleError) { + if (!(requireOrImportModuleError instanceof SyntaxError)) { + throw requireOrImportModuleError; + } + try { + // Likely ESM in a file interpreted as CJS, which means it needs to be + // compiled. We ignore the error and try to load it with a loader. + configObject = await loadTSConfigFile(configPath); + } catch (loadTSConfigFileError) { + // If we still encounter an error, we throw both messages combined. + // This string is caught further down and merged into a new error message. + // eslint-disable-next-line no-throw-literal + throw ( + // Preamble text is added further down: + // Jest: Failed to parse the TypeScript config file ${configPath}\n + ' both with the native node TypeScript support and configured TypeScript loaders.\n' + + ' Errors were:\n' + + ` - ${requireOrImportModuleError}\n` + + ` - ${loadTSConfigFileError}` + ); } } - } - // Fall back to `ts-node` etc. if this cannot be natively parsed/executed. - if (!configObject) { + } else { configObject = await loadTSConfigFile(configPath); } } else if (isJSON) {