@@ -15,11 +15,16 @@ if (common.isIBMi)
1515 common . skip ( 'IBMi does not support `fs.watch()`' ) ;
1616
1717const supportsRecursive = common . isOSX || common . isWindows ;
18+ let disableRestart = false ;
1819
1920function restart ( file ) {
2021 // To avoid flakiness, we save the file repeatedly until test is done
2122 writeFileSync ( file , readFileSync ( file ) ) ;
22- const timer = setInterval ( ( ) => writeFileSync ( file , readFileSync ( file ) ) , 1000 ) ;
23+ const timer = setInterval ( ( ) => {
24+ if ( ! disableRestart ) {
25+ writeFileSync ( file , readFileSync ( file ) ) ;
26+ }
27+ } , common . platformTimeout ( 1000 ) ) ;
2328 return ( ) => clearInterval ( timer ) ;
2429}
2530
@@ -38,11 +43,15 @@ async function spawnWithRestarts({
3843 let stdout = '' ;
3944 let cancelRestarts ;
4045
46+ disableRestart = true ;
4147 const child = spawn ( execPath , [ '--watch' , '--no-warnings' , ...args ] , { encoding : 'utf8' } ) ;
4248 child . stderr . on ( 'data' , ( data ) => {
4349 stderr += data ;
4450 } ) ;
4551 child . stdout . on ( 'data' , async ( data ) => {
52+ if ( data . toString ( ) . includes ( 'Restarting' ) ) {
53+ disableRestart = true ;
54+ }
4655 stdout += data ;
4756 const restartsCount = stdout . match ( new RegExp ( `Restarting ${ printedArgs . replace ( / \\ / g, '\\\\' ) } ` , 'g' ) ) ?. length ?? 0 ;
4857 if ( restarts === 0 || ! isReady ( data . toString ( ) ) ) {
@@ -54,6 +63,9 @@ async function spawnWithRestarts({
5463 return ;
5564 }
5665 cancelRestarts ??= restart ( watchedFile ) ;
66+ if ( isReady ( data . toString ( ) ) ) {
67+ disableRestart = false ;
68+ }
5769 } ) ;
5870
5971 await once ( child , 'exit' ) ;
@@ -97,9 +109,9 @@ async function failWriteSucceed({ file, watchedFile }) {
97109
98110tmpdir . refresh ( ) ;
99111
100- // Warning: this suite can run safely with concurrency: true
101- // only if tests do not watch/depend on the same files
102- describe ( 'watch mode' , { concurrency : true , timeout : 60_000 } , ( ) => {
112+ // Warning: this suite cannot run safely with concurrency: true
113+ // because of the disableRestart flag used for controlling restarts
114+ describe ( 'watch mode' , { concurrency : false , timeout : 60_000 } , ( ) => {
103115 it ( 'should watch changes to a file - event loop ended' , async ( ) => {
104116 const file = createTmpFile ( ) ;
105117 const { stderr, stdout } = await spawnWithRestarts ( { file } ) ;
0 commit comments