22// parent process argv when spawning the watch child.
33import * as common from '../common/index.mjs' ;
44import assert from 'node:assert' ;
5- import { writeFileSync } from 'node:fs' ;
5+ import { setTimeout } from 'node:timers/promises' ;
6+ import { writeFile } from 'node:fs/promises' ;
67import { join } from 'node:path' ;
78import { run } from 'node:test' ;
89import tmpdir from '../common/tmpdir.js' ;
@@ -11,31 +12,36 @@ import { skipIfNoWatch } from '../common/watch.js';
1112skipIfNoWatch ( ) ;
1213tmpdir . refresh ( ) ;
1314
14- writeFileSync ( join ( tmpdir . path , 'test.js' ) , `
15+ await writeFile ( join ( tmpdir . path , 'test.js' ) , `
1516const test = require('node:test');
1617
1718test('test ran from cwd', () => {});
1819` ) ;
1920
21+ // Add some delay to ensure the OS sends the FS events before watch mode is started.
22+ await setTimeout ( common . platformTimeout ( 100 ) ) ;
23+
2024const passed = [ ] ;
2125const controller = new AbortController ( ) ;
2226const stream = run ( {
2327 cwd : tmpdir . path ,
2428 watch : true ,
2529 signal : controller . signal ,
2630 isolation : 'none' ,
27- } ) . on ( 'data' , function ( { type } ) {
28- if ( type === 'test:watch:drained' ) {
29- stream . removeAllListeners ( 'test:fail' ) ;
30- stream . removeAllListeners ( 'test:pass ' ) ;
31- controller . abort ( ) ;
32- }
31+ } ) . on ( 'data' , ( { type } ) => {
32+ if ( type !== 'test:watch:drained' ) return ;
33+
34+ stream . removeAllListeners ( 'test:fail ' ) ;
35+ stream . removeAllListeners ( 'test:pass' ) ;
36+ controller . abort ( ) ;
3337} ) ;
3438
35- stream . on ( 'test:fail' , common . mustNotCall ( ) ) ;
36- stream . on ( 'test:pass' , common . mustCall ( ( data ) => passed . push ( data . name ) , 1 ) ) ;
37- // eslint-disable-next-line no-unused-vars
38- for await ( const _ of stream ) ;
39+ stream . on ( 'test:watch:restarted' , common . mustNotCall ( 'test:watch:restarted' ) ) ;
40+ stream . on ( 'test:fail' , common . mustNotCall ( 'test:fail' ) ) ;
41+ stream . on ( 'test:pass' , common . mustCall ( ( data ) => passed . push ( data . name ) ) ) ;
42+
43+ // eslint-disable-next-line no-empty-pattern
44+ for await ( const { } of stream ) ;
3945
4046// Validate the expected test ran by name:
4147assert . deepStrictEqual ( passed , [ 'test ran from cwd' ] ) ;
0 commit comments