import { readdir } from 'node:fs/promises'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { spawn } from 'node:child_process'; export async function findUnitTests(root) { const files = []; async function visit(directory) { for (const entry of await readdir(directory, { withFileTypes: true })) { const filename = path.join(directory, entry.name); if (entry.isDirectory()) await visit(filename); else if (entry.isFile() && /\.test\.[cm]?[jt]sx?$/.test(entry.name)) files.push(filename); } } // Reports contain local browser capture tools and historical snapshots, not unit suites. for (const directory of ['src', 'tests', 'test/unit']) await visit(path.join(root, directory)); return files.sort(); } if ( process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url) ) { const root = fileURLToPath(new URL('../', import.meta.url)); const files = await findUnitTests(root); if (!files.length) throw new Error('No unit tests found in src/, tests/, or test/unit/.'); const child = spawn( process.execPath, ['--test', ...process.argv.slice(2), ...files], { cwd: root, stdio: 'inherit', }, ); child.on('error', (error) => { console.error(error); process.exitCode = 1; }); child.on('exit', (code) => { process.exitCode = code ?? 1; }); }