@@ -6,6 +6,7 @@ const path = require('node:path');
6
6
const chokidar = require ( 'chokidar' ) ;
7
7
const Context = require ( '../context' ) ;
8
8
const collectFiles = require ( './collect-files' ) ;
9
+ const glob = require ( 'glob' ) ;
9
10
10
11
/**
11
12
* Exports the `watchRun` function that runs mocha in "watch" mode.
@@ -136,6 +137,42 @@ exports.watchRun = (mocha, {watchFiles, watchIgnore}, fileCollectParams) => {
136
137
} ) ;
137
138
} ;
138
139
140
+ class GlobFilesTracker {
141
+ constructor ( watchFiles , watchIgnore ) {
142
+ this . watchFilesSet = new Set ( ) ;
143
+ this . watchFiles = watchFiles ;
144
+ this . watchIgnore = watchIgnore ;
145
+ }
146
+
147
+ regenerate ( ) {
148
+ const watchIgnoreSet = new Set ( ) ;
149
+ for ( const pattern of this . watchIgnore ) {
150
+ glob . sync ( pattern , { dot : true } ) . forEach ( filePath => watchIgnoreSet . add ( filePath ) ) ;
151
+ }
152
+
153
+ const globOpts = {
154
+ dot : true ,
155
+ ignore : {
156
+ childrenIgnored : pathToCheck => watchIgnoreSet . has ( pathToCheck . relative ( ) )
157
+ }
158
+ } ;
159
+
160
+ this . watchFilesSet . clear ( ) ;
161
+ for ( const pattern of this . watchFiles ) {
162
+ glob . sync ( pattern , globOpts ) . forEach ( pathToCheck => {
163
+ if ( watchIgnoreSet . has ( pathToCheck ) ) {
164
+ return ;
165
+ }
166
+ this . watchFilesSet . add ( pathToCheck ) ;
167
+ } ) ;
168
+ }
169
+ }
170
+
171
+ has ( filePath ) {
172
+ return this . watchFilesSet . has ( filePath )
173
+ }
174
+ }
175
+
139
176
/**
140
177
* Bootstraps a chokidar watcher. Handles keyboard input & signals
141
178
* @param {Mocha } mocha - Mocha instance
@@ -167,8 +204,10 @@ const createWatcher = (
167
204
// we handle global fixtures manually
168
205
mocha . enableGlobalSetup ( false ) . enableGlobalTeardown ( false ) ;
169
206
170
- const watcher = chokidar . watch ( watchFiles , {
171
- ignored : watchIgnore ,
207
+ const tracker = new GlobFilesTracker ( watchFiles , watchIgnore ) ;
208
+ tracker . regenerate ( ) ;
209
+
210
+ const watcher = chokidar . watch ( '.' , {
172
211
ignoreInitial : true
173
212
} ) ;
174
213
@@ -184,8 +223,13 @@ const createWatcher = (
184
223
rerunner . run ( ) ;
185
224
} ) ;
186
225
187
- watcher . on ( 'all' , ( ) => {
188
- rerunner . scheduleRun ( ) ;
226
+ watcher . on ( 'all' , ( event , filePath ) => {
227
+ if ( event === 'add' ) {
228
+ tracker . regenerate ( ) ;
229
+ }
230
+ if ( tracker . has ( filePath ) ) {
231
+ rerunner . scheduleRun ( ) ;
232
+ }
189
233
} ) ;
190
234
191
235
hideCursor ( ) ;
0 commit comments