Repository navigation
Expand file tree
/
Copy pathrunner.mjs
More file actions
56 lines (47 loc) · 1.58 KB
/
Copy pathrunner.mjs
File metadata and controls
56 lines (47 loc) · 1.58 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
// Cross-version test runner entry point.
//
// Node's built-in test runner only gained glob-pattern support in Node 21,
// so `node --test 'tests/**/*.spec.ts'` fails on Node 20. This script performs
// the file discovery itself and passes explicit paths to `node --test`, which
// works on every supported Node version and every OS.
//
// Usage: node tests/runner.mjs <spec|integration> [subdir]
import { spawnSync } from "node:child_process";
import { readdirSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
const testsDir = dirname(fileURLToPath(import.meta.url));
const suffix = process.argv[2] + ".ts";
const subdir = process.argv[3];
const searchDir = subdir ? join(testsDir, subdir) : testsDir;
/**
* Recursively collect test files with the given suffix.
* @param {string} dir Directory to search.
* @returns {string[]} Matching file paths.
*/
function findTestFiles(dir) {
const found = [];
for (const entry of readdirSync(dir, { withFileTypes: true })) {
const fullPath = join(dir, entry.name);
if (entry.isDirectory()) {
found.push(...findTestFiles(fullPath));
} else if (entry.name.endsWith(suffix)) {
found.push(fullPath);
}
}
return found;
}
const files = findTestFiles(searchDir);
if (files.length === 0) {
console.error(`No '*${suffix}' test files found in ${searchDir}.`);
process.exit(1);
}
const result = spawnSync(
process.execPath,
["--import", "tsx", "--test", ...files],
{ stdio: "inherit" }
);
if (result.error) {
throw result.error;
}
process.exit(result.status ?? 1);