vitest.snapshot.config.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { availableParallelism } from 'node:os'
  2. import tsconfigPaths from 'vite-tsconfig-paths'
  3. import { defineConfig } from 'vitest/config'
  4. import { vitestExecArgv } from './vitest.shared.ts'
  5. const DEFAULT_SNAPSHOT_MAX_CONCURRENCY = 5
  6. function positiveIntFromEnv(name: string, fallback: number): number {
  7. const raw = process.env[name]
  8. if (raw === undefined || raw === '') return fallback
  9. const value = Number(raw)
  10. if (!Number.isInteger(value) || value < 1) {
  11. throw new Error(`${name} must be a positive integer, got ${JSON.stringify(raw)}`)
  12. }
  13. return value
  14. }
  15. const snapshotMaxConcurrency = positiveIntFromEnv(
  16. 'DSH_SNAPSHOT_MAX_CONCURRENCY',
  17. Math.min(DEFAULT_SNAPSHOT_MAX_CONCURRENCY, availableParallelism()),
  18. )
  19. // Replay is the keyless default: boot real subprocess paths from recorded model responses and diff
  20. // assembled requests, normalized protocol or transcript output, and persisted-log expected outputs.
  21. // `record` calls the real API and updates fixtures and expected outputs; `refresh` replays committed scripts
  22. // and updates current expected outputs. Replay/refresh never load `.env`; only record reads a key from the
  23. // environment or root `.env`.
  24. if (process.env.DSH_SNAPSHOT === 'record') {
  25. try {
  26. process.loadEnvFile(new URL('.env', import.meta.url).pathname)
  27. } catch (error) {
  28. // ENOENT (no .env) is fine — the key may already be in the environment.
  29. // Surface any other failure rather than silently recording with wrong env.
  30. if ((error as NodeJS.ErrnoException | null)?.code !== 'ENOENT') throw error
  31. }
  32. }
  33. export default defineConfig({
  34. // Same resolution note as vitest.config.ts: bare workspace names resolve
  35. // through the tsconfig.base.json paths facade; the native option cannot do
  36. // this (the root tsconfig is a solution file with no paths).
  37. plugins: [tsconfigPaths({ projects: ['./tsconfig.base.json'] })],
  38. test: {
  39. execArgv: vitestExecArgv,
  40. setupFiles: ['./scripts/test-invariants.ts'],
  41. include: [
  42. 'scripts/**/*.snapshot.ts',
  43. // The assembled Web snapshot executes generated client bundles; source
  44. // mode remains the zero-build path, while lib mode requires a prior build.
  45. ...(process.env.DSH_EXAMPLE_MODE === 'lib' ? ['apps/web/tests/**/*.snapshot.ts'] : []),
  46. 'examples/*/tests/**/*.snapshot.ts',
  47. 'packages/sdk/*/tests/**/*.snapshot.ts',
  48. ],
  49. // Replay never writes committed outputs and every scenario owns its
  50. // mutable runtime state (the subprocess suites use a unique temp dir and
  51. // fixture set per scenario), so replay runs the snapshot files in
  52. // parallel and bounds in-file concurrency with the environment knob
  53. // (value 1 restores fully serial replay on constrained machines). Record
  54. // and refresh stay serial: record spends real API quota per scenario, and
  55. // refresh write-back harvests volatile values from fixtures already on
  56. // disk, so concurrent writers would corrupt goldens.
  57. testTimeout: 120_000,
  58. hookTimeout: 30_000,
  59. fileParallelism: (process.env.DSH_SNAPSHOT || 'replay') === 'replay' && snapshotMaxConcurrency > 1,
  60. maxConcurrency: snapshotMaxConcurrency,
  61. },
  62. })