run-node-tests.mjs
1.36 KB
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
import { readdir, stat } from 'node:fs/promises'
import path from 'node:path'
import { spawn } from 'node:child_process'
async function collectTests(entryPath) {
const absolutePath = path.resolve(entryPath)
const entry = await stat(absolutePath)
if (entry.isFile()) {
return absolutePath.endsWith('.test.mjs') ? [absolutePath] : []
}
const results = []
for (const child of await readdir(absolutePath, { withFileTypes: true })) {
const childPath = path.join(absolutePath, child.name)
if (child.isDirectory()) {
results.push(...(await collectTests(childPath)))
} else if (child.isFile() && child.name.endsWith('.test.mjs')) {
results.push(childPath)
}
}
return results
}
const roots = process.argv.slice(2)
if (!roots.length) {
throw new Error('Provide at least one test directory or .test.mjs file')
}
const testFiles = (
await Promise.all(roots.map(entryPath => collectTests(entryPath)))
)
.flat()
.sort()
if (!testFiles.length) {
throw new Error(`No .test.mjs files found under: ${roots.join(', ')}`)
}
const child = spawn(
process.execPath,
['--experimental-detect-module', '--test', ...testFiles],
{
stdio: 'inherit',
shell: false
}
)
child.on('error', error => {
throw error
})
const exitCode = await new Promise(resolve => {
child.on('exit', code => resolve(code ?? 1))
})
process.exitCode = exitCode