@@ -238,6 +238,7 @@ class Workers extends EventEmitter {
238238 this . workers = [ ]
239239 this . testGroups = [ ]
240240 this . testPool = [ ]
241+ this . testPoolInitialized = false
241242 this . isPoolMode = config . by === 'pool'
242243 this . activeWorkers = new Map ( )
243244 this . maxWorkers = numberOfWorkers // Track original worker count for pool mode
@@ -322,7 +323,28 @@ class Workers extends EventEmitter {
322323 * @param {Number } numberOfWorkers
323324 */
324325 createTestPool ( numberOfWorkers ) {
326+ // For pool mode, create empty groups for each worker and initialize empty pool
327+ // Test pool will be populated lazily when getNextTest() is first called
328+ this . testPool = [ ]
329+ this . testPoolInitialized = false
330+ this . testGroups = populateGroups ( numberOfWorkers )
331+ }
332+
333+ /**
334+ * Initialize the test pool if not already done
335+ * This is called lazily to avoid state pollution issues during construction
336+ */
337+ _initializeTestPool ( ) {
338+ if ( this . testPoolInitialized ) {
339+ return
340+ }
341+
325342 const files = this . codecept . testFiles
343+ if ( ! files || files . length === 0 ) {
344+ this . testPoolInitialized = true
345+ return
346+ }
347+
326348 const mocha = Container . mocha ( )
327349 mocha . files = files
328350 mocha . loadFiles ( )
@@ -333,15 +355,28 @@ class Workers extends EventEmitter {
333355 }
334356 } )
335357
336- // For pool mode, create empty groups for each worker
337- this . testGroups = populateGroups ( numberOfWorkers )
358+ // If no tests were found, fallback to using createGroupsOfTests approach
359+ // This works around state pollution issues
360+ if ( this . testPool . length === 0 && files . length > 0 ) {
361+ const testGroups = this . createGroupsOfTests ( 2 ) // Use 2 as a default for fallback
362+ for ( const group of testGroups ) {
363+ this . testPool . push ( ...group )
364+ }
365+ }
366+
367+ this . testPoolInitialized = true
338368 }
339369
340370 /**
341371 * Gets the next test from the pool
342372 * @returns {String|null } test uid or null if no tests available
343373 */
344374 getNextTest ( ) {
375+ // Initialize test pool lazily on first access
376+ if ( ! this . testPoolInitialized ) {
377+ this . _initializeTestPool ( )
378+ }
379+
345380 return this . testPool . shift ( ) || null
346381 }
347382
0 commit comments