forked from japa/runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
499 lines (432 loc) · 12 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
* @japa/runner
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import getopts from 'getopts'
import { extname } from 'path'
import fastGlob from 'fast-glob'
import inclusion from 'inclusion'
import { pathToFileURL } from 'url'
import { Hooks } from '@poppinss/hooks'
import { ErrorsPrinter } from '@japa/errors-printer'
import { Emitter, Refiner, TestExecutor, ReporterContract } from '@japa/core'
import { Test, TestContext, Group, Suite, Runner } from './src/Core'
import {
Config,
Filters,
PluginFn,
RunnerHooksHandler,
RunnerHooksCleanupHandler,
} from './src/Contracts'
export {
Test,
Config,
Suite,
Runner,
Group,
PluginFn,
TestContext,
ReporterContract,
RunnerHooksHandler,
RunnerHooksCleanupHandler,
}
/**
* Filtering layers allowed by the refiner
*/
const refinerFilteringLayers = ['tests', 'groups', 'tags'] as const
/**
* Reference to the recently imported file. We pass it to the
* test and the group both
*/
let recentlyImportedFile: string
/**
* Global timeout for tests. Fetched from runner options or suites
* options
*/
let globalTimeout: number
/**
* Function to create the test context for the test
*/
const getContext = (testInstance: Test<any>) => new TestContext(testInstance)
/**
* The global reference to the tests emitter
*/
const emitter = new Emitter()
/**
* Active suite for tests
*/
let activeSuite = new Suite('default', emitter)
/**
* Currently active group
*/
let activeGroup: Group | undefined
/**
* Configuration options
*/
let runnerOptions: Required<Config>
/**
* Ensure the configure method has been called
*/
function ensureIsConfigured(message: string) {
if (!runnerOptions) {
throw new Error(message)
}
}
/**
* Validate suites filter to ensure a wrong suite is not
* mentioned
*/
function validateSuitesFilter() {
if (!('suites' in runnerOptions)) {
return
}
if (!runnerOptions.filters.suites || !runnerOptions.filters.suites.length) {
return
}
const suites = runnerOptions.suites.map(({ name }) => name)
const invalidSuites = runnerOptions.filters.suites.filter((suite) => !suites.includes(suite))
if (invalidSuites.length) {
throw new Error(
`Unrecognized suite "${invalidSuites[0]}". Make sure to define it in the config first`
)
}
}
/**
* End tests. We wait for the "beforeExit" event when
* forceExit is not set to true
*/
async function endTests(runner: Runner) {
if (runnerOptions.forceExit) {
await runner.end()
} else {
return new Promise<void>((resolve) => {
async function beforeExit() {
process.removeListener('beforeExit', beforeExit)
await runner.end()
resolve()
}
process.on('beforeExit', beforeExit)
})
}
}
/**
* Process command line argument into a string value
*/
function processAsString(
argv: Record<string, any>,
flagName: string,
onMatch: (value: string[]) => any
): void {
const flag = argv[flagName]
if (flag) {
onMatch((Array.isArray(flag) ? flag : flag.split(',')).map((tag: string) => tag.trim()))
}
}
/**
* Find if the file path matches the files filter array.
* The ending of the file is matched
*/
function isFileAllowed(filePath: string, filters: string[]): boolean {
return !!filters.find((matcher) => {
if (filePath.endsWith(matcher)) {
return true
}
return filePath.replace(extname(filePath), '').endsWith(matcher)
})
}
/**
* Returns "true" when no filters are applied or the name is part
* of the applied filter
*/
function isSuiteAllowed(name: string, filters?: string[]) {
if (!filters || !filters.length) {
return true
}
return filters.includes(name)
}
/**
* Configure the tests runner
*/
export function configure(options: Config) {
const defaultOptions: Required<Config> = {
cwd: process.cwd(),
files: [],
suites: [],
plugins: [],
reporters: [],
timeout: 2000,
filters: {},
setup: [],
teardown: [],
importer: (filePath) => inclusion(pathToFileURL(filePath).href),
refiner: new Refiner({}),
forceExit: false,
configureSuite: () => {},
}
runnerOptions = Object.assign(defaultOptions, options)
}
/**
* Add a new test
*/
export function test(title: string, callback?: TestExecutor<TestContext, undefined>) {
ensureIsConfigured('Cannot add test without configuring the test runner')
const testInstance = new Test<undefined>(title, getContext, emitter, runnerOptions.refiner)
/**
* Set filename
*/
testInstance.options.meta.fileName = recentlyImportedFile
/**
* Define timeout on the test when exists globally
*/
if (globalTimeout !== undefined) {
testInstance.timeout(globalTimeout)
}
/**
* Define test executor function
*/
if (callback) {
testInstance.run(callback)
}
/**
* Add test to the group or suite
*/
if (activeGroup) {
activeGroup.add(testInstance)
} else {
activeSuite.add(testInstance)
}
return testInstance
}
/**
* Define test group
*/
test.group = function (title: string, callback: (group: Group) => void) {
ensureIsConfigured('Cannot add test group without configuring the test runner')
/**
* Disallow nested groups
*/
if (activeGroup) {
throw new Error('Cannot create nested test groups')
}
activeGroup = new Group(title, emitter, runnerOptions.refiner)
/**
* Set filename
*/
activeGroup.options.meta.fileName = recentlyImportedFile
/**
* Add group to the default suite
*/
activeSuite.add(activeGroup)
callback(activeGroup)
activeGroup = undefined
}
/**
* Collect files using the files collector function or by processing
* the glob pattern
*/
async function collectFiles(files: string | string[] | (() => string[] | Promise<string[]>)) {
if (Array.isArray(files) || typeof files === 'string') {
return await fastGlob(files, { absolute: true, onlyFiles: true, cwd: runnerOptions.cwd })
} else if (typeof files === 'function') {
return await files()
}
throw new Error('Invalid value for "files" property. Expected a string, array or a function')
}
/**
* Import test files using the configured importer. Also
* filter files using the file filter. (if mentioned).
*/
async function importFiles(files: string[]) {
for (let file of files) {
recentlyImportedFile = file
if (runnerOptions.filters.files && runnerOptions.filters.files.length) {
if (isFileAllowed(file, runnerOptions.filters.files)) {
await runnerOptions.importer(file)
}
} else {
await runnerOptions.importer(file)
}
}
}
/**
* Run japa tests
*/
export async function run() {
const runner = new Runner(emitter)
runner.manageUnHandledExceptions()
runner.onSuite(runnerOptions.configureSuite)
const hooks = new Hooks()
let setupRunner: ReturnType<Hooks['runner']>
let teardownRunner: ReturnType<Hooks['runner']>
try {
ensureIsConfigured('Cannot run tests without configuring the tests runner')
/**
* Step 1: Run all plugins
*
* Plugins can also mutate config. So we process the config after
* running plugins only
*/
for (let plugin of runnerOptions.plugins) {
await plugin(runnerOptions, runner, { Test, TestContext, Group })
}
validateSuitesFilter()
/**
* Step 2: Notify runner about reporters
*/
runnerOptions.reporters.forEach((reporter) => runner.registerReporter(reporter))
/**
* Step 3: Configure runner hooks.
*/
runnerOptions.setup.forEach((hook) => hooks.add('setup', hook))
runnerOptions.teardown.forEach((hook) => hooks.add('teardown', hook))
setupRunner = hooks.runner('setup')
teardownRunner = hooks.runner('teardown')
/**
* Step 3.1: Run setup hooks
*
* We run the setup hooks before importing test files. It
* allows hooks to setup the app environment for the
* test files.
*/
await setupRunner.run(runner)
/**
* Step 4: Entertain files property and import test files
* as part of the default suite
*/
if ('files' in runnerOptions && runnerOptions.files.length) {
globalTimeout = runnerOptions.timeout
const files = await collectFiles(runnerOptions.files)
runner.add(activeSuite)
await importFiles(files)
}
/**
* Step 5: Entertain suites property and import test files
* for the filtered suites.
*/
if ('suites' in runnerOptions) {
for (let suite of runnerOptions.suites) {
if (isSuiteAllowed(suite.name, runnerOptions.filters.suites)) {
if (suite.timeout !== undefined) {
globalTimeout = suite.timeout
} else {
globalTimeout = runnerOptions.timeout
}
activeSuite = new Suite(suite.name, emitter)
if (typeof suite.configure === 'function') {
suite.configure(activeSuite)
}
const files = await collectFiles(suite.files)
runner.add(activeSuite)
await importFiles(files)
}
}
}
/**
* Step 6: Add filters to the refiner
*/
Object.keys(runnerOptions.filters).forEach((layer: 'tests' | 'groups' | 'tags') => {
if (refinerFilteringLayers.includes(layer)) {
const values = runnerOptions.filters[layer]
if (values) {
runnerOptions.refiner.add(layer, values)
}
}
})
/**
* Step 7.1: Start the tests runner
*/
await runner.start()
/**
* Step 7.2: Execute all the tests
*/
await runner.exec()
/**
* Step 7.3: Run cleanup and teardown hooks
*/
await setupRunner.cleanup(runner)
await teardownRunner.run(runner)
await teardownRunner.cleanup(runner)
/**
* Step 7.4: End or wait for process to exit
*/
await endTests(runner)
/**
* Step 8: Update the process exit code
*/
const summary = runner.getSummary()
if (summary.hasError) {
process.exitCode = 1
}
runnerOptions.forceExit && process.exit()
} catch (error) {
if (setupRunner! && setupRunner.isCleanupPending) {
await setupRunner.cleanup(error, runner)
}
if (teardownRunner! && teardownRunner.isCleanupPending) {
await teardownRunner.cleanup(error, runner)
}
const printer = new ErrorsPrinter()
await printer.printError(error)
process.exitCode = 1
runnerOptions.forceExit && process.exit()
}
}
/**
* Process CLI arguments into configuration options. The following
* command line arguments are processed.
*
* * --tests=Specify test titles
* * --tags=Specify test tags
* * --groups=Specify group titles
* * --ignore-tags=Specify negated tags
* * --files=Specify files to match and run
* * --force-exit=Enable/disable force exit
* * --timeout=Define timeout for all the tests
*/
export function processCliArgs(argv: string[]): Partial<Config> {
const parsed = getopts(argv, {
string: ['tests', 'tags', 'groups', 'ignoreTags', 'files', 'timeout'],
boolean: ['forceExit'],
alias: {
ignoreTags: 'ignore-tags',
forceExit: 'force-exit',
},
})
const config: { filters: Filters; timeout?: number; forceExit?: boolean } = {
filters: {},
}
processAsString(parsed, 'tags', (tags) => (config.filters.tags = tags))
processAsString(parsed, 'ignoreTags', (tags) => {
config.filters.tags = config.filters.tags || []
tags.forEach((tag) => config.filters.tags!.push(`!${tag}`))
})
processAsString(parsed, 'groups', (groups) => (config.filters.groups = groups))
processAsString(parsed, 'tests', (tests) => (config.filters.tests = tests))
processAsString(parsed, 'files', (files) => (config.filters.files = files))
/**
* Get suites
*/
if (parsed._.length) {
processAsString({ suites: parsed._ }, 'suites', (suites) => (config.filters.suites = suites))
}
/**
* Get timeout
*/
if (parsed.timeout) {
const value = Number(parsed.timeout)
if (!isNaN(value)) {
config.timeout = value
}
}
/**
* Get forceExit
*/
if (parsed.forceExit) {
config.forceExit = true
}
return config
}