vitest.snapshot.config.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 subprocess paths from recorded model responses and diff
  19. // assembled requests, normalized protocol or transcript output, and persisted-log expected outputs.
  20. // `record` calls the real API and updates fixtures and expected outputs; `refresh` replays committed scripts
  21. // and updates current expected outputs. Replay/refresh never load `.env`; only record reads a key from the
  22. // environment or root `.env`.
  23. if (process.env.DSH_SNAPSHOT === 'record') {
  24. try {
  25. process.loadEnvFile(new URL('.env', import.meta.url).pathname)
  26. } catch (error) {
  27. // ENOENT (no .env) is fine — the key may already be in the environment.
  28. // Surface any other failure rather than silently recording with wrong env.
  29. if ((error as NodeJS.ErrnoException | null)?.code !== 'ENOENT') throw error
  30. }
  31. }
  32. export default defineConfig({
  33. // Same resolution note as vitest.config.ts: bare workspace names resolve
  34. // through the tsconfig.base.json paths facade; the native option cannot do
  35. // this (the root tsconfig is a solution file with no paths).
  36. plugins: [tsconfigPaths({ projects: ['./tsconfig.base.json'] })],
  37. test: {
  38. setupFiles: ['./scripts/test-invariants.ts'],
  39. include: [
  40. 'scripts/**/*.snapshot.ts',
  41. // The assembled Web snapshot executes generated client bundles; source
  42. // mode remains the zero-build path, while lib mode requires a prior build.
  43. ...(process.env.DSH_EXAMPLE_MODE === 'lib' ? ['apps/web/tests/**/*.snapshot.ts'] : []),
  44. 'examples/*/tests/**/*.snapshot.ts',
  45. 'packages/sdk/*/tests/**/*.snapshot.ts',
  46. 'packages/ui/tui/tests/**/*.snapshot.ts',
  47. ],
  48. // Each test boots a subprocess; give it room and keep the worker file singular. Replay tests
  49. // opt into bounded in-file concurrency, while record/refresh stay serial because they write
  50. // fixtures. The environment knob restores serial replay with value 1 on constrained machines.
  51. testTimeout: 120_000,
  52. hookTimeout: 30_000,
  53. fileParallelism: false,
  54. maxConcurrency: snapshotMaxConcurrency,
  55. },
  56. })