@@ -6,7 +6,8 @@ import path from 'node:path';
66import assert from 'node:assert' ;
77import process from 'node:process' ;
88import { describe , it , beforeEach , afterEach } from 'node:test' ;
9- import { writeFileSync , mkdirSync } from 'node:fs' ;
9+ import { writeFileSync , mkdirSync , appendFileSync } from 'node:fs' ;
10+ import { createInterface } from 'node:readline' ;
1011import { setTimeout } from 'node:timers/promises' ;
1112import { once } from 'node:events' ;
1213import { spawn } from 'node:child_process' ;
@@ -51,6 +52,33 @@ describe('watch mode file watcher', () => {
5152 assert . strictEqual ( changesCount , 1 ) ;
5253 } ) ;
5354
55+ it ( 'should watch changed files with same prefix path string' , async ( ) => {
56+ mkdirSync ( tmpdir . resolve ( 'subdir' ) ) ;
57+ mkdirSync ( tmpdir . resolve ( 'sub' ) ) ;
58+ const file1 = tmpdir . resolve ( 'subdir' , 'file1.mjs' ) ;
59+ const file2 = tmpdir . resolve ( 'sub' , 'file2.mjs' ) ;
60+ writeFileSync ( file2 , 'export const hello = () => { return "hello world"; };' ) ;
61+ writeFileSync ( file1 , 'import { hello } from "../sub/file2.mjs"; console.log(hello());' ) ;
62+
63+ const child = spawn ( process . execPath ,
64+ [ '--watch' , file1 ] ,
65+ { stdio : [ 'ignore' , 'pipe' , 'ignore' ] } ) ;
66+ let completeCount = 0 ;
67+ for await ( const line of createInterface ( child . stdout ) ) {
68+ if ( line . startsWith ( 'Completed running' ) ) {
69+ completeCount ++ ;
70+ }
71+ if ( completeCount === 1 ) {
72+ appendFileSync ( file1 , '\n // append 1' ) ;
73+ }
74+ // The file is reloaded due to file watching
75+ if ( completeCount === 2 ) {
76+ child . kill ( ) ;
77+ await once ( child , 'exit' ) ;
78+ }
79+ }
80+ } ) ;
81+
5482 it ( 'should debounce changes' , async ( ) => {
5583 const file = tmpdir . resolve ( 'file2' ) ;
5684 writeFileSync ( file , 'written' ) ;
0 commit comments