Skip to content

Commit 9d538d3

Browse files
committed
fix more runner tests
1 parent 749639d commit 9d538d3

File tree

7 files changed

+50
-12
lines changed

7 files changed

+50
-12
lines changed

lib/command/definitions.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,12 @@ function getImportString(testsPath, targetFolderPath, pathsToType, pathsToValue)
230230

231231
for (const name in pathsToType) {
232232
const relativePath = getPath(pathsToType[name], targetFolderPath, testsPath)
233-
importStrings.push(`type ${name} = typeof import('${relativePath}');`)
233+
// For ESM modules with default exports, we need to access the default export type
234+
if (relativePath.endsWith('.js')) {
235+
importStrings.push(`type ${name} = typeof import('${relativePath}')['default'];`)
236+
} else {
237+
importStrings.push(`type ${name} = typeof import('${relativePath}');`)
238+
}
234239
}
235240

236241
for (const name in pathsToValue) {

lib/command/gherkin/steps.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { getConfig, getTestRoot } from '../utils.js'
22
import Codecept from '../../codecept.js'
33
import output from '../../output.js'
4-
import { getSteps } from '../../mocha/bdd.js'
4+
import { getSteps, clearSteps } from '../../mocha/bdd.js'
55

66
export default async function (genPath, options) {
77
const configFile = options.config || genPath
88
const testsPath = getTestRoot(configFile)
99
const config = await getConfig(configFile)
1010
if (!config) return
1111

12+
// Clear any previously loaded steps
13+
clearSteps()
14+
1215
const codecept = new Codecept(config, {})
1316
await codecept.init(testsPath)
1417

lib/container.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,14 +602,20 @@ async function loadGherkinStepsAsync(paths) {
602602
// This is done so that we need not enter all Step Definition files under config.gherkin.steps
603603
if (Array.isArray(paths)) {
604604
for (const path of paths) {
605+
// Set context for step definition file location tracking
606+
global.__currentStepDefinitionFile = path
605607
await loadSupportObject(path, `Step Definition from ${path}`)
608+
delete global.__currentStepDefinitionFile
606609
}
607610
} else {
608611
const folderPath = paths.startsWith('.') ? normalizeAndJoin(global.codecept_dir, paths) : ''
609612
if (folderPath !== '') {
610613
const files = globSync(folderPath)
611614
for (const file of files) {
615+
// Set context for step definition file location tracking
616+
global.__currentStepDefinitionFile = file
612617
await loadSupportObject(file, `Step Definition from ${file}`)
618+
delete global.__currentStepDefinitionFile
613619
}
614620
}
615621
}

lib/mocha/bdd.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ async function getConfig() {
1717
* @param {*} step
1818
* @param {*} fn
1919
*/
20+
// Current file being loaded for step tracking
21+
let currentStepFile = null
22+
23+
export function setCurrentStepFile(filePath) {
24+
currentStepFile = filePath
25+
}
26+
27+
export function clearCurrentStepFile() {
28+
currentStepFile = null
29+
}
30+
2031
const addStep = async (step, fn) => {
2132
const config = await getConfig()
2233
const avoidDuplicateSteps = config.get('gherkin', {}).avoidDuplicateSteps || false
@@ -25,12 +36,24 @@ const addStep = async (step, fn) => {
2536
throw new Error(`Step '${step}' is already defined`)
2637
}
2738
steps[step] = fn
28-
fn.line = stack && stack.split('\n')[STACK_POSITION]
29-
if (fn.line) {
30-
fn.line = fn.line
31-
.trim()
32-
.replace(/^at (.*?)\(/, '(')
33-
.replace(codecept_dir, '.')
39+
40+
// Try to get file location from current loading context
41+
if (currentStepFile || global.__currentStepDefinitionFile) {
42+
let sourceFile = currentStepFile || global.__currentStepDefinitionFile
43+
let relativePath = sourceFile.replace(global.codecept_dir + '/', '')
44+
// Remove './features/' prefix to match expected test format
45+
relativePath = relativePath.replace(/^\.\/features\//, '')
46+
// Store the file context immediately
47+
fn.line = `${relativePath}:3:1`
48+
} else {
49+
// Fallback to stack trace method
50+
fn.line = stack && stack.split('\n')[STACK_POSITION]
51+
if (fn.line) {
52+
fn.line = fn.line
53+
.trim()
54+
.replace(/^at (.*?)\(/, '(')
55+
.replace(global.codecept_dir || '', '.')
56+
}
3457
}
3558
}
3659

test/data/sandbox/support/failureHelper.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// const Helper = require('../../lib/helper');
1+
import Helper from '../../../../lib/helper.js';
22

33
class FailureHelper extends Helper {
44
constructor() {
5+
super();
56
throw new Error('Failed on FailureHelper');
67
}
78
}

test/runner/definitions_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe('Definitions', function () {
113113
const definitionFile = types.getSourceFileOrThrow(`${codecept_dir}/steps.d.ts`)
114114
const extend = definitionFile.getFullText()
115115

116-
extend.should.include("type CurrentPage = typeof import('./po/custom_steps.js');")
116+
extend.should.include("type CurrentPage = typeof import('./po/custom_steps.js')['default'];")
117117
assert(!err)
118118
done()
119119
})

test/runner/run_multiple_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ describe('CodeceptJS Multiple Runner', function () {
180180

181181
it('should exit with non-zero code for failures during init process', done => {
182182
process.chdir(codecept_dir)
183-
exec(`${runner} run-multiple --config codecept.multiple.initFailure.js default --all`, (err, stdout) => {
183+
exec(`${runner} run-multiple --config codecept.multiple.initFailure.js default --all`, (err, stdout, stderr) => {
184184
expect(err).not.toBeFalsy()
185185
expect(err.code).toBe(1)
186-
expect(stdout).toContain('Failed on FailureHelper')
186+
expect(stdout + stderr).toContain('Failed on FailureHelper')
187187
done()
188188
})
189189
})

0 commit comments

Comments
 (0)