( filePath: string, applyInteropRequireDefault = true, )
| 43 | } |
| 44 | |
| 45 | export 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 | } |
no test coverage detected