@@ -42,20 +42,22 @@ let configurationRequestId = 0
4242 *
4343 * @param {object } message
4444 * @param {() => void } [onError]
45+ * @param {() => void } [onDone]
4546 * @returns {void }
4647 */
47- function sendWebdriverioMessage ( message , onError ) {
48+ function sendWebdriverioMessage ( message , onError , onDone ) {
4849 if ( ! process . send || ! process . connected ) {
4950 onError ?. ( )
51+ onDone ?. ( )
5052 return
5153 }
5254
5355 process . send ( createWebdriverioWorkerMessage ( message ) , ( error ) => {
54- if ( ! error ) {
55- return
56+ if ( error ) {
57+ log . error ( 'WebdriverIO Test Optimization IPC error' , error )
58+ onError ?. ( )
5659 }
57- log . error ( 'WebdriverIO Test Optimization IPC error' , error )
58- onError ?. ( )
60+ onDone ?. ( )
5961 } )
6062}
6163
@@ -266,10 +268,12 @@ function getWebdriverioSuiteResults (runner) {
266268 * Sends suite results to the WebdriverIO launcher.
267269 *
268270 * @param {object } runner
271+ * @param {() => void } [onDone]
269272 * @returns {void }
270273 */
271- function reportWebdriverioSuiteResults ( runner ) {
274+ function reportWebdriverioSuiteResults ( runner , onDone ) {
272275 if ( ! isWebdriverioWorker ) {
276+ onDone ?. ( )
273277 return
274278 }
275279
@@ -279,7 +283,32 @@ function reportWebdriverioSuiteResults (runner) {
279283 content : {
280284 results : getWebdriverioSuiteResults ( runner ) ,
281285 } ,
282- } )
286+ } , undefined , onDone )
287+ }
288+
289+ /**
290+ * Flushes worker payloads before reporting suite results and completing Mocha.
291+ *
292+ * @param {object } runner
293+ * @param {() => void } onDone
294+ * @returns {void }
295+ */
296+ function finishWebdriverioWorker ( runner , onDone ) {
297+ try {
298+ workerFinishCh . publish ( {
299+ onDone : ( ) => {
300+ try {
301+ reportWebdriverioSuiteResults ( runner , onDone )
302+ } catch ( error ) {
303+ log . error ( 'WebdriverIO Test Optimization worker completion error' , error )
304+ onDone ( )
305+ }
306+ } ,
307+ } )
308+ } catch ( error ) {
309+ log . error ( 'WebdriverIO Test Optimization worker completion error' , error )
310+ onDone ( )
311+ }
283312}
284313
285314function isFailedTestReplayEnabled ( ) {
@@ -376,11 +405,22 @@ addHook({
376405 if ( isFailedTestReplayEnabled ( ) ) {
377406 patchFailedTestReplayHookUp ( Runner )
378407 }
379- // Flush after the worker finishes its Mocha run, including grouped spec files.
380- this . once ( 'end' , ( ) => {
381- workerFinishCh . publish ( )
382- reportWebdriverioSuiteResults ( this )
383- } )
408+ const onRunDone = args [ 0 ]
409+ if ( isWebdriverioWorker && typeof onRunDone === 'function' ) {
410+ args [ 0 ] = ( ...onRunDoneArgs ) => {
411+ finishWebdriverioWorker ( this , ( ) => onRunDone ( ...onRunDoneArgs ) )
412+ }
413+ } else {
414+ // Flush after the worker finishes its Mocha run, including grouped spec files.
415+ this . once ( 'end' , ( ) => {
416+ try {
417+ workerFinishCh . publish ( )
418+ reportWebdriverioSuiteResults ( this )
419+ } catch ( error ) {
420+ log . error ( 'WebdriverIO Test Optimization worker completion error' , error )
421+ }
422+ } )
423+ }
384424 this . on ( 'test' , getOnTestHandler ( false ) )
385425
386426 this . on ( 'test end' , getOnTestEndHandler ( config ) )
0 commit comments