@@ -5,21 +5,40 @@ const { event } = require('codeceptjs');
5
5
module . exports = async ( req , res ) => {
6
6
debug ( 'Stopping test execution' ) ;
7
7
8
- if ( global . runner ) {
9
- global . runner . abort ( ) ;
10
-
11
- // Ensure we properly signal test completion and reset running state
12
- event . dispatcher . once ( event . all . result , ( ) => {
13
- global . runner . _abort = false ;
14
- debug ( 'Test runner stopped and reset' ) ;
15
- // Emit exit event to reset frontend running state
16
- wsEvents . codeceptjs . exit ( - 1 ) ; // -1 indicates stopped by user
17
- } ) ;
18
- } else {
19
- // If no runner is active, still emit exit event to reset frontend state
20
- debug ( 'No active runner found, resetting state' ) ;
8
+ try {
9
+ if ( global . runner ) {
10
+ debug ( 'Aborting active runner' ) ;
11
+ global . runner . abort ( ) ;
12
+
13
+ // Set a timeout to ensure we don't wait forever
14
+ const timeout = setTimeout ( ( ) => {
15
+ debug ( 'Stop timeout reached, forcing exit event' ) ;
16
+ wsEvents . codeceptjs . exit ( - 1 ) ;
17
+ global . runner = null ;
18
+ } , 5000 ) ; // 5 second timeout
19
+
20
+ // Ensure we properly signal test completion and reset running state
21
+ event . dispatcher . once ( event . all . result , ( ) => {
22
+ clearTimeout ( timeout ) ;
23
+ if ( global . runner ) {
24
+ global . runner . _abort = false ;
25
+ }
26
+ global . runner = null ;
27
+ debug ( 'Test runner stopped and reset' ) ;
28
+ // Emit exit event to reset frontend running state
29
+ wsEvents . codeceptjs . exit ( - 1 ) ; // -1 indicates stopped by user
30
+ } ) ;
31
+ } else {
32
+ // If no runner is active, still emit exit event to reset frontend state
33
+ debug ( 'No active runner found, resetting state' ) ;
34
+ wsEvents . codeceptjs . exit ( - 1 ) ;
35
+ }
36
+
37
+ return res . status ( 200 ) . send ( 'OK' ) ;
38
+ } catch ( error ) {
39
+ debug ( 'Error stopping test execution:' , error ) ;
40
+ // Always emit exit event to reset state even if there's an error
21
41
wsEvents . codeceptjs . exit ( - 1 ) ;
42
+ return res . status ( 500 ) . send ( 'Failed to stop execution' ) ;
22
43
}
23
-
24
- return res . status ( 200 ) . send ( 'OK' ) ;
25
44
} ;
0 commit comments