68 lines
1.8 KiB
JavaScript
68 lines
1.8 KiB
JavaScript
// __tests__/run-tests.cjs
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const { spawnSync } = require("node:child_process");
|
|
const dotenv = require("dotenv");
|
|
|
|
function walk(dir) {
|
|
const out = [];
|
|
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
const full = path.join(dir, entry.name);
|
|
if (entry.isDirectory()) out.push(...walk(full));
|
|
else if (entry.isFile() && entry.name.endsWith(".test.ts")) out.push(full);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
const cwd = process.cwd();
|
|
dotenv.config({ path: path.join(cwd, ".env") });
|
|
const testsDir = path.join(cwd, "__tests__");
|
|
const testFiles = fs.existsSync(testsDir) ? walk(testsDir) : [];
|
|
|
|
const inferredAllowedDbNames = (() => {
|
|
if (process.env.ALLOWED_DB_NAMES) return process.env.ALLOWED_DB_NAMES;
|
|
if (!process.env.DATABASE_URL) return undefined;
|
|
try {
|
|
const url = new URL(process.env.DATABASE_URL);
|
|
const name = url.pathname.replace(/^\/+/, "");
|
|
return name ? decodeURIComponent(name) : undefined;
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
})();
|
|
|
|
if (testFiles.length === 0) {
|
|
console.error("No .test.ts files found under __tests__/");
|
|
process.exit(1);
|
|
}
|
|
|
|
// Run Node's test runner, with tsx registered so TS can execute.
|
|
// Node supports registering tsx via --import=tsx. :contentReference[oaicite:2]{index=2}
|
|
const nodeArgs = [
|
|
"--require",
|
|
"./__tests__/server-only.cjs",
|
|
"--import",
|
|
"tsx",
|
|
"--test-concurrency=1",
|
|
"--test",
|
|
// Optional nicer output:
|
|
// "--test-reporter=spec",
|
|
...testFiles,
|
|
];
|
|
|
|
const env = {
|
|
...process.env,
|
|
NODE_ENV: "test",
|
|
NODE_PATH: path.join(cwd, "__tests__", "node_modules"),
|
|
...(inferredAllowedDbNames ? { ALLOWED_DB_NAMES: inferredAllowedDbNames } : {}),
|
|
};
|
|
|
|
const r = spawnSync(process.execPath, nodeArgs, {
|
|
stdio: "inherit",
|
|
cwd,
|
|
env,
|
|
shell: false,
|
|
});
|
|
|
|
process.exit(r.status ?? 1);
|