* Require modules before codeceptjs running * * @param {string[]} requiringModules
(requiringModules)
| 57 | * @param {string[]} requiringModules |
| 58 | */ |
| 59 | async requireModules(requiringModules) { |
| 60 | if (requiringModules) { |
| 61 | for (const requiredModule of requiringModules) { |
| 62 | let modulePath = requiredModule |
| 63 | const isLocalFile = existsSync(modulePath) || existsSync(`${modulePath}.js`) |
| 64 | if (isLocalFile) { |
| 65 | modulePath = resolve(modulePath) |
| 66 | // For ESM, ensure .js extension for local files |
| 67 | if (!modulePath.endsWith('.js') && !modulePath.endsWith('.mjs') && !modulePath.endsWith('.cjs')) { |
| 68 | if (existsSync(`${modulePath}.js`)) { |
| 69 | modulePath = `${modulePath}.js` |
| 70 | } |
| 71 | } |
| 72 | } else { |
| 73 | // For npm packages, resolve from the user's directory |
| 74 | // This ensures packages like tsx are found in user's node_modules |
| 75 | const userDir = store.codeceptDir || process.cwd() |
| 76 | |
| 77 | try { |
| 78 | // Use createRequire to resolve from user's directory |
| 79 | const userRequire = createRequire(pathToFileURL(resolve(userDir, 'package.json')).href) |
| 80 | const resolvedPath = userRequire.resolve(requiredModule) |
| 81 | modulePath = pathToFileURL(resolvedPath).href |
| 82 | } catch (resolveError) { |
| 83 | // If resolution fails, try direct import (will check from CodeceptJS node_modules) |
| 84 | // This is the fallback for globally installed packages |
| 85 | modulePath = requiredModule |
| 86 | } |
| 87 | } |
| 88 | // Use dynamic import for ESM |
| 89 | const resolvedPath = resolveImportModulePath(modulePath) |
| 90 | await import(resolvedPath) |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Initialize CodeceptJS at specific dir. |
no test coverage detected