Repository navigation
Expand file tree
/
Copy pathunit-tests.mjs
More file actions
56 lines (51 loc) 路 1.78 KB
/
Copy pathunit-tests.mjs
File metadata and controls
56 lines (51 loc) 路 1.78 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
// Runs the unit tests (src/**/*.test.ts) with node:test.
//
// The sources import through the tsconfig path aliases (main/..., common/...)
// without file extensions, which Node can't resolve on its own. Each test
// file is bundled with the same esbuild config as the main process first,
// then the bundles run under `node --test`.
import * as esbuild from "esbuild";
import { spawnSync } from "child_process";
import { readdirSync, rmSync } from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { mainConfig } from "./esbuild.config.mjs";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const outdir = path.join(__dirname, ".cache", "unit-tests");
function findFiles(dir, pattern) {
const found = [];
for (const entry of readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
found.push(...findFiles(full, pattern));
} else if (pattern.test(entry.name)) {
found.push(full);
}
}
return found;
}
const tests = findFiles(path.join(__dirname, "src"), /\.test\.tsx?$/);
if (tests.length === 0) {
console.error("no unit tests found under src/");
process.exit(1);
}
rmSync(outdir, { recursive: true, force: true });
await esbuild.build({
...mainConfig,
entryPoints: tests,
outdir,
outbase: path.join(__dirname, "src"),
outExtension: { ".js": ".cjs" },
sourcemap: "inline",
minify: false,
metafile: false,
// tests run under plain node: node:test and other builtins stay external
external: [...mainConfig.external, "node:*"],
});
const bundles = findFiles(outdir, /\.cjs$/);
const result = spawnSync(
process.execPath,
["--test", "--enable-source-maps", ...process.argv.slice(2), ...bundles],
{ stdio: "inherit" }
);
process.exit(result.status ?? 1);