@@ -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+
2031const 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 ( / ^ a t ( .* ?) \( / , '(' )
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 ( / ^ \. \/ f e a t u r e s \/ / , '' )
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 ( / ^ a t ( .* ?) \( / , '(' )
55+ . replace ( global . codecept_dir || '' , '.' )
56+ }
3457 }
3558}
3659
0 commit comments