import { spawn } from "node:child_process"; import { readdir } from "node:fs/promises"; import { join } from "node:path"; async function testFiles(directory: string): Promise { const entries = await readdir(directory, { withFileTypes: true }); const files = await Promise.all( entries.map(async (entry) => { const path = join(directory, entry.name); if (entry.isDirectory()) { if ( ["node_modules", "dist", ".turbo"].includes(entry.name) || path === "apps/server/src/testing/http" ) { return []; } return await testFiles(path); } return entry.name.endsWith(".test.ts") && !entry.name.endsWith(".integration.test.ts") ? [path] : []; }) ); return files.flat(); } async function main(): Promise { const files = ( await Promise.all(["apps", "modules", "packages", "tests/e2e/support"].map(testFiles)) ) .flat() .sort(); if (process.argv.includes("--list")) { console.info(files.join("\n")); return; } const child = spawn( process.execPath, [ "--experimental-test-module-mocks", "--import", "tsx", "--test", "--test-concurrency=4", ...files, ], { stdio: "inherit" } ); const stop = () => { child.kill("SIGTERM"); }; process.once("SIGINT", stop); process.once("SIGTERM", stop); child.once("error", () => { console.error("Unable to start unit tests."); process.exitCode = 1; }); child.once("close", (code) => { process.removeListener("SIGINT", stop); process.removeListener("SIGTERM", stop); process.exitCode = code ?? 1; }); } main().catch(() => { console.error("Unable to discover unit tests."); process.exitCode = 1; });