( options: Config.InitialOptionsWithRootDir, optionsPreset: string, )
| 168 | }; |
| 169 | |
| 170 | const setupPreset = async ( |
| 171 | options: Config.InitialOptionsWithRootDir, |
| 172 | optionsPreset: string, |
| 173 | ): Promise<Config.InitialOptionsWithRootDir> => { |
| 174 | let preset: Config.InitialOptions; |
| 175 | const presetPath = replaceRootDirInPath(options.rootDir, optionsPreset); |
| 176 | const presetModule = Resolver.findNodeModule( |
| 177 | presetPath.startsWith('.') || path.isAbsolute(presetPath) |
| 178 | ? presetPath |
| 179 | : `${presetPath}/${PRESET_NAME}`, |
| 180 | { |
| 181 | basedir: options.rootDir, |
| 182 | extensions: PRESET_EXTENSIONS, |
| 183 | }, |
| 184 | ); |
| 185 | |
| 186 | try { |
| 187 | if (!presetModule) { |
| 188 | throw new Error(`Cannot find module '${presetPath}'`); |
| 189 | } |
| 190 | |
| 191 | // Force re-evaluation to support multiple projects |
| 192 | try { |
| 193 | delete require.cache[require.resolve(presetModule)]; |
| 194 | } catch {} |
| 195 | |
| 196 | preset = await requireOrImportModule(presetModule); |
| 197 | } catch (error: any) { |
| 198 | if (error instanceof SyntaxError || error instanceof TypeError) { |
| 199 | throw createConfigError( |
| 200 | ` Preset ${chalk.bold(presetPath)} is invalid:\n\n ${ |
| 201 | error.message |
| 202 | }\n ${error.stack}`, |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | if (error.message.includes('Cannot find module')) { |
| 207 | if (error.message.includes(presetPath)) { |
| 208 | const preset = Resolver.findNodeModule(presetPath, { |
| 209 | basedir: options.rootDir, |
| 210 | }); |
| 211 | |
| 212 | if (preset) { |
| 213 | throw createConfigError( |
| 214 | ` Module ${chalk.bold( |
| 215 | presetPath, |
| 216 | )} should have "jest-preset.js" or "jest-preset.json" file at the root.`, |
| 217 | ); |
| 218 | } |
| 219 | throw createConfigError( |
| 220 | ` Preset ${chalk.bold( |
| 221 | presetPath, |
| 222 | )} not found relative to rootDir ${chalk.bold(options.rootDir)}.`, |
| 223 | ); |
| 224 | } |
| 225 | throw createConfigError( |
| 226 | ` Missing dependency in ${chalk.bold(presetPath)}:\n\n ${ |
| 227 | error.message |
no test coverage detected