@@ -40,6 +40,8 @@ export class TestRunner {
40
40
#scriptFile: string = 'bin/test.js'
41
41
#isMetaFile: picomatch . Matcher
42
42
#isTestFile: picomatch . Matcher
43
+ #scriptArgs: string [ ]
44
+ #initialFiltersArgs: string [ ]
43
45
44
46
/**
45
47
* In watch mode, after a file is changed, we wait for the current
@@ -79,6 +81,37 @@ export class TestRunner {
79
81
. map ( ( suite ) => suite . files )
80
82
. flat ( 1 )
81
83
)
84
+
85
+ this . #scriptArgs = this . #convertOptionsToArgs( )
86
+ this . #initialFiltersArgs = this . #convertFiltersToArgs( this . #options. filters )
87
+ }
88
+
89
+ /**
90
+ * Converts options to CLI args
91
+ */
92
+ #convertOptionsToArgs( ) {
93
+ const args : string [ ] = [ ]
94
+
95
+ if ( this . #options. reporters ) {
96
+ args . push ( '--reporters' )
97
+ args . push ( this . #options. reporters . join ( ',' ) )
98
+ }
99
+
100
+ if ( this . #options. timeout !== undefined ) {
101
+ args . push ( '--timeout' )
102
+ args . push ( String ( this . #options. timeout ) )
103
+ }
104
+
105
+ if ( this . #options. failed ) {
106
+ args . push ( '--failed' )
107
+ }
108
+
109
+ if ( this . #options. retries !== undefined ) {
110
+ args . push ( '--timeout' )
111
+ args . push ( String ( this . #options. retries ) )
112
+ }
113
+
114
+ return args
82
115
}
83
116
84
117
/**
@@ -135,14 +168,22 @@ export class TestRunner {
135
168
/**
136
169
* Runs tests
137
170
*/
138
- #runTests( port : string , filtersArgs : string [ ] , mode : 'blocking' | 'nonblocking' ) {
171
+ #runTests(
172
+ port : string ,
173
+ mode : 'blocking' | 'nonblocking' ,
174
+ filters ?: TestRunnerOptions [ 'filters' ]
175
+ ) {
139
176
this . #isBusy = true
140
177
178
+ const scriptArgs = filters
179
+ ? this . #convertFiltersToArgs( filters ) . concat ( this . #scriptArgs)
180
+ : this . #initialFiltersArgs. concat ( this . #scriptArgs)
181
+
141
182
this . #testScript = runNode ( this . #cwd, {
142
183
script : this . #scriptFile,
143
184
env : { PORT : port , ...this . #options. env } ,
144
185
nodeArgs : this . #options. nodeArgs ,
145
- scriptArgs : filtersArgs . concat ( this . #options . scriptArgs ) ,
186
+ scriptArgs,
146
187
} )
147
188
148
189
this . #testScript
@@ -166,13 +207,13 @@ export class TestRunner {
166
207
/**
167
208
* Restarts the HTTP server
168
209
*/
169
- #rerunTests( port : string , filtersArgs : string [ ] ) {
210
+ #rerunTests( port : string , filters ?: TestRunnerOptions [ 'filters' ] ) {
170
211
if ( this . #testScript) {
171
212
this . #testScript. removeAllListeners ( )
172
213
this . #testScript. kill ( 'SIGKILL' )
173
214
}
174
215
175
- this . #runTests( port , filtersArgs , 'blocking' )
216
+ this . #runTests( port , 'blocking' , filters )
176
217
}
177
218
178
219
/**
@@ -187,22 +228,22 @@ export class TestRunner {
187
228
/**
188
229
* Handles a non TypeScript file change
189
230
*/
190
- #handleFileChange( action : string , port : string , filters : string [ ] , relativePath : string ) {
231
+ #handleFileChange( action : string , port : string , relativePath : string ) {
191
232
if ( this . #isBusy) {
192
233
return
193
234
}
194
235
195
236
if ( isDotEnvFile ( relativePath ) || this . #isMetaFile( relativePath ) ) {
196
237
this . #clearScreen( )
197
238
this . #logger. log ( `${ this . #colors. green ( action ) } ${ relativePath } ` )
198
- this . #rerunTests( port , filters )
239
+ this . #rerunTests( port )
199
240
}
200
241
}
201
242
202
243
/**
203
244
* Handles TypeScript source file change
204
245
*/
205
- #handleSourceFileChange( action : string , port : string , filters : string [ ] , relativePath : string ) {
246
+ #handleSourceFileChange( action : string , port : string , relativePath : string ) {
206
247
if ( this . #isBusy) {
207
248
return
208
249
}
@@ -215,17 +256,14 @@ export class TestRunner {
215
256
* then only run that file
216
257
*/
217
258
if ( this . #isTestFile( relativePath ) ) {
218
- this . #rerunTests(
219
- port ,
220
- this . #convertFiltersToArgs( {
221
- ...this . #options. filters ,
222
- files : [ relativePath ] ,
223
- } )
224
- )
259
+ this . #rerunTests( port , {
260
+ ...this . #options. filters ,
261
+ files : [ relativePath ] ,
262
+ } )
225
263
return
226
264
}
227
265
228
- this . #rerunTests( port , filters )
266
+ this . #rerunTests( port )
229
267
}
230
268
231
269
/**
@@ -272,27 +310,25 @@ export class TestRunner {
272
310
*/
273
311
async run ( ) {
274
312
const port = String ( await getPort ( this . #cwd) )
275
- const initialFilters = this . #convertFiltersToArgs( this . #options. filters )
276
313
277
314
this . #clearScreen( )
278
315
this . #startAssetsServer( )
279
316
280
317
this . #logger. info ( 'booting application to run tests...' )
281
- this . #runTests( port , initialFilters , 'nonblocking' )
318
+ this . #runTests( port , 'nonblocking' )
282
319
}
283
320
284
321
/**
285
322
* Run tests in watch mode
286
323
*/
287
324
async runAndWatch ( ts : typeof tsStatic , options ?: { poll : boolean } ) {
288
325
const port = String ( await getPort ( this . #cwd) )
289
- const initialFilters = this . #convertFiltersToArgs( this . #options. filters )
290
326
291
327
this . #clearScreen( )
292
328
this . #startAssetsServer( )
293
329
294
330
this . #logger. info ( 'booting application to run tests...' )
295
- this . #runTests( port , initialFilters , 'blocking' )
331
+ this . #runTests( port , 'blocking' )
296
332
297
333
/**
298
334
* Create watcher using tsconfig.json file
@@ -330,26 +366,26 @@ export class TestRunner {
330
366
* Changes in TypeScript source file
331
367
*/
332
368
output . watcher . on ( 'source:add' , ( { relativePath } ) =>
333
- this . #handleSourceFileChange( 'add' , port , initialFilters , relativePath )
369
+ this . #handleSourceFileChange( 'add' , port , relativePath )
334
370
)
335
371
output . watcher . on ( 'source:change' , ( { relativePath } ) =>
336
- this . #handleSourceFileChange( 'update' , port , initialFilters , relativePath )
372
+ this . #handleSourceFileChange( 'update' , port , relativePath )
337
373
)
338
374
output . watcher . on ( 'source:unlink' , ( { relativePath } ) =>
339
- this . #handleSourceFileChange( 'delete' , port , initialFilters , relativePath )
375
+ this . #handleSourceFileChange( 'delete' , port , relativePath )
340
376
)
341
377
342
378
/**
343
379
* Changes in non-TypeScript source files
344
380
*/
345
381
output . watcher . on ( 'add' , ( { relativePath } ) =>
346
- this . #handleFileChange( 'add' , port , initialFilters , relativePath )
382
+ this . #handleFileChange( 'add' , port , relativePath )
347
383
)
348
384
output . watcher . on ( 'change' , ( { relativePath } ) =>
349
- this . #handleFileChange( 'update' , port , initialFilters , relativePath )
385
+ this . #handleFileChange( 'update' , port , relativePath )
350
386
)
351
387
output . watcher . on ( 'unlink' , ( { relativePath } ) =>
352
- this . #handleFileChange( 'delete' , port , initialFilters , relativePath )
388
+ this . #handleFileChange( 'delete' , port , relativePath )
353
389
)
354
390
}
355
391
}
0 commit comments