vitest.snapshot.config.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { availableParallelism } from 'node:os'
  2. import tsconfigPaths from 'vite-tsconfig-paths'
  3. import { defineConfig } from 'vitest/config'
  4. import { standardDecoratorPlugin, 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'] }), standardDecoratorPlugin()],
  38. test: {
  39. execArgv: vitestExecArgv,
  40. setupFiles: ['./scripts/test-proxy-environment.ts', './scripts/test-invariants.ts'],
  41. include: [
  42. 'scripts/session-snapshot-corpus.corpus.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. 'snapshots/**/*.snapshot.ts',
  47. ],
  48. // Replay never writes committed outputs and every scenario owns its
  49. // mutable runtime state (the subprocess suites use a unique temp dir and
  50. // fixture set per scenario), so replay runs the snapshot files in
  51. // parallel and bounds in-file concurrency with the environment knob
  52. // (value 1 restores fully serial replay on constrained machines). Record
  53. // and refresh stay serial: record spends real API quota per scenario, and
  54. // refresh write-back harvests volatile values from fixtures already on
  55. // disk, so concurrent writers would corrupt expected outputs.
  56. testTimeout: 120_000,
  57. hookTimeout: 30_000,
  58. fileParallelism: (process.env.DSH_SNAPSHOT || 'replay') === 'replay' && snapshotMaxConcurrency > 1,
  59. maxConcurrency: snapshotMaxConcurrency,
  60. },
  61. })