@@ -95,6 +95,11 @@ async function runPoolTests() {
9595 const testUid = eventData . test
9696
9797 try {
98+ // In pool mode, we need to create a fresh Mocha instance for each test
99+ // because Mocha instances become disposed after running tests
100+ container . createMocha ( ) // Create fresh Mocha instance
101+ const mocha = container . mocha ( )
102+
98103 // Filter to run only the assigned test
99104 filterTestById ( testUid )
100105
@@ -135,15 +140,28 @@ async function runPoolTests() {
135140 // Log teardown errors but don't fail
136141 console . error ( 'Teardown error:' , err )
137142 }
143+
144+ // Close worker thread when pool mode is complete
145+ parentPort ?. close ( )
138146}
139147
140148function filterTestById ( testUid ) {
141149 // Reload test files fresh for each test in pool mode
142150 const files = codecept . testFiles
151+
152+ // Get the existing mocha instance
143153 const mocha = container . mocha ( )
144154
145- // Clear existing suites and reload
155+ // Clear suites and tests but preserve other mocha settings
146156 mocha . suite . suites = [ ]
157+ mocha . suite . tests = [ ]
158+
159+ // Clear require cache for test files to ensure fresh loading
160+ files . forEach ( file => {
161+ delete require . cache [ require . resolve ( file ) ]
162+ } )
163+
164+ // Set files and load them
147165 mocha . files = files
148166 mocha . loadFiles ( )
149167
@@ -183,22 +201,35 @@ function initializeListeners() {
183201
184202 // steps
185203 event . dispatcher . on ( event . step . finished , step => sendToParentThread ( { event : event . step . finished , workerIndex, data : step . simplify ( ) } ) )
186- event . dispatcher . on ( event . step . started , step => sendToParentThread ( { event : event . step . started , workerIndex, data : step . simplify ( ) } ) )
204+ event . dispatcher . on ( event . step . started , step => sendToParentThread ( { event : step . started , workerIndex, data : step . simplify ( ) } ) )
187205 event . dispatcher . on ( event . step . passed , step => sendToParentThread ( { event : event . step . passed , workerIndex, data : step . simplify ( ) } ) )
188206 event . dispatcher . on ( event . step . failed , step => sendToParentThread ( { event : event . step . failed , workerIndex, data : step . simplify ( ) } ) )
189207
190208 event . dispatcher . on ( event . hook . failed , ( hook , err ) => sendToParentThread ( { event : event . hook . failed , workerIndex, data : { ...hook . simplify ( ) , err } } ) )
191209 event . dispatcher . on ( event . hook . passed , hook => sendToParentThread ( { event : event . hook . passed , workerIndex, data : hook . simplify ( ) } ) )
192210 event . dispatcher . on ( event . hook . finished , hook => sendToParentThread ( { event : event . hook . finished , workerIndex, data : hook . simplify ( ) } ) )
193211
194- event . dispatcher . once ( event . all . after , ( ) => {
195- sendToParentThread ( { event : event . all . after , workerIndex, data : container . result ( ) . simplify ( ) } )
196- } )
197- // all
198- event . dispatcher . once ( event . all . result , ( ) => {
199- sendToParentThread ( { event : event . all . result , workerIndex, data : container . result ( ) . simplify ( ) } )
200- parentPort ?. close ( )
201- } )
212+ if ( ! poolMode ) {
213+ // In regular mode, close worker after all tests are complete
214+ event . dispatcher . once ( event . all . after , ( ) => {
215+ sendToParentThread ( { event : event . all . after , workerIndex, data : container . result ( ) . simplify ( ) } )
216+ } )
217+ // all
218+ event . dispatcher . once ( event . all . result , ( ) => {
219+ sendToParentThread ( { event : event . all . result , workerIndex, data : container . result ( ) . simplify ( ) } )
220+ parentPort ?. close ( )
221+ } )
222+ } else {
223+ // In pool mode, don't close worker after individual test completion
224+ // The worker will close when it receives 'NO_MORE_TESTS' message
225+ event . dispatcher . on ( event . all . after , ( ) => {
226+ sendToParentThread ( { event : event . all . after , workerIndex, data : container . result ( ) . simplify ( ) } )
227+ } )
228+ event . dispatcher . on ( event . all . result , ( ) => {
229+ sendToParentThread ( { event : event . all . result , workerIndex, data : container . result ( ) . simplify ( ) } )
230+ // Don't close parentPort in pool mode - let the pool manager handle worker lifecycle
231+ } )
232+ }
202233}
203234
204235function disablePause ( ) {
0 commit comments