-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jest-util): add requireOrImportModule util for importing CJS or …
…ESM (#11199)
- Loading branch information
Showing
4 changed files
with
58 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {isAbsolute} from 'path'; | ||
import {pathToFileURL} from 'url'; | ||
import type {Config} from '@jest/types'; | ||
import interopRequireDefault from './interopRequireDefault'; | ||
|
||
export default async function requireOrImportModule<T>( | ||
filePath: Config.Path, | ||
): Promise<T> { | ||
let module: T; | ||
if (!isAbsolute(filePath) && filePath[0] === '.') { | ||
throw new Error(`Jest: requireOrImportModule path must be absolute`); | ||
} | ||
try { | ||
module = interopRequireDefault(require(filePath)).default; | ||
} catch (error) { | ||
if (error.code === 'ERR_REQUIRE_ESM') { | ||
try { | ||
const configUrl = pathToFileURL(filePath); | ||
|
||
// node `import()` supports URL, but TypeScript doesn't know that | ||
const importedConfig = await import(configUrl.href); | ||
|
||
if (!importedConfig.default) { | ||
throw new Error( | ||
`Jest: Failed to load ESM at ${filePath} - did you use a default export?`, | ||
); | ||
} | ||
|
||
module = 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 ${filePath}`, | ||
); | ||
} | ||
throw innerError; | ||
} | ||
} else { | ||
throw error; | ||
} | ||
} | ||
return module; | ||
} |