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

refactor(jest-config): use requireOrImportModule util function for ESM/CJS import #11206

Merged
merged 1 commit into from Mar 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 12 additions & 33 deletions packages/jest-config/src/normalize.ts
Expand Up @@ -7,7 +7,6 @@

import {createHash} from 'crypto';
import * as path from 'path';
import {pathToFileURL} from 'url';
import chalk = require('chalk');
import merge = require('deepmerge');
import {sync as glob} from 'glob';
Expand All @@ -16,7 +15,12 @@ import micromatch = require('micromatch');
import type {Config} from '@jest/types';
import {replacePathSepForRegex} from 'jest-regex-util';
import Resolver from 'jest-resolve';
import {clearLine, replacePathSepForGlob, tryRealpath} from 'jest-util';
import {
clearLine,
replacePathSepForGlob,
requireOrImportModule,
tryRealpath,
} from 'jest-util';
import {ValidationError, validate} from 'jest-validate';
import DEFAULT_CONFIG from './Defaults';
import DEPRECATED_CONFIG from './Deprecated';
Expand Down Expand Up @@ -143,7 +147,7 @@ const setupPreset = async (
delete require.cache[require.resolve(presetModule)];
} catch {}

preset = require(presetModule);
preset = await requireOrImportModule(presetModule);
} catch (error) {
if (error instanceof SyntaxError || error instanceof TypeError) {
throw createConfigError(
Expand Down Expand Up @@ -177,36 +181,11 @@ const setupPreset = async (
);
}

if (presetModule && error.code === 'ERR_REQUIRE_ESM') {
try {
const presetModuleUrl = pathToFileURL(presetModule);

// node `import()` supports URL, but TypeScript doesn't know that
const importedPreset = await import(presetModuleUrl.href);

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

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

throw innerError;
}
} else {
throw createConfigError(
` An unknown error occurred in ${chalk.bold(presetPath)}:\n\n ${
error.message
}\n ${error.stack}`,
);
}
throw createConfigError(
` An unknown error occurred in ${chalk.bold(presetPath)}:\n\n ${
error.message
}\n ${error.stack}`,
);
}

if (options.setupFiles) {
Expand Down