vitest.config.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import tsconfigPaths from 'vite-tsconfig-paths'
  2. import { defineConfig } from 'vitest/config'
  3. import { vitestExecArgv } from './vitest.shared.ts'
  4. import { COVERAGE_EXEMPT_ENV, coverageExemptHeavySuites } from './scripts/coverage-exempt.ts'
  5. // Resolution facade shared by every plugin instance below: tsconfig.base.json
  6. // has no include, which vite-tsconfig-paths treats as match-all, so its paths
  7. // map applies to every test file. paths must win over package exports so built
  8. // lib/ never loads a second module-singleton copy.
  9. const pathsPlugin = (): ReturnType<typeof tsconfigPaths> => tsconfigPaths({ projects: ['./tsconfig.base.json'] })
  10. const windowsUnsupportedPackages = process.platform === 'win32'
  11. ? [
  12. 'packages/bash/*',
  13. 'packages/hooks/*',
  14. 'packages/subprocess/*',
  15. 'packages/pty/pty-local',
  16. 'packages/sandbox/sandbox-local',
  17. 'packages/sdk/create-sdk',
  18. 'packages/sdk/helper',
  19. ]
  20. : []
  21. // These files retain 100% per-file coverage on POSIX, where their process-pipe and terminal timing
  22. // tests are deterministic; Windows skips those cases and must not fail solely on their uncovered paths.
  23. const windowsCoverageExclusions = process.platform === 'win32'
  24. ? [
  25. 'packages/lsp/lsp-local/src/connection.ts',
  26. 'packages/lsp/lsp-local/src/index.ts',
  27. 'packages/lsp/lsp-local/src/instance.ts',
  28. 'packages/ui/tui/src/index.ts',
  29. ]
  30. : []
  31. const testIncludes = [
  32. 'packages/*/*/tests/**/*.spec.{ts,tsx}',
  33. 'apps/*/tests/**/*.spec.ts',
  34. 'examples/*/tests/**/*.spec.ts',
  35. 'scripts/**/*.spec.ts',
  36. ]
  37. // The instrumented coverage gate sets this env; the exempt heavy suites then
  38. // run beside it uninstrumented (membership contract in scripts/coverage-exempt.ts).
  39. // A set-but-not-'1' value is a misconfiguration, not a silent no-op.
  40. const coverageExemptRaw = process.env[COVERAGE_EXEMPT_ENV]
  41. if (coverageExemptRaw !== undefined && coverageExemptRaw !== '' && coverageExemptRaw !== '1') {
  42. throw new Error(`vitest config: ${COVERAGE_EXEMPT_ENV} must be '1' or unset, got ${JSON.stringify(coverageExemptRaw)}.`)
  43. }
  44. const coverageExemptExcludes = coverageExemptRaw === '1'
  45. ? coverageExemptHeavySuites.map(suite => suite.exclude)
  46. : []
  47. // These suites exercise process-global state, process APIs, or timing-sensitive process I/O
  48. // that worker threads cannot isolate reliably under aggregate gate contention.
  49. // Keep the narrow exception in forks while the rest of the inventory avoids per-file processes.
  50. const processBoundTests = [
  51. 'packages/subprocess/subprocess-local/tests/spawn.spec.ts',
  52. 'packages/context/time-context/tests/time-context.spec.ts',
  53. 'packages/llm/llm-pi-ai/tests/adapter.spec.ts',
  54. 'packages/ui/app-boot/tests/app-boot.spec.ts',
  55. 'packages/workflow/workflow-workerthread/tests/session.spec.ts',
  56. ]
  57. export default defineConfig({
  58. plugins: [pathsPlugin()],
  59. test: {
  60. setupFiles: ['./scripts/test-invariants.ts'],
  61. // .tsx: client component specs (jsdom via per-file @vitest-environment pragma).
  62. include: testIncludes,
  63. exclude: windowsUnsupportedPackages.map(path => `${path}/tests/**/*.spec.ts`),
  64. // One coverage invocation aggregates both projects. Regular suites fork on
  65. // POSIX for Node stability and use threads on Windows; process-bound suites
  66. // always fork.
  67. projects: [
  68. {
  69. plugins: [pathsPlugin()],
  70. test: {
  71. name: 'thread-safe',
  72. execArgv: vitestExecArgv,
  73. // Node 24 has aborted in its CJS lexer (v8::ToLocalChecked Empty
  74. // MaybeLocal in cjs_lexer::Parse) from worker threads on macOS
  75. // arm64 and later on Linux. A fork contains that external runtime
  76. // failure to the test process; Windows keeps the thread pool, where
  77. // the abort has not reproduced and process spawn is costlier.
  78. pool: process.platform === 'win32' ? 'threads' : 'forks',
  79. setupFiles: ['./scripts/test-invariants.ts'],
  80. include: testIncludes,
  81. exclude: [
  82. ...windowsUnsupportedPackages.map(path => `${path}/tests/**/*.spec.ts`),
  83. ...processBoundTests,
  84. ...coverageExemptExcludes,
  85. ],
  86. },
  87. },
  88. {
  89. plugins: [pathsPlugin()],
  90. test: {
  91. name: 'process-bound',
  92. execArgv: vitestExecArgv,
  93. pool: 'forks',
  94. setupFiles: ['./scripts/test-invariants.ts'],
  95. include: processBoundTests,
  96. exclude: [
  97. ...windowsUnsupportedPackages.map(path => `${path}/tests/**/*.spec.ts`),
  98. ...coverageExemptExcludes,
  99. ],
  100. },
  101. },
  102. ],
  103. coverage: {
  104. provider: 'v8',
  105. // Coverage measures OUR runtime source. Types-only files carry no
  106. // executable code; vendor/ and examples/ are out of scope (examples are
  107. // exercised by the demo smoke test instead).
  108. // .tsx: client components are gated like everything else (jsdom lane).
  109. include: ['packages/*/*/src/**/*.{ts,tsx}'],
  110. // Types-only files have no runtime coverage. Importing self-executing bins/workers would boot
  111. // them inside the unit process, so real subprocess/Worker tests cover their thin entry glue.
  112. exclude: [
  113. 'packages/*/*/src/types.ts',
  114. 'packages/*/*/src/bin.ts',
  115. 'packages/*/*/src/worker.ts',
  116. // A killed executable lint-contract test can leave a non-product source probe behind.
  117. 'packages/*/*/src/oxlint-contract-*.ts',
  118. // GUI step-1 skeleton (PR #500): client/web UI files whose remaining
  119. // branches need a browser-grade harness the jsdom lane doesn't cover
  120. // yet. TODO(gui): cover and remove as the client test lane matures.
  121. 'packages/client/ui-trajectory/src/*',
  122. // Trajectory's compact Markdown projection retains deferred branch coverage.
  123. 'packages/client/ui-primitives/src/markdown/plain-text.ts',
  124. 'packages/client/ui-question/src/client/QuestionComposer.tsx',
  125. 'packages/client/ui-primitives/src/Menu.tsx',
  126. 'packages/client/ui-primitives/src/RiskConfirmation.tsx',
  127. 'packages/client/ui-workspace/src/client/WorkspaceBrowser.tsx',
  128. 'packages/client/ui-workspace/src/client/WorkspacePicker.tsx',
  129. 'packages/client/web-react/src/*',
  130. 'packages/client/runtime/src/*',
  131. 'packages/client/ui-conversation/src/*',
  132. 'packages/client/ui-slots/src/*',
  133. 'packages/client/ui-layout/src/*',
  134. 'packages/client/web/src/*',
  135. 'packages/host/webserver/src/*',
  136. 'packages/client/modules/src/client/system.ts',
  137. 'packages/client/hmr/src/client/index.ts',
  138. // Web config-tree boot round: the new host-side web-transport halves
  139. // whose remaining branches need real-composition/process harnesses.
  140. // TODO(gui): cover and remove with the client test lane above.
  141. 'packages/client/modules/src/index.ts',
  142. 'packages/client/modules/src/invariant.ts',
  143. 'packages/client/modules/src/client/index.ts',
  144. 'packages/client/modules/src/client/manifest.ts',
  145. 'packages/client/hmr/src/index.ts',
  146. 'packages/client/hmr/src/invariant.ts',
  147. 'packages/client/connection/src/index.ts',
  148. 'packages/client/connection/src/http-bridge.ts',
  149. // Slash/command/input round: per-file gaps deferred with the same
  150. // client-lane debt. TODO(gui): cover and remove with the lane above.
  151. 'packages/client/connection/src/client/fixture.ts',
  152. 'packages/client/ui-command/src/index.ts',
  153. 'packages/client/ui-skill/src/index.ts',
  154. 'packages/client/ui-slash/src/index.ts',
  155. 'packages/client/ui-subagent/src/index.ts',
  156. 'packages/client/ui-command/src/client/popup.ts',
  157. 'packages/client/ui-command/src/client/directory.ts',
  158. 'packages/client/ui-command/src/client/service.ts',
  159. 'packages/client/ui-command/src/client/PopupSelectView.tsx',
  160. 'packages/client/ui-model/src/index.ts',
  161. 'packages/client/ui-permission/src/index.ts',
  162. 'packages/client/ui-model/src/client/ModelSelect.tsx',
  163. 'packages/client/ui-model/src/client/directory.ts',
  164. 'packages/client/ui-model/src/client/index.ts',
  165. 'packages/client/ui-model/src/client/service.ts',
  166. 'packages/client/ui-slash/src/client/controller.ts',
  167. 'packages/client/ui-slash/src/client/service.ts',
  168. 'packages/client/ui-slash/src/core/menu.ts',
  169. 'packages/client/ui-slash/src/core/detect.ts',
  170. 'packages/client/ui-sidebar/src/client/index.ts',
  171. 'packages/client/ui-skill/src/client/index.ts',
  172. 'packages/client/ui-workspace/src/client/index.ts',
  173. 'packages/client/test-runtime/src/translate.ts',
  174. 'packages/client/ui-primitives/src/JsonTree.tsx',
  175. // Typert generator: correctness is pinned by its fixture suites and
  176. // the byte-for-byte catalog reproduction test; per-file coverage
  177. // would put whole-workspace compiler analysis under v8
  178. // instrumentation — the coverage lane's longest tail.
  179. 'packages/typert/generator/src/*.ts',
  180. 'packages/host/apiproxy/src/index.ts',
  181. 'packages/host/apiproxy/src/invariant.ts',
  182. 'packages/host/apiproxy/src/api-proxy.ts',
  183. // Projection/command round: executor lifecycle branches and the
  184. // registry's drive tails need the same maturing lanes. TODO(gui):
  185. // cover and remove with the client test lane above.
  186. 'packages/ui/commands/src/index.ts',
  187. 'packages/ui/commands/src/invariant.ts',
  188. 'packages/session-projection/session-projection/src/index.ts',
  189. 'packages/ui/tui/src/index.ts',
  190. ...windowsUnsupportedPackages.map(path => `${path}/src/**/*.ts`),
  191. ...windowsCoverageExclusions,
  192. ],
  193. // 100% or it doesn't merge (docs/testing.md: excessive tests are welcome).
  194. // Per-file so a well-covered big file can't subsidize a bare one.
  195. // Every v8 ignore comment must carry a reason — see the quality-gates Agent Note
  196. // (.agents/notes/implemented/process/2026-06-11-quality-gates.md).
  197. thresholds: {
  198. perFile: true,
  199. statements: 100,
  200. branches: 100,
  201. functions: 100,
  202. lines: 100,
  203. },
  204. reporter: process.env.CI ? ['text'] : ['text', 'html'],
  205. },
  206. },
  207. })