vitest.snapshot.config.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { availableParallelism } from 'node:os'
  2. import tsconfigPaths from 'vite-tsconfig-paths'
  3. import { defineConfig } from 'vitest/config'
  4. const DEFAULT_SNAPSHOT_MAX_CONCURRENCY = 5
  5. function positiveIntFromEnv(name: string, fallback: number): number {
  6. const raw = process.env[name]
  7. if (raw === undefined || raw === '') return fallback
  8. const value = Number(raw)
  9. if (!Number.isInteger(value) || value < 1) {
  10. throw new Error(`${name} must be a positive integer, got ${JSON.stringify(raw)}`)
  11. }
  12. return value
  13. }
  14. const snapshotMaxConcurrency = positiveIntFromEnv(
  15. 'DSH_SNAPSHOT_MAX_CONCURRENCY',
  16. Math.min(DEFAULT_SNAPSHOT_MAX_CONCURRENCY, availableParallelism()),
  17. )
  18. // Replay is the keyless default: boot real example subprocesses from recorded model scripts and diff
  19. // normalized protocol or transcript output plus persisted-log expected outputs. `record` calls the real API
  20. // and updates fixtures and expected outputs; `refresh` replays committed scripts and updates current expected outputs.
  21. // Replay/refresh never load `.env`; only record reads a key from the environment or root `.env`.
  22. if (process.env.DSH_SNAPSHOT === 'record') {
  23. try {
  24. process.loadEnvFile(new URL('.env', import.meta.url).pathname)
  25. } catch (error) {
  26. // ENOENT (no .env) is fine — the key may already be in the environment.
  27. // Surface any other failure rather than silently recording with wrong env.
  28. if ((error as NodeJS.ErrnoException | null)?.code !== 'ENOENT') throw error
  29. }
  30. }
  31. export default defineConfig({
  32. // Same resolution note as vitest.config.ts: bare workspace names resolve
  33. // through the root tsconfig paths map; the native option cannot do this.
  34. plugins: [tsconfigPaths({ projects: ['./tsconfig.json'] })],
  35. test: {
  36. include: [
  37. 'examples/*/tests/**/*.snapshot.ts',
  38. 'packages/sdk/*/tests/**/*.snapshot.ts',
  39. 'packages/ui/tui/tests/**/*.snapshot.ts',
  40. ],
  41. // Each test boots a subprocess; give it room and keep the worker file singular. Replay tests
  42. // opt into bounded in-file concurrency, while record/refresh stay serial because they write
  43. // fixtures. The environment knob restores serial replay with value 1 on constrained machines.
  44. testTimeout: 120_000,
  45. hookTimeout: 30_000,
  46. fileParallelism: false,
  47. maxConcurrency: snapshotMaxConcurrency,
  48. },
  49. })