MCPcopy Index your code
hub / github.com/codeceptjs/CodeceptJS / requireHelperFromModule

Function requireHelperFromModule

lib/container.js:439–529  ·  view source on GitHub ↗
(helperName, config, HelperClass)

Source from the content-addressed store, hash-verified

437}
438
439async function requireHelperFromModule(helperName, config, HelperClass) {
440 const moduleName = getHelperModuleName(helperName, config)
441 if (moduleName.startsWith('./helper/')) {
442 try {
443 // For built-in helpers, use direct relative import with .js extension
444 const helperPath = `${moduleName}.js`
445
446 const resolvedPath = resolveImportModulePath(helperPath)
447 const mod = await import(resolvedPath)
448 HelperClass = mod.default || mod
449 } catch (err) {
450 throw err
451 }
452 } else {
453 // Handle TypeScript files
454 let importPath = moduleName
455 let tempJsFile = null
456 let fileMapping = null
457 const ext = path.extname(moduleName)
458
459 if (ext === '.ts') {
460 try {
461 // Use the TypeScript transpilation utility
462 const typescript = ((await import('typescript')).default || (await import('typescript')))
463 const { tempFile, allTempFiles, fileMapping: mapping } = await transpileTypeScript(importPath, typescript)
464
465 debug(`Transpiled TypeScript helper: ${importPath} -> ${tempFile}`)
466
467 importPath = tempFile
468 tempJsFile = allTempFiles
469 fileMapping = mapping
470 // Store file mapping in container for runtime error fixing (merge with existing)
471 if (!store.tsFileMapping) {
472 store.tsFileMapping = new Map()
473 }
474 for (const [key, value] of mapping.entries()) {
475 store.tsFileMapping.set(key, value)
476 }
477 } catch (tsError) {
478 throw new Error(`Failed to load TypeScript helper ${importPath}: ${tsError.message}. Make sure 'typescript' package is installed.`)
479 }
480 }
481
482 // check if the new syntax export default HelperName is used and loads the Helper, otherwise loads the module that used old syntax export = HelperName.
483 try {
484 // Try dynamic import for both CommonJS and ESM modules
485 const resolvedPath = resolveImportModulePath(importPath)
486 const mod = await import(resolvedPath)
487
488 if (!mod && !mod.default) {
489 throw new Error(`Helper module '${moduleName}' was not found. Make sure you have installed the package correctly.`)
490 }
491 HelperClass = mod.default || mod
492
493 // Clean up temp files if created
494 if (tempJsFile) {
495 const filesToClean = Array.isArray(tempJsFile) ? tempJsFile : [tempJsFile]
496 cleanupTempFiles(filesToClean)

Callers 1

createHelpersFunction · 0.85

Calls 8

resolveImportModulePathFunction · 0.90
transpileTypeScriptFunction · 0.90
cleanupTempFilesFunction · 0.90
fixErrorStackFunction · 0.90
getHelperModuleNameFunction · 0.85
debugFunction · 0.85
entriesMethod · 0.80
includesMethod · 0.80

Tested by

no test coverage detected