Skip to content

Commit 7bcf28b

Browse files
committed
fix: uts
1 parent 506df81 commit 7bcf28b

10 files changed

Lines changed: 43 additions & 48 deletions

File tree

bin/codecept.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Codecept from '../lib/codecept.js'
55
import output from '../lib/output.js'
66
const { print, error } = output
77
import { printError } from '../lib/command/utils.js'
8-
import { importModule } from '../lib/utils.js'
8+
import { resolveImportModulePath } from '../lib/utils.js'
99

1010
const commandFlags = {
1111
ai: {
@@ -46,7 +46,8 @@ const errorHandler =
4646
}
4747

4848
const dynamicImport = async modulePath => {
49-
const module = await importModule(modulePath)
49+
const resolvedPath = resolveImportModulePath(modulePath)
50+
const module = await import(resolvedPath)
5051
return module.default || module
5152
}
5253

lib/ai.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { removeNonInteractiveElements, minifyHtml, splitByChunks } from './html.
66
import { generateText } from 'ai'
77
import { fileURLToPath } from 'url'
88
import path from 'path'
9-
import { fileExists, importModule } from './utils.js'
9+
import { fileExists, resolveImportModulePath } from './utils.js'
1010
import store from './store.js'
1111

1212
const __dirname = path.dirname(fileURLToPath(import.meta.url))
@@ -34,7 +34,8 @@ async function loadPrompts() {
3434
}
3535

3636
try {
37-
const module = await importModule(promptPath)
37+
const resolvedPath = resolveImportModulePath(promptPath)
38+
const module = await import(resolvedPath)
3839
prompts[name] = module.default || module
3940
debug(`Loaded prompt ${name} from ${promptPath}`)
4041
} catch (err) {

lib/codecept.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import event from './event.js'
1717
import runHook from './hooks.js'
1818
import ActorFactory from './actor.js'
1919
import output from './output.js'
20-
import { emptyFolder, importModule } from './utils.js'
20+
import { emptyFolder, resolveImportModulePath } from './utils.js'
2121
import { initCodeceptGlobals } from './globals.js'
2222
import { validateTypeScriptSetup, getTSNodeESMWarning } from './utils/loaderCheck.js'
2323
import recorder from './recorder.js'
@@ -86,7 +86,8 @@ class Codecept {
8686
}
8787
}
8888
// Use dynamic import for ESM
89-
await importModule(modulePath)
89+
const resolvedPath = resolveImportModulePath(modulePath)
90+
await import(resolvedPath)
9091
}
9192
}
9293
}
@@ -137,7 +138,8 @@ class Codecept {
137138
]
138139

139140
for (const modulePath of listenerModules) {
140-
const module = await importModule(modulePath)
141+
const resolvedPath = resolveImportModulePath(modulePath)
142+
const module = await import(resolvedPath)
141143
runHook(module.default || module)
142144
}
143145
}

lib/config.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'fs'
22
import path from 'path'
33
import { createRequire } from 'module'
4-
import { fileExists, isFile, deepMerge, deepClone, importModule } from './utils.js'
4+
import { fileExists, isFile, deepMerge, deepClone, resolveImportModulePath } from './utils.js'
55
import { transpileTypeScript, cleanupTempFiles, fixErrorStack } from './utils/typescript.js'
66

