Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions e2e/__tests__/__snapshots__/jest.config.ts.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<REPLACED>>
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 <<REPLACED>>
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
: ^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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`] = `
Expand Down
34 changes: 21 additions & 13 deletions packages/jest-config/src/readConfigFileAndSetRootDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,29 @@
try {
// Try native node TypeScript support first.
configObject = await requireOrImportModule<any>(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;

Check warning on line 50 in packages/jest-config/src/readConfigFileAndSetRootDir.ts

View check run for this annotation

Codecov / codecov/patch

packages/jest-config/src/readConfigFileAndSetRootDir.ts#L50

Added line #L50 was not covered by tests
}
try {

Check warning on line 52 in packages/jest-config/src/readConfigFileAndSetRootDir.ts

View check run for this annotation

Codecov / codecov/patch

packages/jest-config/src/readConfigFileAndSetRootDir.ts#L52

Added line #L52 was not covered by tests
// 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);

Check warning on line 55 in packages/jest-config/src/readConfigFileAndSetRootDir.ts

View check run for this annotation

Codecov / codecov/patch

packages/jest-config/src/readConfigFileAndSetRootDir.ts#L55

Added line #L55 was not covered by tests
} 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 (

Check warning on line 60 in packages/jest-config/src/readConfigFileAndSetRootDir.ts

View check run for this annotation

Codecov / codecov/patch

packages/jest-config/src/readConfigFileAndSetRootDir.ts#L60

Added line #L60 was not covered by tests
// 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 {

Check warning on line 70 in packages/jest-config/src/readConfigFileAndSetRootDir.ts

View check run for this annotation

Codecov / codecov/patch

packages/jest-config/src/readConfigFileAndSetRootDir.ts#L70

Added line #L70 was not covered by tests
configObject = await loadTSConfigFile(configPath);
}
} else if (isJSON) {
Expand Down
Loading