11import { CucumberExpression , ParameterTypeRegistry , ParameterType } from '@cucumber/cucumber-expressions'
2+ import event from '../event.js'
23
34let steps = { }
45let Config
@@ -35,14 +36,14 @@ const addStep = async (step, fn) => {
3536 throw new Error ( `Step '${ step } ' is already defined` )
3637 }
3738 steps [ step ] = fn
38-
39+
3940 // Use the current step file context if available (fallback for old usage)
4041 if ( currentStepFile ) {
4142 let relativePath = currentStepFile
42-
43+
4344 // Remove any leading './' and keep step_definitions/ path
4445 relativePath = relativePath . replace ( / ^ \. \/ / , '' ) . replace ( / ^ .* \/ (? = s t e p _ d e f i n i t i o n s ) / , '' )
45-
46+
4647 fn . line = `${ relativePath } :3:1`
4748 } else {
4849 fn . line = 'unknown_file:1:1'
@@ -94,11 +95,11 @@ const buildParameterType = ({ name, regexp, transformer, useForSnippets, preferF
9495}
9596
9697// Create wrapper functions that capture the call context
97- const createStepFunction = ( stepType ) => {
98+ const createStepFunction = stepType => {
9899 return ( step , fn ) => {
99100 // Capture the stack trace at the point where Given/When/Then is called
100101 const callStack = new Error ( ) . stack
101-
102+
102103 // Find the caller (step definition file) in the stack
103104 let callerInfo = 'unknown_file:1:1'
104105 if ( callStack ) {
@@ -115,7 +116,7 @@ const createStepFunction = (stepType) => {
115116 }
116117 }
117118 }
118-
119+
119120 // Instead of using global currentStepFile, pass the caller info directly to addStep
120121 return addStepWithCaller ( step , fn , callerInfo )
121122 }
@@ -129,7 +130,7 @@ const addStepWithCaller = async (step, fn, callerInfo) => {
129130 throw new Error ( `Step '${ step } ' is already defined` )
130131 }
131132 steps [ step ] = fn
132-
133+
133134 // Use the caller info passed directly
134135 fn . line = callerInfo
135136}
@@ -139,17 +140,21 @@ const When = createStepFunction('When')
139140const Then = createStepFunction ( 'Then' )
140141const And = createStepFunction ( 'And' )
141142
142- export {
143- Given ,
144- When ,
145- Then ,
146- And ,
147- matchStep ,
148- getSteps ,
149- clearSteps ,
150- defineParameterType ,
143+ // Before/After hooks for BDD - these are global event listeners
144+ const Before = fn => {
145+ event . dispatcher . on ( event . test . started , fn )
146+ }
147+
148+ const After = fn => {
149+ event . dispatcher . on ( event . test . finished , fn )
151150}
152151
152+ const Fail = fn => {
153+ event . dispatcher . on ( event . test . failed , fn )
154+ }
155+
156+ export { Given , When , Then , And , Before , After , Fail , matchStep , getSteps , clearSteps , defineParameterType }
157+
153158export default {
154159 Given : addStep ,
155160 When : addStep ,
0 commit comments