MCPcopy
hub / github.com/jestjs/jest / requireOrImportModule

Function requireOrImportModule

packages/jest-util/src/requireOrImportModule.ts:45–74  ·  view source on GitHub ↗
(
  filePath: string,
  applyInteropRequireDefault = true,
)

Source from the content-addressed store, hash-verified

43}
44
45export default async function requireOrImportModule<T>(
46 filePath: string,
47 applyInteropRequireDefault = true,
48): Promise<T> {
49 if (!isAbsolute(filePath) && filePath[0] === '.') {
50 throw new Error(
51 `Jest: requireOrImportModule path must be absolute, was "${filePath}"`,
52 );
53 }
54 try {
55 if (filePath.endsWith('.mjs') || filePath.endsWith('.mts')) {
56 return importModule(filePath, applyInteropRequireDefault);
57 }
58
59 const requiredModule = require(filePath);
60 if (!applyInteropRequireDefault) {
61 return requiredModule;
62 }
63 return interopRequireDefault(requiredModule).default;
64 } catch (error: any) {
65 if (
66 error.code === 'ERR_REQUIRE_ESM' ||
67 error.code === 'ERR_REQUIRE_ASYNC_MODULE'
68 ) {
69 return importModule(filePath, applyInteropRequireDefault);
70 } else {
71 throw error;
72 }
73 }
74}

Callers 4

setupPresetFunction · 0.90
runJestFunction · 0.90
loadTransformersMethod · 0.90

Calls 3

isAbsoluteFunction · 0.85
importModuleFunction · 0.85
interopRequireDefaultFunction · 0.85

Tested by

no test coverage detected