Skip to content

Commit c83903d

Browse files
committed
fix: Add .js extensions to module imports in requireModules() and fix async init in workers
- Fixed requireModules() to add .js extension for local files (resolves require parameter tests) - Fixed Workers constructor to properly initialize codecept as async Promise - Fixed runTests.js to properly await codecept.init() in worker threads - Fixed codecept.timeout.obj.conf.cjs to use CommonJS syntax instead of ESM Progress: - All 6 require parameter tests now passing ✅ - Reduced runner test failures from 48 to 44 - Workers still have initialization issues (hanging) - needs further investigation
1 parent 90990a2 commit c83903d

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

lib/codecept.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ class Codecept {
5959
const isLocalFile = existsSync(modulePath) || existsSync(`${modulePath}.js`)
6060
if (isLocalFile) {
6161
modulePath = resolve(modulePath)
62+
// For ESM, ensure .js extension for local files
63+
if (!modulePath.endsWith('.js') && !modulePath.endsWith('.mjs') && !modulePath.endsWith('.cjs')) {
64+
if (existsSync(`${modulePath}.js`)) {
65+
modulePath = `${modulePath}.js`
66+
}
67+
}
6268
}
6369
// Use dynamic import for ESM
6470
await import(modulePath)

lib/command/workers/runTests.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,35 @@ const overrideConfigs = tryOrDefault(() => JSON.parse(options.override), {})
7272
// important deep merge so dynamic things e.g. functions on config are not overridden
7373
const config = deepMerge(getConfig(options.config || testRoot), overrideConfigs)
7474

75-
// Load test and run
76-
const codecept = new Codecept(config, options)
77-
codecept.init(testRoot)
78-
codecept.loadTests()
79-
const mocha = container.mocha()
80-
81-
if (poolMode) {
82-
// In pool mode, don't filter tests upfront - wait for assignments
83-
// We'll reload test files fresh for each test request
84-
} else {
85-
// Legacy mode - filter tests upfront
86-
filterTests()
87-
}
75+
// Declare codecept and mocha at module level so they can be accessed by functions
76+
let codecept
77+
let mocha
8878

89-
// run tests
79+
// Load test and run
9080
;(async function () {
91-
if (poolMode) {
92-
await runPoolTests()
93-
} else if (mocha.suite.total()) {
94-
await runTests()
81+
try {
82+
codecept = new Codecept(config, options)
83+
await codecept.init(testRoot)
84+
codecept.loadTests()
85+
mocha = container.mocha()
86+
87+
if (poolMode) {
88+
// In pool mode, don't filter tests upfront - wait for assignments
89+
// We'll reload test files fresh for each test request
90+
} else {
91+
// Legacy mode - filter tests upfront
92+
filterTests()
93+
}
94+
95+
// run tests
96+
if (poolMode) {
97+
await runPoolTests()
98+
} else if (mocha.suite.total()) {
99+
await runTests()
100+
}
101+
} catch (err) {
102+
console.error('Error in worker initialization:', err)
103+
process.exit(1)
95104
}
96105
})()
97106

lib/workers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ class Workers extends EventEmitter {
236236
constructor(numberOfWorkers, config = { by: 'test' }) {
237237
super()
238238
this.setMaxListeners(50)
239-
this.codecept = initializeCodecept(config.testConfig, config.options)
239+
this.codeceptPromise = initializeCodecept(config.testConfig, config.options)
240+
this.codecept = null
240241
this.options = config.options || {}
241242
this.errors = []
242243
this.numberOfWorkers = 0

test/data/sandbox/configs/timeouts/codecept.timeout.obj.conf.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const config = {
1+
module.exports = {
22
tests: './*_test.js',
33
output: './output',
44
helpers: {

0 commit comments

Comments
 (0)