11import  *  as  common  from  '../common/index.mjs' ; 
22import  {  describe ,  it ,  beforeEach  }  from  'node:test' ; 
3- import  {  once  }  from  'node:events' ; 
43import  assert  from  'node:assert' ; 
54import  {  spawn  }  from  'node:child_process' ; 
65import  {  writeFileSync  }  from  'node:fs' ; 
@@ -19,27 +18,10 @@ if (common.isAIX) {
1918} 
2019
2120const  indexContents  =  ` 
22-   const { setTimeout } = require("timers/promises"); 
23-   (async () => { 
24-       // Wait a few milliseconds to make sure that the 
25-       // parent process has time to attach its listeners 
26-       await setTimeout(200); 
27- 
28-       process.on('SIGTERM', () => { 
29-           console.log('__SIGTERM received__'); 
30-           process.exit(123); 
31-       }); 
32- 
33-       process.on('SIGINT', () => { 
34-           console.log('__SIGINT received__'); 
35-           process.exit(124); 
36-       }); 
37- 
38-       console.log('ready!'); 
39- 
40-       // Wait for a long time (just to keep the process alive) 
41-       await setTimeout(100_000_000); 
42-   })(); 
21+   process.on('SIGTERM', () => { console.log('__SIGTERM received__'); process.exit(); }); 
22+   process.on('SIGINT', () => { console.log('__SIGINT received__'); process.exit(); }); 
23+   process.send('script ready'); 
24+   setTimeout(() => {}, 100_000); 
4325` ; 
4426
4527let  indexPath  =  '' ; 
@@ -54,60 +36,82 @@ describe('test runner watch mode with --watch-kill-signal', () => {
5436  beforeEach ( refresh ) ; 
5537
5638  it ( 'defaults to SIGTERM' ,  async  ( )  =>  { 
57-     let  currentRun  =  Promise . withResolvers ( ) ; 
58-     const  child  =  spawn ( process . execPath ,  [ '--watch' ,  indexPath ] ,  { 
59-       cwd : tmpdir . path , 
60-     } ) ; 
39+     const  child  =  spawn ( 
40+       process . execPath , 
41+       [ '--watch' ,  indexPath ] , 
42+       { 
43+         cwd : tmpdir . path , 
44+         stdio : [ 'pipe' ,  'pipe' ,  'pipe' ,  'ipc' ] , 
45+       } 
46+     ) ; 
6147
6248    let  stdout  =  '' ; 
6349    child . stdout . on ( 'data' ,  ( data )  =>  { 
64-       stdout  +=  data . toString ( ) ; 
65-       currentRun . resolve ( ) ; 
50+       stdout  +=  `${ data }  ; 
51+       if  ( / _ _ ( S I G I N T | S I G T E R M )   r e c e i v e d _ _ / . test ( stdout ) )  { 
52+         child . kill ( ) ; 
53+       } 
6654    } ) ; 
6755
68-     await  currentRun . promise ; 
56+     child . on ( 'message' ,  ( msg )  =>  { 
57+       if  ( msg  ===  'script ready' )  { 
58+         writeFileSync ( indexPath ,  indexContents ) ; 
59+       } 
60+     } ) ; 
6961
70-     currentRun  =  Promise . withResolvers ( ) ; 
71-     writeFileSync ( indexPath ,  indexContents ) ; 
62+     await  new  Promise ( ( resolve )  => 
63+       child . on ( 'exit' ,  ( )  =>  { 
64+         resolve ( ) ; 
65+       } ) 
66+     ) ; 
7267
73-     await  currentRun . promise ; 
74-     child . kill ( ) ; 
75-     const  [ exitCode ]  =  await  once ( child ,  'exit' ) ; 
7668    assert . match ( stdout ,  / _ _ S I G T E R M   r e c e i v e d _ _ / ) ; 
77-     assert . strictEqual ( exitCode ,   123 ) ; 
69+     assert . doesNotMatch ( stdout ,   / _ _ S I G I N T   r e c e i v e d _ _ / ) ; 
7870  } ) ; 
7971
8072  it ( 'can be overridden (to SIGINT)' ,  async  ( )  =>  { 
81-     let  currentRun  =  Promise . withResolvers ( ) ; 
82-     const  child  =  spawn ( process . execPath ,  [ '--watch' ,  '--watch-kill-signal' ,  'SIGINT' ,  indexPath ] ,  { 
83-       cwd : tmpdir . path , 
84-     } ) ; 
85-     let  stdout  =  '' ; 
73+     const  child  =  spawn ( 
74+       process . execPath , 
75+       [ '--watch' ,  '--watch-kill-signal' ,  'SIGINT' ,  indexPath ] , 
76+       { 
77+         cwd : tmpdir . path , 
78+         stdio : [ 'pipe' ,  'pipe' ,  'pipe' ,  'ipc' ] , 
79+       } 
80+     ) ; 
8681
82+     let  stdout  =  '' ; 
8783    child . stdout . on ( 'data' ,  ( data )  =>  { 
88-       stdout  +=  data . toString ( ) ; 
89-       if  ( stdout . includes ( 'ready!' ) )  { 
90-         currentRun . resolve ( ) ; 
84+       stdout  +=  ` ${ data } ` ; 
85+       if  ( / _ _ ( S I G I N T | S I G T E R M )   r e c e i v e d _ _ / . test ( stdout ) )  { 
86+         child . kill ( ) ; 
9187      } 
9288    } ) ; 
9389
94-     await  currentRun . promise ; 
90+     child . on ( 'message' ,  ( msg )  =>  { 
91+       if  ( msg  ===  'script ready' )  { 
92+         writeFileSync ( indexPath ,  indexContents ) ; 
93+       } 
94+     } ) ; 
9595
96-     currentRun  =  Promise . withResolvers ( ) ; 
97-     writeFileSync ( indexPath ,  indexContents ) ; 
96+     await  new  Promise ( ( resolve )  => 
97+       child . on ( 'exit' ,  ( )  =>  { 
98+         resolve ( ) ; 
99+       } ) 
100+     ) ; 
98101
99-     await  currentRun . promise ; 
100-     child . kill ( ) ; 
101-     const  [ exitCode ]  =  await  once ( child ,  'exit' ) ; 
102102    assert . match ( stdout ,  / _ _ S I G I N T   r e c e i v e d _ _ / ) ; 
103-     assert . strictEqual ( exitCode ,   124 ) ; 
103+     assert . doesNotMatch ( stdout ,   / _ _ S I G T E R M   r e c e i v e d _ _ / ) ; 
104104  } ) ; 
105105
106106  it ( 'errors if an invalid signal is provided' ,  async  ( )  =>  { 
107107    const  currentRun  =  Promise . withResolvers ( ) ; 
108-     const  child  =  spawn ( process . execPath ,  [ '--watch' ,  '--watch-kill-signal' ,  'invalid_signal' ,  indexPath ] ,  { 
109-       cwd : tmpdir . path , 
110-     } ) ; 
108+     const  child  =  spawn ( 
109+       process . execPath , 
110+       [ '--watch' ,  '--watch-kill-signal' ,  'invalid_signal' ,  indexPath ] , 
111+       { 
112+         cwd : tmpdir . path , 
113+       } 
114+     ) ; 
111115    let  stdout  =  '' ; 
112116
113117    child . stderr . on ( 'data' ,  ( data )  =>  { 
@@ -117,6 +121,11 @@ describe('test runner watch mode with --watch-kill-signal', () => {
117121
118122    await  currentRun . promise ; 
119123
120-     assert . match ( stdout ,  new  RegExp ( / T y p e E r r o r   \[ E R R _ U N K N O W N _ S I G N A L \] :   U n k n o w n   s i g n a l :   i n v a l i d _ s i g n a l / ) ) ; 
124+     assert . match ( 
125+       stdout , 
126+       new  RegExp ( 
127+         / T y p e E r r o r   \[ E R R _ U N K N O W N _ S I G N A L \] :   U n k n o w n   s i g n a l :   i n v a l i d _ s i g n a l / 
128+       ) 
129+     ) ; 
121130  } ) ; 
122131} ) ; 
0 commit comments