77
const defaultConfig = {
@@ -242,7 +242,8 @@ async function loadConfigFile(configFile) {
242242
allTempFiles = result.allTempFiles
243243
fileMapping = result.fileMapping
244244

245-
configModule = await importModule(tempFile)
245+
const resolvedPath = resolveImportModulePath(tempFile)
246+
configModule = await import(resolvedPath)
246247
cleanupTempFiles(allTempFiles)
247248
} catch (err) {
248249
transpileError = err
@@ -258,7 +259,8 @@ async function loadConfigFile(configFile) {
258259
}
259260
} else {
260261
// Try ESM import first for JS files
261-
configModule = await importModule(configFile)
262+
const resolvedPath = resolveImportModulePath(configFile)
263+
configModule = await import(resolvedPath)
262264
}
263265
} catch (importError) {
264266
try {

lib/container.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
isAsyncFunction,
1313
installedLocally,
1414
deepMerge,
15-
importModule,
15+
resolveImportModulePath,
1616
} from './utils.js'
1717
import { transpileTypeScript, cleanupTempFiles, fixErrorStack } from './utils/typescript.js'
1818
import Translation from './translation.js'
@@ -443,7 +443,8 @@ async function requireHelperFromModule(helperName, config, HelperClass) {
443443
// For built-in helpers, use direct relative import with .js extension
444444
const helperPath = `${moduleName}.js`
445445

446-
const mod = await importModule(helperPath)
446+
const resolvedPath = resolveImportModulePath(helperPath)
447+
const mod = await import(resolvedPath)
447448
HelperClass = mod.default || mod
448449
} catch (err) {
449450
throw err
@@ -481,7 +482,8 @@ async function requireHelperFromModule(helperName, config, HelperClass) {
481482
// check if the new syntax export default HelperName is used and loads the Helper, otherwise loads the module that used old syntax export = HelperName.
482483
try {
483484
// Try dynamic import for both CommonJS and ESM modules
484-
const mod = await importModule(importPath)
485+
const resolvedPath = resolveImportModulePath(importPath)
486+
const mod = await import(resolvedPath)
485487

486488
if (!mod && !mod.default) {
487489
throw new Error(`Helper module '${moduleName}' was not found. Make sure you have installed the package correctly.`)
@@ -693,7 +695,8 @@ async function loadPluginAsync(modulePath, config) {
693695
let pluginMod
694696
try {
695697
// Try dynamic import first (works for both ESM and CJS)
696-
pluginMod = await importModule(modulePath)
698+
const resolvedPath = resolveImportModulePath(modulePath)
699+
pluginMod = await import(resolvedPath)
697700
} catch (err) {
698701
throw new Error(`Could not load plugin from '${modulePath}': ${err.message}`)
699702
}
@@ -900,7 +903,8 @@ async function loadSupportObject(modulePath, supportObjectName) {
900903

901904
let obj
902905
try {
903-
obj = await importModule(importPath)
906+
const resolvedPath = resolveImportModulePath(importPath)
907+
obj = await import(resolvedPath)
904908
} catch (importError) {
905909
if (fileMapping) {
906910
fixErrorStack(importError, fileMapping)

lib/helper/ApiDataFactory.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from 'path'
22
import Helper from '@codeceptjs/helper'
33
import REST from './REST.js'
44
import store from '../store.js'
5-
import { importModule } from '../utils.js'
5+
import { resolveImportModulePath } from '../utils.js'
66

77
/**
88
* Helper for managing remote data using REST API.
@@ -329,7 +329,8 @@ class ApiDataFactory extends Helper {
329329
modulePath = path.join(store.codeceptDir, modulePath)
330330
}
331331
// check if the new syntax `export default new Factory()` is used and loads the builder, otherwise loads the module that used old syntax `module.exports = new Factory()`.
332-
const module = await importModule(modulePath)
332+
const resolvedPath = resolveImportModulePath(modulePath)
333+
const module = await import(resolvedPath)
333334
const builder = module.default || module
334335
return builder.build(data, options)
335336
} catch (err) {

lib/rerun.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import event from './event.js'
55
import BaseCodecept from './codecept.js'
66
import output from './output.js'
77
import { createRequire } from 'module'
8-
import { importModule } from './utils.js'
8+
import { resolveImportModulePath } from './utils.js'
99

1010
const require = createRequire(import.meta.url)
1111

@@ -52,7 +52,8 @@ class CodeceptRerunner extends BaseCodecept {
5252

5353
// Force reload the module by using a cache-busting query parameter
5454
const fileUrl = `${fsPath.resolve(file)}?t=${Date.now()}`
55-
await importModule(fileUrl)
55+
const resolvedPath = resolveImportModulePath(fileUrl)
56+
await import(resolvedPath)
5657
} catch (e) {
5758
console.error(`Error loading test file ${file}:`, e)
5859
}

lib/utils.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,11 @@ export const isAsyncFunction = function (fn) {
3939
return fn[Symbol.toStringTag] === 'AsyncFunction'
4040
}
4141

42-
/**
43-
* Dynamically imports a module.
44-
* On Windows, absolute paths are converted to file URLs.
45-
* @param {string} modulePath - The path to the module.
46-
* @param {Object} [options] - Import options.
47-
* @returns {Promise<any>} The imported module.
48-
*/
49-
export const importModule = async function (modulePath, options) {
50-
if (typeof modulePath === 'string' && path.isAbsolute(modulePath)) {
51-
return import(pathToFileURL(modulePath).href, options)
42+
export const resolveImportModulePath = function (modulePath) {
43+
if (isWindows() && modulePath === "string" && path.isAbsolute(modulePath)) {
44+
return pathToFileURL(modulePath).href
5245
}
53-
return import(modulePath, options)
46+
return modulePath
5447
}
5548

5649
export const fileExists = function (filePath) {

test/unit/container_test.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import FileSystem from '../../lib/helper/FileSystem.js'
99
import actor from '../../lib/actor.js'
1010
import container from '../../lib/container.js'
1111
import Translation from '../../lib/translation.js'
12-
import { isWindows } from '../../lib/utils.js'
12+
import { resolveImportModulePath } from '../../lib/utils.js'
1313

1414
describe('Container', () => {
1515
before(() => {
@@ -185,12 +185,8 @@ describe('Container', () => {
185185
},
186186
})
187187

188-
const resolvedImportPath =
189-
isWindows() && typeof '../data/dummy_page.js' === 'string' && path.isAbsolute('../data/dummy_page.js')
190-
? pathToFileURL('../data/dummy_page.js').href
191-
: '../data/dummy_page.js'
192-
193-
const dummyPage = await import(resolvedImportPath)
188+
const resolvedPath = resolveImportModulePath('../data/dummy_page.js')
189+
const dummyPage = await import(resolvedPath)
194190
expect(container.support('dummyPage').toString()).is.eql((dummyPage.default || dummyPage).toString())
195191
})
196192

test/unit/utils_test.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import sinon from 'sinon'
66
import * as utils from '../../lib/utils.js'
77
import store from '../../lib/store.js'
88
import playwright from 'playwright'
9-
import { isWindows } from '../../lib/utils.js'
9+
import { isWindows, resolveImportModulePath } from '../../lib/utils.js'
1010

1111
const __filename = fileURLToPath(import.meta.url)
1212
const __dirname = path.dirname(__filename)
@@ -353,7 +353,8 @@ describe('utils', () => {
353353
})
354354

355355
it('should import a module', async () => {
356-
const module = await utils.importModule(path.join(__dirname, '../../lib/output.js'))
356+
const resolvedPath = utils.resolveImportModulePath(path.join(__dirname, '../../lib/output.js'))
357+
const module = await import(resolvedPath)
357358
expect(module.default).to.be.ok
358359
})
359360

@@ -364,15 +365,8 @@ describe('utils', () => {
364365
// Use an absolute path that exists
365366
const absolutePath = path.resolve(__dirname, '../../lib/output.js')
366367

367-
const module = await utils.importModule(absolutePath)
368-
expect(module.default).to.be.ok
369-
})
370-
371-
it('should import a relative module', async () => {
372-
// Relative to lib/utils.js where importModule is defined
373-
// lib/utils.js is in lib/
374-
// we want to import lib/output.js
375-
const module = await utils.importModule('./output.js')
368+
const resolvedPath = utils.resolveImportModulePath(absolutePath)
369+
const module = await import(resolvedPath)
376370
expect(module.default).to.be.ok
377371
})
378372
})

0 commit comments

Comments
 (0)