run-web-snapshots.ts 1.6 KB

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