Skip to content

Commit b83078c

Browse files
committed
feat: accept rest of japa flags as config options
1 parent 9164284 commit b83078c

File tree

2 files changed

+66
-25
lines changed

2 files changed

+66
-25
lines changed

src/test_runner.ts

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export class TestRunner {
4040
#scriptFile: string = 'bin/test.js'
4141
#isMetaFile: picomatch.Matcher
4242
#isTestFile: picomatch.Matcher
43+
#scriptArgs: string[]
44+
#initialFiltersArgs: string[]
4345

4446
/**
4547
* In watch mode, after a file is changed, we wait for the current
@@ -79,6 +81,37 @@ export class TestRunner {
7981
.map((suite) => suite.files)
8082
.flat(1)
8183
)
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
82115
}
83116

84117
/**
@@ -135,14 +168,22 @@ export class TestRunner {
135168
/**
136169
* Runs tests
137170
*/
138-
#runTests(port: string, filtersArgs: string[], mode: 'blocking' | 'nonblocking') {
171+
#runTests(
172+
port: string,
173+
mode: 'blocking' | 'nonblocking',
174+
filters?: TestRunnerOptions['filters']
175+
) {
139176
this.#isBusy = true
140177

178+
const scriptArgs = filters
179+
? this.#convertFiltersToArgs(filters).concat(this.#scriptArgs)
180+
: this.#initialFiltersArgs.concat(this.#scriptArgs)
181+
141182
this.#testScript = runNode(this.#cwd, {
142183
script: this.#scriptFile,
143184
env: { PORT: port, ...this.#options.env },
144185
nodeArgs: this.#options.nodeArgs,
145-
scriptArgs: filtersArgs.concat(this.#options.scriptArgs),
186+
scriptArgs,
146187
})
147188

148189
this.#testScript
@@ -166,13 +207,13 @@ export class TestRunner {
166207
/**
167208
* Restarts the HTTP server
168209
*/
169-
#rerunTests(port: string, filtersArgs: string[]) {
210+
#rerunTests(port: string, filters?: TestRunnerOptions['filters']) {
170211
if (this.#testScript) {
171212
this.#testScript.removeAllListeners()
172213
this.#testScript.kill('SIGKILL')
173214
}
174215

175-
this.#runTests(port, filtersArgs, 'blocking')
216+
this.#runTests(port, 'blocking', filters)
176217
}
177218

178219
/**
@@ -187,22 +228,22 @@ export class TestRunner {
187228
/**
188229
* Handles a non TypeScript file change
189230
*/
190-
#handleFileChange(action: string, port: string, filters: string[], relativePath: string) {
231+
#handleFileChange(action: string, port: string, relativePath: string) {
191232
if (this.#isBusy) {
192233
return
193234
}
194235

195236
if (isDotEnvFile(relativePath) || this.#isMetaFile(relativePath)) {
196237
this.#clearScreen()
197238
this.#logger.log(`${this.#colors.green(action)} ${relativePath}`)
198-
this.#rerunTests(port, filters)
239+
this.#rerunTests(port)
199240
}
200241
}
201242

202243
/**
203244
* Handles TypeScript source file change
204245
*/
205-
#handleSourceFileChange(action: string, port: string, filters: string[], relativePath: string) {
246+
#handleSourceFileChange(action: string, port: string, relativePath: string) {
206247
if (this.#isBusy) {
207248
return
208249
}
@@ -215,17 +256,14 @@ export class TestRunner {
215256
* then only run that file
216257
*/
217258
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+
})
225263
return
226264
}
227265

228-
this.#rerunTests(port, filters)
266+
this.#rerunTests(port)
229267
}
230268

231269
/**
@@ -272,27 +310,25 @@ export class TestRunner {
272310
*/
273311
async run() {
274312
const port = String(await getPort(this.#cwd))
275-
const initialFilters = this.#convertFiltersToArgs(this.#options.filters)
276313

277314
this.#clearScreen()
278315
this.#startAssetsServer()
279316

280317
this.#logger.info('booting application to run tests...')
281-
this.#runTests(port, initialFilters, 'nonblocking')
318+
this.#runTests(port, 'nonblocking')
282319
}
283320

284321
/**
285322
* Run tests in watch mode
286323
*/
287324
async runAndWatch(ts: typeof tsStatic, options?: { poll: boolean }) {
288325
const port = String(await getPort(this.#cwd))
289-
const initialFilters = this.#convertFiltersToArgs(this.#options.filters)
290326

291327
this.#clearScreen()
292328
this.#startAssetsServer()
293329

294330
this.#logger.info('booting application to run tests...')
295-
this.#runTests(port, initialFilters, 'blocking')
331+
this.#runTests(port, 'blocking')
296332

297333
/**
298334
* Create watcher using tsconfig.json file
@@ -330,26 +366,26 @@ export class TestRunner {
330366
* Changes in TypeScript source file
331367
*/
332368
output.watcher.on('source:add', ({ relativePath }) =>
333-
this.#handleSourceFileChange('add', port, initialFilters, relativePath)
369+
this.#handleSourceFileChange('add', port, relativePath)
334370
)
335371
output.watcher.on('source:change', ({ relativePath }) =>
336-
this.#handleSourceFileChange('update', port, initialFilters, relativePath)
372+
this.#handleSourceFileChange('update', port, relativePath)
337373
)
338374
output.watcher.on('source:unlink', ({ relativePath }) =>
339-
this.#handleSourceFileChange('delete', port, initialFilters, relativePath)
375+
this.#handleSourceFileChange('delete', port, relativePath)
340376
)
341377

342378
/**
343379
* Changes in non-TypeScript source files
344380
*/
345381
output.watcher.on('add', ({ relativePath }) =>
346-
this.#handleFileChange('add', port, initialFilters, relativePath)
382+
this.#handleFileChange('add', port, relativePath)
347383
)
348384
output.watcher.on('change', ({ relativePath }) =>
349-
this.#handleFileChange('update', port, initialFilters, relativePath)
385+
this.#handleFileChange('update', port, relativePath)
350386
)
351387
output.watcher.on('unlink', ({ relativePath }) =>
352-
this.#handleFileChange('delete', port, initialFilters, relativePath)
388+
this.#handleFileChange('delete', port, relativePath)
353389
)
354390
}
355391
}

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ export type TestRunnerOptions = {
8787
ignoreTags: string[]
8888
}>
8989

90+
reporters?: string[]
91+
timeout?: number
92+
retries?: number
93+
failed?: boolean
94+
9095
/**
9196
* All other tags are provided as a collection of
9297
* arguments

0 commit comments

Comments
 (0)