Files
lehuo/apps/api/test/run-integration.ts
2026-08-12 14:35:12 +08:00

42 lines
1.4 KiB
TypeScript

import { spawn } from 'node:child_process';
function run(command: string, args: string[], environment: NodeJS.ProcessEnv): Promise<void> {
return new Promise((resolve, reject) => {
const child = spawn(command, args, {
cwd: process.cwd(),
env: environment,
shell: false,
stdio: 'inherit',
});
child.once('error', reject);
child.once('exit', (code) => {
if (code === 0) resolve();
else reject(new Error(`${command} exited with code ${String(code)}`));
});
});
}
async function main(): Promise<void> {
const testDatabaseUrl = process.env.TEST_DATABASE_URL;
if (!testDatabaseUrl) throw new Error('TEST_DATABASE_URL is required for integration tests');
const parsed = new URL(testDatabaseUrl);
if (parsed.protocol !== 'mysql:' || parsed.pathname !== '/lehuo_test') {
throw new Error('Integration tests are restricted to the lehuo_test MySQL database');
}
const pnpmCli = process.env.npm_execpath;
if (!pnpmCli) throw new Error('npm_execpath is required to locate the active pnpm CLI');
const environment = {
...process.env,
APP_ENV: 'test',
DATABASE_URL: testDatabaseUrl,
};
await run(process.execPath, [pnpmCli, 'exec', 'prisma', 'migrate', 'deploy'], environment);
await run(
process.execPath,
[pnpmCli, 'exec', 'vitest', 'run', '--config', 'vitest.integration.config.ts'],
environment,
);
}
void main();