@@ -11,7 +11,7 @@ import * as fs from 'graceful-fs';
1111import parseJson from 'parse-json' ;
1212import stripJsonComments from 'strip-json-comments' ;
1313import type { Config } from '@jest/types' ;
14- import { extract , parse } from 'jest-docblock' ;
14+ import { type Pragmas , extract , parse } from 'jest-docblock' ;
1515import { interopRequireDefault , requireOrImportModule } from 'jest-util' ;
1616import {
1717 JEST_CONFIG_EXT_CTS ,
@@ -47,10 +47,15 @@ export default async function readConfigFileAndSetRootDir(
4747 configObject = await requireOrImportModule < any > ( configPath ) ;
4848 } catch ( requireOrImportModuleError ) {
4949 if ( ! ( requireOrImportModuleError instanceof SyntaxError ) ) {
50- throw requireOrImportModuleError ;
50+ if ( ! hasTsLoaderExplicitlyConfigured ( configPath ) ) {
51+ throw requireOrImportModuleError ;
52+ }
5153 }
5254 try {
53- // Likely ESM in a file interpreted as CJS, which means it needs to be
55+ // There are various reasons of failed loadout of Jest config in Typescript:
56+ // 1. User has specified a TypeScript loader in the docblock and
57+ // desire non-native compilation (https://github.com/jestjs/jest/issues/15837)
58+ // 2. Likely ESM in a file interpreted as CJS, which means it needs to be
5459 // compiled. We ignore the error and try to load it with a loader.
5560 configObject = await loadTSConfigFile ( configPath ) ;
5661 } catch ( loadTSConfigFileError ) {
@@ -120,11 +125,22 @@ export default async function readConfigFileAndSetRootDir(
120125// Load the TypeScript configuration
121126let extraTSLoaderOptions : Record < string , unknown > ;
122127
128+ const hasTsLoaderExplicitlyConfigured = ( configPath : string ) : boolean => {
129+ const docblockPragmas = loadDocblockPragmasInConfig ( configPath ) ;
130+ const tsLoader = docblockPragmas [ 'jest-config-loader' ] ;
131+ return ! Array . isArray ( tsLoader ) && ( tsLoader ?? '' ) . trim ( ) !== '' ;
132+ } ;
133+
134+ const loadDocblockPragmasInConfig = ( configPath : string ) : Pragmas => {
135+ const docblockPragmas = parse ( extract ( fs . readFileSync ( configPath , 'utf8' ) ) ) ;
136+ return docblockPragmas ;
137+ } ;
138+
123139const loadTSConfigFile = async (
124140 configPath : string ,
125141) : Promise < Config . InitialOptions > => {
126142 // Get registered TypeScript compiler instance
127- const docblockPragmas = parse ( extract ( fs . readFileSync ( configPath , 'utf8' ) ) ) ;
143+ const docblockPragmas = loadDocblockPragmasInConfig ( configPath ) ;
128144 const tsLoader = docblockPragmas [ 'jest-config-loader' ] || 'ts-node' ;
129145 const docblockTSLoaderOptions = docblockPragmas [ 'jest-config-loader-options' ] ;
130146
0 commit comments