Skip to content

Commit

Permalink
fix: use Node ESM loader when reading config
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU committed Feb 7, 2023
1 parent 0f88ace commit a873c25
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/jest-config/src/readConfigFileAndSetRootDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,27 @@ import {
export default async function readConfigFileAndSetRootDir(
configPath: string,
): Promise<Config.InitialOptions> {
const hasLoader = process.env.NODE_OPTIONS?.includes('--loader');
const isTS = configPath.endsWith(JEST_CONFIG_EXT_TS);
const isJSON = configPath.endsWith(JEST_CONFIG_EXT_JSON);
let configObject;

try {
if (isTS) {
configObject = await loadTSConfigFile(configPath);
} else if (isJSON) {
if (isJSON) {
const fileContent = fs.readFileSync(configPath, 'utf8');
configObject = parseJson(stripJsonComments(fileContent), configPath);
} else if (hasLoader) {
const importedModule = await import(configPath);

if (!importedModule.default) {
throw new Error(
`Jest: Failed to load ESM at ${configPath} - did you use a default export?`,
);
}

configObject = importedModule.default;
} else if (isTS) {
configObject = await loadTSConfigFile(configPath);
} else {
configObject = await requireOrImportModule<any>(configPath);
}
Expand Down

0 comments on commit a873c25

Please sign in to comment.