-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
94 lines (86 loc) · 2.99 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
import { spawn } from 'child_process'
import { createRequire } from 'module'
import { pathToFileURL } from 'url'
import { resolveTestPaths } from './resolution.js'
const DEFAULT_TEST_EXTENSIONS = ['.js', '.mjs', '.cjs', '.ts', '.mts', '.cts']
export function getTestExtensions (): string[] {
if (typeof process.env.TEST_EXTENSIONS !== 'undefined' && process.env.TEST_EXTENSIONS !== null) {
return process.env.TEST_EXTENSIONS.split(',').map(e => e.trim())
}
return DEFAULT_TEST_EXTENSIONS
}
export interface Options {
'test-name-pattern'?: string[]
'test-reporter'?: string[]
'test-reporter-destination'?: string[]
'test-only'?: boolean
watch?: boolean
'watch-preserve-output'?: boolean
'experimental-test-coverage'?: boolean
}
export async function main (paths: string[], options: Options): Promise<void> {
const require = createRequire(import.meta.url)
const esmLoader = pathToFileURL(require.resolve('ts-node/esm')).toString()
const extensions = getTestExtensions()
const resolvedPaths = await resolveTestPaths(paths, extensions)
if (resolvedPaths.length === 0) {
throw new Error('no test files found')
}
spawnChild(esmLoader, resolvedPaths, options)
}
/**
* Execute the Node.js test runner with the given loader and set of test file paths.
*
* @param loader The loader to use (fully resolved path to the loader script).
* @param resolvedTestPaths The files under test.
* @param options Additional options.
*/
function spawnChild (loader: string, resolvedTestPaths: string[], options: Options): void {
const args = ['--loader', loader, '--test', ...getOptionFlags(options), ...resolvedTestPaths]
const child = spawn(process.execPath, args, {
stdio: 'inherit',
argv0: process.argv0
})
child.on('error', (error) => {
console.error(error)
process.exit(1)
})
child.on('exit', (code) => {
child.removeAllListeners()
process.off('SIGINT', sendSignalToChild)
process.off('SIGTERM', sendSignalToChild)
process.exitCode = code === null ? 1 : code
})
process.on('SIGINT', sendSignalToChild)
process.on('SIGTERM', sendSignalToChild)
function sendSignalToChild (signal: string): void {
if (child.pid != null) {
process.kill(child.pid, signal)
}
}
}
function getOptionFlags (options: Options): string[] {
const flags: string[] = []
for (const pattern of options['test-name-pattern'] ?? []) {
flags.push('--test-name-pattern', pattern)
}
for (const reporter of options['test-reporter'] ?? []) {
flags.push('--test-reporter', reporter)
}
for (const destination of options['test-reporter-destination'] ?? []) {
flags.push('--test-reporter-destination', destination)
}
if (options['test-only'] === true) {
flags.push('--test-only')
}
if (options.watch === true) {
flags.push('--watch')
}
if (options['watch-preserve-output'] === true) {
flags.push('--watch-preserve-output')
}
if (options['experimental-test-coverage'] === true) {
flags.push('--experimental-test-coverage')
}
return flags
}