@@ -13,6 +13,7 @@ import { DevToolsAppLauncher } from './launcher.js'
1313import { getBrowserObject } from './utils.ts'
1414import { parse } from 'stack-trace'
1515import { type TraceLog , TraceType } from './types.ts'
16+ import { INTERNAL_COMMANDS , SPEC_FILE_PATTERN } from './constants.ts'
1617
1718export const launcher = DevToolsAppLauncher
1819
@@ -131,19 +132,23 @@ export default class DevToolsHookService implements Services.ServiceInstance {
131132 }
132133
133134 /**
135+ * Hook for Cucumber framework.
134136 * beforeScenario is triggered at the beginning of every worker session, therefore
135137 * we can use it to reset the command stack and last command signature
136138 */
137139 beforeScenario ( ) {
138- this . #lastCommandSig = null ;
139- this . #commandStack = [ ]
140+ this . resetStack ( )
140141 }
141142
142143 /**
143- * Hook for Mocha/Jasmine.
144+ * Hook for Mocha/Jasmine frameworks .
144145 * It does the exact same thing as beforeScenario.
145146 */
146147 beforeTest ( ) {
148+ this . resetStack ( )
149+ }
150+
151+ private resetStack ( ) {
147152 this . #lastCommandSig = null
148153 this . #commandStack = [ ]
149154 }
@@ -164,17 +169,15 @@ export default class DevToolsHookService implements Services.ServiceInstance {
164169 /**
165170 * Smart stack filtering to detect top-level user commands
166171 */
167- const stack = parse ( new Error ( '' ) )
168- const source = stack . find ( ( frame ) =>
169- Boolean ( frame . getFileName ( ) ) &&
170- ! frame . getFileName ( ) ?. includes ( 'node_modules' )
171- )
172-
173- // If the command is a selector command, we don't want to capture it
174- // as it is not a top-level user command.
175- const isSelectorCommand = command . startsWith ( '$' ) || command . includes ( 'LocateNodes' )
176-
177- if ( source && this . #commandStack. length === 0 && ! isSelectorCommand ) {
172+ Error . stackTraceLimit = 20
173+ const stack = parse ( new Error ( '' ) ) . reverse ( )
174+ const source = stack . find ( ( frame ) => {
175+ const file = frame . getFileName ( )
176+ // Only consider frames from user spec/test files
177+ return file && SPEC_FILE_PATTERN . test ( file )
178+ } )
179+
180+ if ( source && this . #commandStack. length === 0 && ! INTERNAL_COMMANDS . includes ( command ) ) {
178181 const cmdSig = JSON . stringify ( {
179182 command,
180183 args,
0 commit comments