@@ -42,6 +42,15 @@ const testDir = tmpdir.path;
4242
4343tmpdir . refresh ( ) ;
4444
45+ // Because macOS (and possibly other operating systems) can return a watcher
46+ // before it is actually watching, we need to repeat the operation to avoid
47+ // a race condition.
48+ function repeat ( fn ) {
49+ setImmediate ( fn ) ;
50+ const interval = setInterval ( fn , 5000 ) ;
51+ return interval ;
52+ }
53+
4554{
4655 const filepath = path . join ( testDir , 'watch.txt' ) ;
4756
@@ -54,12 +63,11 @@ tmpdir.refresh();
5463 if ( expectFilePath ) {
5564 assert . strictEqual ( filename , 'watch.txt' ) ;
5665 }
66+ clearInterval ( interval ) ;
5767 watcher . close ( ) ;
5868 } ) ) ;
5969
60- setImmediate ( function ( ) {
61- fs . writeFileSync ( filepath , 'world' ) ;
62- } ) ;
70+ const interval = repeat ( ( ) => { fs . writeFileSync ( filepath , 'world' ) ; } ) ;
6371}
6472
6573{
@@ -76,12 +84,11 @@ tmpdir.refresh();
7684 if ( expectFilePath ) {
7785 assert . strictEqual ( filename , 'hasOwnProperty' ) ;
7886 }
87+ clearInterval ( interval ) ;
7988 watcher . close ( ) ;
8089 } ) ) ;
8190
82- setImmediate ( function ( ) {
83- fs . writeFileSync ( filepathAbs , 'pardner' ) ;
84- } ) ;
91+ const interval = repeat ( ( ) => { fs . writeFileSync ( filepathAbs , 'pardner' ) ; } ) ;
8592}
8693
8794{
@@ -97,14 +104,15 @@ tmpdir.refresh();
97104 } else {
98105 assert . strictEqual ( filename , null ) ;
99106 }
107+ clearInterval ( interval ) ;
100108 watcher . close ( ) ;
101109 } ) ) ;
102110
103- setImmediate ( function ( ) {
111+ const interval = repeat ( ( ) => {
112+ fs . rmSync ( filepath , { force : true } ) ;
104113 const fd = fs . openSync ( filepath , 'w' ) ;
105114 fs . closeSync ( fd ) ;
106115 } ) ;
107-
108116}
109117
110118// https://github.com/joyent/node/issues/2293 - non-persistent watcher should
0 commit comments