@@ -15,11 +15,16 @@ if (common.isIBMi)
1515common . 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
2122writeFileSync ( 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 ) ) ;
2328return ( ) => clearInterval ( timer ) ;
2429}
2530
@@ -38,11 +43,15 @@ async function spawnWithRestarts({
3843let stdout = '' ;
3944let cancelRestarts ;
4045
46+ disableRestart = true ;
4147const child = spawn ( execPath , [ '--watch' , '--no-warnings' , ...args ] , { encoding : 'utf8' } ) ;
4248child . stderr . on ( 'data' , ( data ) => {
4349stderr += data ;
4450} ) ;
4551child . stdout . on ( 'data' , async ( data ) => {
52+ if ( data . toString ( ) . includes ( 'Restarting' ) ) {
53+ disableRestart = true ;
54+ }
4655stdout += data ;
4756const restartsCount = stdout . match ( new RegExp ( `Restarting ${ printedArgs . replace ( / \\ / g, '\\\\' ) } ` , 'g' ) ) ?. length ?? 0 ;
4857if ( restarts === 0 || ! isReady ( data . toString ( ) ) ) {
@@ -54,6 +63,9 @@ async function spawnWithRestarts({
5463return ;
5564}
5665cancelRestarts ??= restart ( watchedFile ) ;
66+ if ( isReady ( data . toString ( ) ) ) {
67+ disableRestart = false ;
68+ }
5769} ) ;
5870
5971await 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 } , ( ) => {
103115it ( 'should watch changes to a file - event loop ended' , async ( ) => {
104116const file = createTmpFile ( ) ;
105117const { stderr, stdout } = await spawnWithRestarts ( { file } ) ;
0 commit comments