Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle ERR_REQUIRE_ESM when reading configuration #9573

Merged
merged 4 commits into from
Feb 16, 2020
Merged
Changes from 2 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
64 changes: 29 additions & 35 deletions packages/jest-config/src/readConfigFileAndSetRootDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,49 @@ import * as fs from 'fs';
import {Config} from '@jest/types';
// @ts-ignore: vendored
import jsonlint from './vendor/jsonlint';
import {
JEST_CONFIG_EXT_JSON,
JEST_CONFIG_EXT_MJS,
PACKAGE_JSON,
} from './constants';
import {JEST_CONFIG_EXT_JSON, PACKAGE_JSON} from './constants';
import importMjs from './importMjs';

// Read the configuration and set its `rootDir`
// 1. If it's a `package.json` file, we look into its "jest" property
// 2. For any other file, we just require it.
// 2. For any other file, we just require it. If we receive an 'ERR_REQUIRE_ESM'
// from node, perform a dynamic import instead.
export default async function readConfigFileAndSetRootDir(
configPath: Config.Path,
): Promise<Config.InitialOptions> {
const isJSON = configPath.endsWith(JEST_CONFIG_EXT_JSON);
const isMjs = configPath.endsWith(JEST_CONFIG_EXT_MJS);
let configObject;

if (isMjs) {
try {
const importedConfig = await importMjs(configPath);
try {
configObject = require(configPath);
} catch (error) {
if (error.code === 'ERR_REQUIRE_ESM') {
try {
const importedConfig = await importMjs(configPath);
azz marked this conversation as resolved.
Show resolved Hide resolved

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

configObject = importedConfig.default;
} catch (error) {
if (error.message === 'Not supported') {
throw new Error(
`Jest: Your version of Node does not support dynamic import - please enable it or use a .cjs file extension for file ${configPath}`,
);
}
configObject = importedConfig.default;
} catch (innerError) {
if (innerError.message === 'Not supported') {
throw new Error(
`Jest: Your version of Node does not support dynamic import - please enable it or use a .cjs file extension for file ${configPath}`,
);
}

throw error;
}
} else {
try {
configObject = require(configPath);
} catch (error) {
if (isJSON) {
throw new Error(
`Jest: Failed to parse config file ${configPath}\n` +
` ${jsonlint.errors(fs.readFileSync(configPath, 'utf8'))}`,
);
} else {
throw error;
throw innerError;
}
} else if (isJSON) {
throw new Error(
`Jest: Failed to parse config file ${configPath}\n` +
` ${jsonlint.errors(fs.readFileSync(configPath, 'utf8'))}`,
);
} else {
throw error;
}
}

Expand Down