run-web-snapshots.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /** Run serial browser owners before one bounded snapshot pool. */
  2. import { spawn } from 'node:child_process'
  3. import { pnpmInvocation } from './pnpm-invocation.ts'
  4. const serialFiles = [
  5. 'apps/web/tests/hmr-live.e2e.ts',
  6. 'apps/web/tests/cordis-tool-round.e2e.ts',
  7. ]
  8. const workerRaw = process.env.DSH_WEB_SNAPSHOT_WORKERS
  9. const workers = Number.parseInt(workerRaw ?? '', 10)
  10. if (!Number.isSafeInteger(workers) || workers < 2 || String(workers) !== workerRaw) {
  11. throw new Error(`DSH_WEB_SNAPSHOT_WORKERS must be an integer greater than 1, got ${JSON.stringify(workerRaw)}.`)
  12. }
  13. const invocation = pnpmInvocation(['exec', 'vitest', 'run', '--config', 'vitest.web.config.ts'])
  14. let serialStatus = 0
  15. for (const file of serialFiles) {
  16. serialStatus = await run(invocation.command, [...invocation.args, file])
  17. if (serialStatus !== 0) break
  18. }
  19. if (serialStatus === 0) {
  20. process.exitCode = await run(invocation.command, [
  21. ...invocation.args,
  22. ...serialFiles.map(file => `--exclude=${file}`),
  23. '--fileParallelism',
  24. `--maxWorkers=${String(workers)}`,
  25. ])
  26. } else {
  27. process.exitCode = serialStatus
  28. }
  29. function run(command: string, args: string[]): Promise<number> {
  30. return new Promise((resolveRun, reject) => {
  31. const child = spawn(command, args, { stdio: 'inherit' })
  32. child.once('error', reject)
  33. child.once('exit', (exitCode, signalCode) => {
  34. if (signalCode !== null) {
  35. console.error(`web snapshots terminated by ${signalCode}`)
  36. resolveRun(1)
  37. return
  38. }
  39. resolveRun(exitCode ?? 1)
  40. })
  41. })
  42. }