@vitest-agent/mcp 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (229) hide show
  1. package/README.md +1 -2
  2. package/bin/vitest-agent-mcp.js +1 -17
  3. package/index.d.ts +324 -315
  4. package/index.js +2 -4
  5. package/middleware/idempotency.js +1 -1
  6. package/package.json +2 -2
  7. package/server.js +2 -4
  8. package/tools/acceptance-metrics.js +1 -1
  9. package/tools/cache-health.js +1 -1
  10. package/tools/commit-changes.js +1 -1
  11. package/tools/configure.js +1 -1
  12. package/tools/coverage.js +1 -1
  13. package/tools/errors.js +1 -1
  14. package/tools/failure-signature-get.js +1 -1
  15. package/tools/file-coverage.js +1 -1
  16. package/tools/history.js +1 -1
  17. package/tools/inventory.js +1 -1
  18. package/tools/overview.js +1 -1
  19. package/tools/run-tests.js +15 -3
  20. package/tools/settings-list.js +1 -1
  21. package/tools/status.js +1 -1
  22. package/tools/tdd-artifact.js +1 -1
  23. package/tools/tdd-task.js +1 -1
  24. package/tools/test.js +1 -1
  25. package/tools/trends.js +1 -1
  26. package/tools/turn-search.js +1 -1
  27. package/public/patterns/_meta.json +0 -67
  28. package/public/patterns/authoring-a-custom-vitest-agent-reporter.md +0 -82
  29. package/public/patterns/known-issues-and-caveats.md +0 -52
  30. package/public/patterns/operating-vitest-agent-as-an-agent.md +0 -53
  31. package/public/patterns/running-tests-via-mcp.md +0 -58
  32. package/public/patterns/silencing-leaking-output-in-tests.md +0 -91
  33. package/public/patterns/testing-effect-schema-definitions.md +0 -71
  34. package/public/patterns/testing-effect-services-with-mock-layers.md +0 -63
  35. package/public/vendor/vitest-docs/ATTRIBUTION.md +0 -5
  36. package/public/vendor/vitest-docs/api/advanced/artifacts.md +0 -189
  37. package/public/vendor/vitest-docs/api/advanced/metadata.md +0 -68
  38. package/public/vendor/vitest-docs/api/advanced/plugin.md +0 -168
  39. package/public/vendor/vitest-docs/api/advanced/reporters.md +0 -342
  40. package/public/vendor/vitest-docs/api/advanced/runner.md +0 -334
  41. package/public/vendor/vitest-docs/api/advanced/test-case.md +0 -302
  42. package/public/vendor/vitest-docs/api/advanced/test-collection.md +0 -89
  43. package/public/vendor/vitest-docs/api/advanced/test-module.md +0 -140
  44. package/public/vendor/vitest-docs/api/advanced/test-project.md +0 -321
  45. package/public/vendor/vitest-docs/api/advanced/test-specification.md +0 -96
  46. package/public/vendor/vitest-docs/api/advanced/test-suite.md +0 -230
  47. package/public/vendor/vitest-docs/api/advanced/vitest.md +0 -684
  48. package/public/vendor/vitest-docs/api/assert-type.md +0 -22
  49. package/public/vendor/vitest-docs/api/assert.md +0 -1960
  50. package/public/vendor/vitest-docs/api/browser/assertions.md +0 -1277
  51. package/public/vendor/vitest-docs/api/browser/commands.md +0 -154
  52. package/public/vendor/vitest-docs/api/browser/context.md +0 -338
  53. package/public/vendor/vitest-docs/api/browser/interactivity.md +0 -681
  54. package/public/vendor/vitest-docs/api/browser/locators.md +0 -1171
  55. package/public/vendor/vitest-docs/api/browser/react.md +0 -346
  56. package/public/vendor/vitest-docs/api/browser/svelte.md +0 -292
  57. package/public/vendor/vitest-docs/api/browser/vue.md +0 -222
  58. package/public/vendor/vitest-docs/api/describe.md +0 -374
  59. package/public/vendor/vitest-docs/api/expect-typeof.md +0 -571
  60. package/public/vendor/vitest-docs/api/expect.md +0 -2304
  61. package/public/vendor/vitest-docs/api/hooks.md +0 -463
  62. package/public/vendor/vitest-docs/api/mock.md +0 -701
  63. package/public/vendor/vitest-docs/api/test.md +0 -926
  64. package/public/vendor/vitest-docs/api/vi.md +0 -1372
  65. package/public/vendor/vitest-docs/config/alias.md +0 -13
  66. package/public/vendor/vitest-docs/config/allowonly.md +0 -32
  67. package/public/vendor/vitest-docs/config/api.md +0 -27
  68. package/public/vendor/vitest-docs/config/attachmentsdir.md +0 -6
  69. package/public/vendor/vitest-docs/config/bail.md +0 -9
  70. package/public/vendor/vitest-docs/config/benchmark.md +0 -65
  71. package/public/vendor/vitest-docs/config/browser/api.md +0 -23
  72. package/public/vendor/vitest-docs/config/browser/commands.md +0 -6
  73. package/public/vendor/vitest-docs/config/browser/connecttimeout.md +0 -10
  74. package/public/vendor/vitest-docs/config/browser/detailspanelposition.md +0 -38
  75. package/public/vendor/vitest-docs/config/browser/enabled.md +0 -40
  76. package/public/vendor/vitest-docs/config/browser/expect.md +0 -250
  77. package/public/vendor/vitest-docs/config/browser/headless.md +0 -7
  78. package/public/vendor/vitest-docs/config/browser/instances.md +0 -47
  79. package/public/vendor/vitest-docs/config/browser/isolate.md +0 -11
  80. package/public/vendor/vitest-docs/config/browser/locators.md +0 -24
  81. package/public/vendor/vitest-docs/config/browser/orchestratorscripts.md +0 -39
  82. package/public/vendor/vitest-docs/config/browser/playwright.md +0 -214
  83. package/public/vendor/vitest-docs/config/browser/preview.md +0 -32
  84. package/public/vendor/vitest-docs/config/browser/provider.md +0 -79
  85. package/public/vendor/vitest-docs/config/browser/screenshotdirectory.md +0 -6
  86. package/public/vendor/vitest-docs/config/browser/screenshotfailures.md +0 -6
  87. package/public/vendor/vitest-docs/config/browser/testerhtmlpath.md +0 -5
  88. package/public/vendor/vitest-docs/config/browser/trace.md +0 -43
  89. package/public/vendor/vitest-docs/config/browser/trackunhandlederrors.md +0 -10
  90. package/public/vendor/vitest-docs/config/browser/ui.md +0 -7
  91. package/public/vendor/vitest-docs/config/browser/viewport.md +0 -6
  92. package/public/vendor/vitest-docs/config/browser/webdriverio.md +0 -64
  93. package/public/vendor/vitest-docs/config/cache.md +0 -26
  94. package/public/vendor/vitest-docs/config/chaiconfig.md +0 -29
  95. package/public/vendor/vitest-docs/config/clearmocks.md +0 -22
  96. package/public/vendor/vitest-docs/config/coverage.md +0 -455
  97. package/public/vendor/vitest-docs/config/css.md +0 -47
  98. package/public/vendor/vitest-docs/config/dangerouslyignoreunhandlederrors.md +0 -23
  99. package/public/vendor/vitest-docs/config/deps.md +0 -127
  100. package/public/vendor/vitest-docs/config/detectasyncleaks.md +0 -39
  101. package/public/vendor/vitest-docs/config/diff.md +0 -96
  102. package/public/vendor/vitest-docs/config/dir.md +0 -7
  103. package/public/vendor/vitest-docs/config/disableconsoleintercept.md +0 -15
  104. package/public/vendor/vitest-docs/config/env.md +0 -5
  105. package/public/vendor/vitest-docs/config/environment.md +0 -96
  106. package/public/vendor/vitest-docs/config/environmentoptions.md +0 -30
  107. package/public/vendor/vitest-docs/config/exclude.md +0 -49
  108. package/public/vendor/vitest-docs/config/execargv.md +0 -10
  109. package/public/vendor/vitest-docs/config/expandsnapshotdiff.md +0 -7
  110. package/public/vendor/vitest-docs/config/expect.md +0 -38
  111. package/public/vendor/vitest-docs/config/experimental.md +0 -510
  112. package/public/vendor/vitest-docs/config/faketimers.md +0 -51
  113. package/public/vendor/vitest-docs/config/fileparallelism.md +0 -11
  114. package/public/vendor/vitest-docs/config/forcereruntriggers.md +0 -19
  115. package/public/vendor/vitest-docs/config/globals.md +0 -42
  116. package/public/vendor/vitest-docs/config/globalsetup.md +0 -72
  117. package/public/vendor/vitest-docs/config/hideskippedtests.md +0 -7
  118. package/public/vendor/vitest-docs/config/hooktimeout.md +0 -7
  119. package/public/vendor/vitest-docs/config/include-source.md +0 -115
  120. package/public/vendor/vitest-docs/config/include.md +0 -71
  121. package/public/vendor/vitest-docs/config/includetasklocation.md +0 -17
  122. package/public/vendor/vitest-docs/config/index.md +0 -85
  123. package/public/vendor/vitest-docs/config/isolate.md +0 -13
  124. package/public/vendor/vitest-docs/config/logheapusage.md +0 -7
  125. package/public/vendor/vitest-docs/config/maxconcurrency.md +0 -9
  126. package/public/vendor/vitest-docs/config/maxworkers.md +0 -49
  127. package/public/vendor/vitest-docs/config/mockreset.md +0 -22
  128. package/public/vendor/vitest-docs/config/mode.md +0 -7
  129. package/public/vendor/vitest-docs/config/name.md +0 -111
  130. package/public/vendor/vitest-docs/config/onconsolelog.md +0 -25
  131. package/public/vendor/vitest-docs/config/onstacktrace.md +0 -32
  132. package/public/vendor/vitest-docs/config/onunhandlederror.md +0 -35
  133. package/public/vendor/vitest-docs/config/open.md +0 -7
  134. package/public/vendor/vitest-docs/config/outputfile.md +0 -7
  135. package/public/vendor/vitest-docs/config/passwithnotests.md +0 -7
  136. package/public/vendor/vitest-docs/config/pool.md +0 -45
  137. package/public/vendor/vitest-docs/config/printconsoletrace.md +0 -6
  138. package/public/vendor/vitest-docs/config/projects.md +0 -6
  139. package/public/vendor/vitest-docs/config/provide.md +0 -45
  140. package/public/vendor/vitest-docs/config/reporters.md +0 -69
  141. package/public/vendor/vitest-docs/config/resolvesnapshotpath.md +0 -36
  142. package/public/vendor/vitest-docs/config/restoremocks.md +0 -22
  143. package/public/vendor/vitest-docs/config/retry.md +0 -140
  144. package/public/vendor/vitest-docs/config/root.md +0 -6
  145. package/public/vendor/vitest-docs/config/runner.md +0 -6
  146. package/public/vendor/vitest-docs/config/sequence.md +0 -158
  147. package/public/vendor/vitest-docs/config/server.md +0 -68
  148. package/public/vendor/vitest-docs/config/setupfiles.md +0 -40
  149. package/public/vendor/vitest-docs/config/silent.md +0 -9
  150. package/public/vendor/vitest-docs/config/slowtestthreshold.md +0 -7
  151. package/public/vendor/vitest-docs/config/snapshotenvironment.md +0 -27
  152. package/public/vendor/vitest-docs/config/snapshotformat.md +0 -28
  153. package/public/vendor/vitest-docs/config/snapshotserializers.md +0 -6
  154. package/public/vendor/vitest-docs/config/stricttags.md +0 -30
  155. package/public/vendor/vitest-docs/config/tags.md +0 -141
  156. package/public/vendor/vitest-docs/config/teardowntimeout.md +0 -7
  157. package/public/vendor/vitest-docs/config/testnamepattern.md +0 -21
  158. package/public/vendor/vitest-docs/config/testtimeout.md +0 -7
  159. package/public/vendor/vitest-docs/config/typecheck.md +0 -77
  160. package/public/vendor/vitest-docs/config/ui.md +0 -15
  161. package/public/vendor/vitest-docs/config/unstubenvs.md +0 -20
  162. package/public/vendor/vitest-docs/config/unstubglobals.md +0 -20
  163. package/public/vendor/vitest-docs/config/update.md +0 -16
  164. package/public/vendor/vitest-docs/config/vmmemorylimit.md +0 -30
  165. package/public/vendor/vitest-docs/config/watch.md +0 -11
  166. package/public/vendor/vitest-docs/config/watchtriggerpatterns.md +0 -29
  167. package/public/vendor/vitest-docs/guide/advanced/index.md +0 -147
  168. package/public/vendor/vitest-docs/guide/advanced/pool.md +0 -148
  169. package/public/vendor/vitest-docs/guide/advanced/reporters.md +0 -93
  170. package/public/vendor/vitest-docs/guide/advanced/tests.md +0 -125
  171. package/public/vendor/vitest-docs/guide/browser/aria-snapshots.md +0 -470
  172. package/public/vendor/vitest-docs/guide/browser/component-testing.md +0 -571
  173. package/public/vendor/vitest-docs/guide/browser/index.md +0 -630
  174. package/public/vendor/vitest-docs/guide/browser/multiple-setups.md +0 -121
  175. package/public/vendor/vitest-docs/guide/browser/trace-view.md +0 -126
  176. package/public/vendor/vitest-docs/guide/browser/visual-regression-testing.md +0 -734
  177. package/public/vendor/vitest-docs/guide/cli-generated.md +0 -972
  178. package/public/vendor/vitest-docs/guide/cli.md +0 -234
  179. package/public/vendor/vitest-docs/guide/common-errors.md +0 -163
  180. package/public/vendor/vitest-docs/guide/coverage.md +0 -515
  181. package/public/vendor/vitest-docs/guide/debugging.md +0 -127
  182. package/public/vendor/vitest-docs/guide/environment.md +0 -101
  183. package/public/vendor/vitest-docs/guide/extending-matchers.md +0 -160
  184. package/public/vendor/vitest-docs/guide/features.md +0 -310
  185. package/public/vendor/vitest-docs/guide/filtering.md +0 -175
  186. package/public/vendor/vitest-docs/guide/ide.md +0 -43
  187. package/public/vendor/vitest-docs/guide/improving-performance.md +0 -245
  188. package/public/vendor/vitest-docs/guide/in-source.md +0 -159
  189. package/public/vendor/vitest-docs/guide/index.md +0 -128
  190. package/public/vendor/vitest-docs/guide/learn/async.md +0 -147
  191. package/public/vendor/vitest-docs/guide/learn/debugging-tests.md +0 -210
  192. package/public/vendor/vitest-docs/guide/learn/matchers.md +0 -277
  193. package/public/vendor/vitest-docs/guide/learn/mock-functions.md +0 -277
  194. package/public/vendor/vitest-docs/guide/learn/setup-teardown.md +0 -240
  195. package/public/vendor/vitest-docs/guide/learn/snapshots.md +0 -166
  196. package/public/vendor/vitest-docs/guide/learn/testing-in-practice.md +0 -430
  197. package/public/vendor/vitest-docs/guide/learn/writing-tests-with-ai.md +0 -127
  198. package/public/vendor/vitest-docs/guide/learn/writing-tests.md +0 -231
  199. package/public/vendor/vitest-docs/guide/lifecycle.md +0 -379
  200. package/public/vendor/vitest-docs/guide/migration.md +0 -863
  201. package/public/vendor/vitest-docs/guide/mocking/classes.md +0 -158
  202. package/public/vendor/vitest-docs/guide/mocking/dates.md +0 -52
  203. package/public/vendor/vitest-docs/guide/mocking/file-system.md +0 -74
  204. package/public/vendor/vitest-docs/guide/mocking/functions.md +0 -61
  205. package/public/vendor/vitest-docs/guide/mocking/globals.md +0 -20
  206. package/public/vendor/vitest-docs/guide/mocking/modules.md +0 -414
  207. package/public/vendor/vitest-docs/guide/mocking/requests.md +0 -114
  208. package/public/vendor/vitest-docs/guide/mocking/timers.md +0 -48
  209. package/public/vendor/vitest-docs/guide/mocking.md +0 -239
  210. package/public/vendor/vitest-docs/guide/open-telemetry.md +0 -156
  211. package/public/vendor/vitest-docs/guide/parallelism.md +0 -82
  212. package/public/vendor/vitest-docs/guide/profiling-test-performance.md +0 -243
  213. package/public/vendor/vitest-docs/guide/projects.md +0 -291
  214. package/public/vendor/vitest-docs/guide/recipes.md +0 -59
  215. package/public/vendor/vitest-docs/guide/reporters.md +0 -723
  216. package/public/vendor/vitest-docs/guide/snapshot.md +0 -620
  217. package/public/vendor/vitest-docs/guide/test-annotations.md +0 -103
  218. package/public/vendor/vitest-docs/guide/test-context.md +0 -902
  219. package/public/vendor/vitest-docs/guide/test-tags.md +0 -314
  220. package/public/vendor/vitest-docs/guide/testing-types.md +0 -149
  221. package/public/vendor/vitest-docs/guide/ui.md +0 -160
  222. package/public/vendor/vitest-docs/guide/using-plugins.md +0 -5
  223. package/public/vendor/vitest-docs/manifest.json +0 -1691
  224. package/resources/index.js +0 -166
  225. package/resources/indexes.js +0 -77
  226. package/resources/manifest-schema.js +0 -46
  227. package/resources/paths.js +0 -20
  228. package/resources/patterns.js +0 -22
  229. package/resources/upstream-docs.js +0 -22
@@ -1,342 +0,0 @@
1
- # Reporters
2
-
3
- ::: warning
4
- This is an advanced API. If you just want to configure built-in reporters, read the ["Reporters"](/guide/reporters) guide.
5
- :::
6
-
7
- Vitest has its own test run lifecycle. These are represented by reporter's methods:
8
-
9
- - [`onInit`](#oninit)
10
- - [`onTestRunStart`](#ontestrunstart)
11
- - [`onTestModuleQueued`](#ontestmodulequeued)
12
- - [`onTestModuleCollected`](#ontestmodulecollected)
13
- - [`onTestModuleStart`](#ontestmodulestart)
14
- - [`onTestSuiteReady`](#ontestsuiteready)
15
- - [`onHookStart(beforeAll)`](#onhookstart)
16
- - [`onHookEnd(beforeAll)`](#onhookend)
17
- - [`onTestCaseReady`](#ontestcaseready)
18
- - [`onTestCaseAnnotate`](#ontestcaseannotate) <Version>3.2.0</Version>
19
- - [`onTestCaseArtifactRecord`](#ontestcaseartifactrecord) <Version type="experimental">4.0.11</Version>
20
- - [`onHookStart(beforeEach)`](#onhookstart)
21
- - [`onHookEnd(beforeEach)`](#onhookend)
22
- - [`onHookStart(afterEach)`](#onhookstart)
23
- - [`onHookEnd(afterEach)`](#onhookend)
24
- - [`onTestCaseResult`](#ontestcaseresult)
25
- - [`onHookStart(afterAll)`](#onhookstart)
26
- - [`onHookEnd(afterAll)`](#onhookend)
27
- - [`onTestSuiteResult`](#ontestsuiteresult)
28
- - [`onTestModuleEnd`](#ontestmoduleend)
29
- - [`onCoverage`](#oncoverage)
30
- - [`onTestRunEnd`](#ontestrunend)
31
-
32
- Tests and suites within a single module will be reported in order unless they were skipped. All skipped tests are reported at the end of suite/module.
33
-
34
- Note that since test modules can run in parallel, Vitest will report them in parallel.
35
-
36
- This guide lists all supported reporter methods. However, don't forget that instead of creating your own reporter, you can [extend existing one](/guide/advanced/reporters) instead:
37
-
38
- ```ts [custom-reporter.js]
39
- import { BaseReporter } from 'vitest/node'
40
-
41
- export default class CustomReporter extends BaseReporter {
42
- onTestRunEnd(testModules, errors) {
43
- console.log(testModule.length, 'tests finished running')
44
- super.onTestRunEnd(testModules, errors)
45
- }
46
- }
47
- ```
48
-
49
- ## onInit
50
-
51
- ```ts
52
- function onInit(vitest: Vitest): Awaitable<void>
53
- ```
54
-
55
- This method is called when [Vitest](/api/advanced/vitest) was initiated or started, but before the tests were filtered.
56
-
57
- ::: info
58
- Internally this method is called inside [`vitest.start`](/api/advanced/vitest#start), [`vitest.standalone`](/api/advanced/vitest#standalone) or [`vitest.mergeReports`](/api/advanced/vitest#mergereports). If you are using programmatic API, make sure to call either one depending on your needs before calling [`vitest.runTestSpecifications`](/api/advanced/vitest#runtestspecifications), for example. Built-in CLI will always run methods in correct order.
59
- :::
60
-
61
- Note that you can also get access to `vitest` instance from test cases, suites and test modules via a [`project`](/api/advanced/test-project) property, but it might also be useful to store a reference to `vitest` in this method.
62
-
63
- ::: details Example
64
- ```ts
65
- import type { Reporter, TestSpecification, Vitest } from 'vitest/node'
66
-
67
- class MyReporter implements Reporter {
68
- private vitest!: Vitest
69
-
70
- onInit(vitest: Vitest) {
71
- this.vitest = vitest
72
- }
73
-
74
- onTestRunStart(specifications: TestSpecification[]) {
75
- console.log(
76
- specifications.length,
77
- 'test files will run in',
78
- this.vitest.config.root,
79
- )
80
- }
81
- }
82
-
83
- export default new MyReporter()
84
- ```
85
- :::
86
-
87
- ## onBrowserInit {#onbrowserinit}
88
-
89
- ```ts
90
- function onBrowserInit(project: TestProject): Awaitable<void>
91
- ```
92
-
93
- This method is called when the browser instance is initiated. It receives an instance of the project for which the browser is initiated. `project.browser` will always be defined when this method is called.
94
-
95
- ## onTestRunStart
96
-
97
- ```ts
98
- function onTestRunStart(
99
- specifications: TestSpecification[]
100
- ): Awaitable<void>
101
- ```
102
-
103
- This method is called when a new test run has started. It receives an array of [test specifications](/api/advanced/test-specification) scheduled to run. This array is readonly and available only for information purposes.
104
-
105
- If Vitest didn't find any test files to run, this event will be invoked with an empty array, and then [`onTestRunEnd`](#ontestrunend) will be called immediately after.
106
-
107
- ::: details Example
108
- ```ts
109
- import type { Reporter, TestSpecification } from 'vitest/node'
110
-
111
- class MyReporter implements Reporter {
112
- onTestRunStart(specifications: TestSpecification[]) {
113
- console.log(specifications.length, 'test files will run')
114
- }
115
- }
116
-
117
- export default new MyReporter()
118
- ```
119
- :::
120
-
121
- ## onTestRunEnd
122
-
123
- ```ts
124
- function onTestRunEnd(
125
- testModules: ReadonlyArray<TestModule>,
126
- unhandledErrors: ReadonlyArray<SerializedError>,
127
- reason: TestRunEndReason
128
- ): Awaitable<void>
129
- ```
130
-
131
- This method is called after all tests have finished running and the coverage merged all reports, if it's enabled. Note that you can get the coverage information in [`onCoverage`](#oncoverage) hook.
132
-
133
- It receives a readonly list of test modules. You can iterate over it via a [`testModule.children`](/api/advanced/test-collection) property to report the state and errors, if any.
134
-
135
- The second argument is a readonly list of unhandled errors that Vitest wasn't able to attribute to any test. These can happen outside of the test run because of an error in a plugin, or inside the test run as a side-effect of a non-awaited function (for example, a timeout that threw an error after the test has finished running).
136
-
137
- The third argument indicated why the test run was finished:
138
-
139
- - `passed`: test run was finished normally and there are no errors
140
- - `failed`: test run has at least one error (due to a syntax error during collection or an actual error during test execution)
141
- - `interrupted`: test was interrupted by [`vitest.cancelCurrentRun`](/api/advanced/vitest#cancelcurrentrun) call or `Ctrl+C` was pressed in the terminal (note that it's still possible to have failed tests in this case)
142
-
143
- If Vitest didn't find any test files to run, this event will be invoked with empty arrays of modules and errors, and the state will depend on the value of [`config.passWithNoTests`](/config/passwithnotests).
144
-
145
- ::: details Example
146
- ```ts
147
- import type {
148
- Reporter,
149
- SerializedError,
150
- TestModule,
151
- TestRunEndReason,
152
- TestSpecification
153
- } from 'vitest/node'
154
-
155
- class MyReporter implements Reporter {
156
- onTestRunEnd(
157
- testModules: ReadonlyArray<TestModule>,
158
- unhandledErrors: ReadonlyArray<SerializedError>,
159
- reason: TestRunEndReason,
160
- ) {
161
- if (reason === 'passed') {
162
- testModules.forEach(module => console.log(module.moduleId, 'succeeded'))
163
- }
164
- else if (reason === 'failed') {
165
- // note that this will skip possible errors in suites
166
- // you can get them from testSuite.errors()
167
- for (const testCase of testModules.children.allTests()) {
168
- if (testCase.result().state === 'failed') {
169
- console.log(testCase.fullName, 'in', testCase.module.moduleId, 'failed')
170
- console.log(testCase.result().errors)
171
- }
172
- }
173
- }
174
- else {
175
- console.log('test run was interrupted, skipping report')
176
- }
177
- }
178
- }
179
-
180
- export default new MyReporter()
181
- ```
182
- :::
183
-
184
- ## onCoverage
185
-
186
- ```ts
187
- function onCoverage(coverage: unknown): Awaitable<void>
188
- ```
189
-
190
- This hook is called after coverage results have been processed. Coverage provider's reporters are called after this hook. The typings of `coverage` depends on the `coverage.provider`. For Vitest's default built-in providers you can import the types from `istanbul-lib-coverage` package:
191
-
192
- ```ts
193
- import type { CoverageMap } from 'istanbul-lib-coverage'
194
-
195
- declare function onCoverage(coverage: CoverageMap): Awaitable<void>
196
- ```
197
-
198
- If Vitest didn't perform any coverage, this hook is not called.
199
-
200
- ## onTestModuleQueued
201
-
202
- ```ts
203
- function onTestModuleQueued(testModule: TestModule): Awaitable<void>
204
- ```
205
-
206
- This method is called right before Vitest imports the setup file and the test module itself. This means that `testModule` will have no [`children`](/api/advanced/test-suite#children) yet, but you can start reporting it as the next test to run.
207
-
208
- ## onTestModuleCollected
209
-
210
- ```ts
211
- function onTestModuleCollected(testModule: TestModule): Awaitable<void>
212
- ```
213
-
214
- This method is called when all tests inside the file were collected, meaning [`testModule.children`](/api/advanced/test-suite#children) collection is populated, but tests don't have any results yet.
215
-
216
- ## onTestModuleStart
217
-
218
- ```ts
219
- function onTestModuleStart(testModule: TestModule): Awaitable<void>
220
- ```
221
-
222
- This method is called right after [`onTestModuleCollected`](#ontestmodulecollected) unless Vitest runs in collection mode ([`vitest.collect()`](/api/advanced/vitest#collect) or `vitest collect` in the CLI), in this case it will not be called at all because there are no tests to run.
223
-
224
- ## onTestModuleEnd
225
-
226
- ```ts
227
- function onTestModuleEnd(testModule: TestModule): Awaitable<void>
228
- ```
229
-
230
- This method is called when every test in the module finished running. This means, every test inside [`testModule.children`](/api/advanced/test-suite#children) will have a `test.result()` that is not equal to `pending`.
231
-
232
- ## onHookStart
233
-
234
- ```ts
235
- function onHookStart(context: ReportedHookContext): Awaitable<void>
236
- ```
237
-
238
- This method is called when any of these hooks have started running:
239
-
240
- - `beforeAll`
241
- - `afterAll`
242
- - `beforeEach`
243
- - `afterEach`
244
-
245
- If `beforeAll` or `afterAll` are started, the `entity` will be either [`TestSuite`](/api/advanced/test-suite) or [`TestModule`](/api/advanced/test-module).
246
-
247
- If `beforeEach` or `afterEach` are started, the `entity` will always be [`TestCase`](/api/advanced/test-case).
248
-
249
- ::: warning
250
- `onHookStart` method will not be called if the hook did not run during the test run.
251
- :::
252
-
253
- ## onHookEnd
254
-
255
- ```ts
256
- function onHookEnd(context: ReportedHookContext): Awaitable<void>
257
- ```
258
-
259
- This method is called when any of these hooks have finished running:
260
-
261
- - `beforeAll`
262
- - `afterAll`
263
- - `beforeEach`
264
- - `afterEach`
265
-
266
- If `beforeAll` or `afterAll` have finished, the `entity` will be either [`TestSuite`](/api/advanced/test-suite) or [`TestModule`](/api/advanced/test-module).
267
-
268
- If `beforeEach` or `afterEach` have finished, the `entity` will always be [`TestCase`](/api/advanced/test-case).
269
-
270
- ::: warning
271
- `onHookEnd` method will not be called if the hook did not run during the test run.
272
- :::
273
-
274
- ## onTestSuiteReady
275
-
276
- ```ts
277
- function onTestSuiteReady(testSuite: TestSuite): Awaitable<void>
278
- ```
279
-
280
- This method is called before the suite starts to run its tests. This method is also called if the suite was skipped.
281
-
282
- If the file doesn't have any suites, this method will not be called. Consider using `onTestModuleStart` to cover this use case.
283
-
284
- ## onTestSuiteResult
285
-
286
- ```ts
287
- function onTestSuiteResult(testSuite: TestSuite): Awaitable<void>
288
- ```
289
-
290
- This method is called after the suite has finished running tests. This method is also called if the suite was skipped.
291
-
292
- If the file doesn't have any suites, this method will not be called. Consider using `onTestModuleEnd` to cover this use case.
293
-
294
- ## onTestCaseReady
295
-
296
- ```ts
297
- function onTestCaseReady(testCase: TestCase): Awaitable<void>
298
- ```
299
-
300
- This method is called before the test starts to run or it was skipped. Note that `beforeEach` and `afterEach` hooks are considered part of the test because they can influence the result.
301
-
302
- ::: warning
303
- Notice that it's possible to have [`testCase.result()`](/api/advanced/test-case#result) with `passed` or `failed` state already when `onTestCaseReady` is called. This can happen if test was running too fast and both `onTestCaseReady` and `onTestCaseResult` were scheduled to run in the same microtask.
304
- :::
305
-
306
- ## onTestCaseResult
307
-
308
- ```ts
309
- function onTestCaseResult(testCase: TestCase): Awaitable<void>
310
- ```
311
-
312
- This method is called when the test has finished running or was just skipped. Note that this will be called after the `afterEach` hook is finished, if there are any.
313
-
314
- At this point, [`testCase.result()`](/api/advanced/test-case#result) will have non-pending state.
315
-
316
- ## onTestCaseAnnotate <Version>3.2.0</Version> {#ontestcaseannotate}
317
-
318
- ```ts
319
- function onTestCaseAnnotate(
320
- testCase: TestCase,
321
- annotation: TestAnnotation,
322
- ): Awaitable<void>
323
- ```
324
-
325
- The `onTestCaseAnnotate` hook is associated with the [`context.annotate`](/guide/test-context#annotate) method. When `annotate` is invoked, Vitest serialises it and sends the same attachment to the main thread where reporter can interact with it.
326
-
327
- If the path is specified, Vitest stores it in a separate directory (configured by [`attachmentsDir`](/config/attachmentsdir)) and modifies the `path` property to reference it.
328
-
329
- ## onTestCaseArtifactRecord <Version type="experimental">4.0.11</Version> {#ontestcaseartifactrecord}
330
-
331
- ```ts
332
- function onTestCaseArtifactRecord(
333
- testCase: TestCase,
334
- artifact: TestArtifact,
335
- ): Awaitable<void>
336
- ```
337
-
338
- The `onTestCaseArtifactRecord` hook is associated with the [`recordArtifact`](/api/advanced/artifacts#recordartifact) utility. When `recordArtifact` is invoked, Vitest serialises it and sends the same attachment to the main thread where reporter can interact with it.
339
-
340
- If the path is specified, Vitest stores it in a separate directory (configured by [`attachmentsDir`](/config/attachmentsdir)) and modifies the `path` property to reference it.
341
-
342
- Note: annotations, [even though they're built on top of this feature](/api/advanced/artifacts#relationship-with-annotations), won't hit this hook and won't appear in the `task.artifacts` array for backwards compatibility reasons until the next major version.
@@ -1,334 +0,0 @@
1
- # Runner API <Badge type="danger">advanced</Badge>
2
-
3
- ::: warning
4
- This is advanced API. If you just want to [run tests](/guide/), you probably don't need this. It is primarily used by library authors.
5
- :::
6
-
7
- You can specify a path to your test runner with the `runner` option in your configuration file. This file should have a default export with a class constructor implementing these methods:
8
-
9
- ```ts
10
- export interface VitestRunner {
11
- /**
12
- * First thing that's getting called before actually collecting and running tests.
13
- */
14
- onBeforeCollect?: (paths: string[]) => unknown
15
- /**
16
- * Called after collecting tests and before "onBeforeRun".
17
- */
18
- onCollected?: (files: File[]) => unknown
19
-
20
- /**
21
- * Called when test runner should cancel next test runs.
22
- * Runner should listen for this method and mark tests and suites as skipped in
23
- * "onBeforeRunSuite" and "onBeforeRunTask" when called.
24
- */
25
- onCancel?: (reason: CancelReason) => unknown
26
-
27
- /**
28
- * Called before running a single test. Doesn't have "result" yet.
29
- */
30
- onBeforeRunTask?: (test: Test) => unknown
31
- /**
32
- * Called before actually running the test function. Already has "result" with "state" and "startTime".
33
- */
34
- onBeforeTryTask?: (test: Test, options: { retry: number; repeats: number }) => unknown
35
- /**
36
- * Called after result and state are set.
37
- */
38
- onAfterRunTask?: (test: Test) => unknown
39
- /**
40
- * Called right after running the test function. Doesn't have new state yet. Will not be called, if the test function throws.
41
- */
42
- onAfterTryTask?: (test: Test, options: { retry: number; repeats: number }) => unknown
43
- /**
44
- * Called after the retry resolution happened. Unlike `onAfterTryTask`, the test now has a new state.
45
- * All `after` hooks were also called by this point.
46
- */
47
- onAfterRetryTask?: (test: Test, options: { retry: number; repeats: number }) => unknown
48
-
49
- /**
50
- * Called before running a single suite. Doesn't have "result" yet.
51
- */
52
- onBeforeRunSuite?: (suite: Suite) => unknown
53
- /**
54
- * Called after running a single suite. Has state and result.
55
- */
56
- onAfterRunSuite?: (suite: Suite) => unknown
57
-
58
- /**
59
- * If defined, will be called instead of usual Vitest suite partition and handling.
60
- * "before" and "after" hooks will not be ignored.
61
- */
62
- runSuite?: (suite: Suite) => Promise<void>
63
- /**
64
- * If defined, will be called instead of usual Vitest handling. Useful, if you have your custom test function.
65
- * "before" and "after" hooks will not be ignored.
66
- */
67
- runTask?: (test: TaskPopulated) => Promise<void>
68
-
69
- /**
70
- * Called, when a task is updated. The same as "onTaskUpdate" in a reporter, but this is running in the same thread as tests.
71
- */
72
- onTaskUpdate?: (task: [string, TaskResult | undefined, TaskMeta | undefined][]) => Promise<void>
73
-
74
- /**
75
- * Called before running all tests in collected paths.
76
- */
77
- onBeforeRunFiles?: (files: File[]) => unknown
78
- /**
79
- * Called right after running all tests in collected paths.
80
- */
81
- onAfterRunFiles?: (files: File[]) => unknown
82
- /**
83
- * Called when new context for a test is defined. Useful, if you want to add custom properties to the context.
84
- * If you only want to define custom context with a runner, consider using "beforeAll" in "setupFiles" instead.
85
- */
86
- extendTaskContext?: (context: TestContext) => TestContext
87
- /**
88
- * Called when certain files are imported. Can be called in two situations: to collect tests and to import setup files.
89
- */
90
- importFile: (filepath: string, source: VitestRunnerImportSource) => unknown
91
- /**
92
- * Function that is called when the runner attempts to get the value when `test.extend` is used with `{ injected: true }`
93
- */
94
- injectValue?: (key: string) => unknown
95
- /**
96
- * Publicly available configuration.
97
- */
98
- config: VitestRunnerConfig
99
- /**
100
- * The name of the current pool. Can affect how stack trace is inferred on the server side.
101
- */
102
- pool?: string
103
- }
104
- ```
105
-
106
- When initiating this class, Vitest passes down Vitest config, - you should expose it as a `config` property:
107
-
108
- ```ts [runner.ts]
109
- import type { RunnerTestFile, SerializedConfig, TestRunner, VitestTestRunner } from 'vitest'
110
-
111
- class CustomRunner extends TestRunner implements VitestTestRunner {
112
- public config: SerializedConfig
113
-
114
- constructor(config: SerializedConfig) {
115
- this.config = config
116
- }
117
-
118
- onAfterRunFiles(files: RunnerTestFile[]) {
119
- console.log('finished running', files)
120
- }
121
- }
122
-
123
- export default CustomRunner
124
- ```
125
-
126
- ::: warning
127
- Vitest also injects an instance of `ModuleRunner` from `vite/module-runner` as `moduleRunner` property. You can use it to process files in `importFile` method (this is default behavior of `TestRunner` and `BenchmarkRunner`).
128
-
129
- `ModuleRunner` exposes `import` method, which is used to import test files in a Vite-friendly environment. Meaning, it will resolve imports and transform file content at runtime so that Node can understand it:
130
-
131
- ```ts
132
- export default class Runner {
133
- async importFile(filepath: string) {
134
- await this.moduleRunner.import(filepath)
135
- }
136
- }
137
- ```
138
- :::
139
-
140
- ::: warning
141
- If you don't have a custom runner or didn't define `runTest` method, Vitest will try to retrieve a task automatically. If you didn't add a function with `setFn`, it will fail.
142
- :::
143
-
144
- ::: tip
145
- Snapshot support and some other features depend on the runner. If you don't want to lose it, you can extend your runner from `VitestTestRunner` imported from `vitest/runners`. It also exposes `NodeBenchmarkRunner`, if you want to extend benchmark functionality.
146
- :::
147
-
148
- ## Tasks
149
-
150
- ::: warning
151
- The "Runner Tasks API" is experimental and should primarily be used only in the test runtime. Vitest also exposes the ["Reported Tasks API"](/api/advanced/test-module), which should be preferred when working in the main thread (inside the reporter, for example).
152
-
153
- The team is currently discussing if "Runner Tasks" should be replaced by "Reported Tasks" in the future.
154
- :::
155
-
156
- Suites and tests are called `tasks` internally. Vitest runner initiates a `File` task before collecting any tests - this is a superset of `Suite` with a few additional properties. It is available on every task (including `File`) as a `file` property.
157
-
158
- ```ts
159
- interface File extends Suite {
160
- /**
161
- * The name of the pool that the file belongs to.
162
- * @default 'forks'
163
- */
164
- pool?: string
165
- /**
166
- * The path to the file in UNIX format.
167
- */
168
- filepath: string
169
- /**
170
- * The name of the test project the file belongs to.
171
- */
172
- projectName: string | undefined
173
- /**
174
- * The time it took to collect all tests in the file.
175
- * This time also includes importing all the file dependencies.
176
- */
177
- collectDuration?: number
178
- /**
179
- * The time it took to import the setup file.
180
- */
181
- setupDuration?: number
182
- }
183
- ```
184
-
185
- Every suite has a `tasks` property that is populated during collection phase. It is useful to traverse the task tree from the top down.
186
-
187
- ```ts
188
- interface Suite extends TaskBase {
189
- type: 'suite'
190
- /**
191
- * File task. It's the root task of the file.
192
- */
193
- file: File
194
- /**
195
- * An array of tasks that are part of the suite.
196
- */
197
- tasks: Task[]
198
- }
199
- ```
200
-
201
- Every task has a `suite` property that references a suite it is located in. If `test` or `describe` are initiated at the top level, they will not have a `suite` property (it will **not** be equal to `file`!). `File` also never has a `suite` property. It is useful to traverse the tasks from the bottom up.
202
-
203
- ```ts
204
- interface Test<ExtraContext = object> extends TaskBase {
205
- type: 'test'
206
- /**
207
- * Test context that will be passed to the test function.
208
- */
209
- context: TestContext & ExtraContext
210
- /**
211
- * File task. It's the root task of the file.
212
- */
213
- file: File
214
- /**
215
- * Whether the task was skipped by calling `context.skip()`.
216
- */
217
- pending?: boolean
218
- /**
219
- * Whether the task should succeed if it fails. If the task fails, it will be marked as passed.
220
- */
221
- fails?: boolean
222
- /**
223
- * Store promises (from async expects) to wait for them before finishing the test
224
- */
225
- promises?: Promise<any>[]
226
- }
227
- ```
228
-
229
- Every task can have a `result` field. Suites can only have this field if an error thrown within a suite callback or `beforeAll`/`afterAll` callbacks prevents them from collecting tests. Tests always have this field after their callbacks are called - the `state` and `errors` fields are present depending on the outcome. If an error was thrown in `beforeEach` or `afterEach` callbacks, the thrown error will be present in `task.result.errors`.
230
-
231
- ```ts
232
- export interface TaskResult {
233
- /**
234
- * State of the task. Inherits the `task.mode` during collection.
235
- * When the task has finished, it will be changed to `pass` or `fail`.
236
- * - **pass**: task ran successfully
237
- * - **fail**: task failed
238
- */
239
- state: TaskState
240
- /**
241
- * Errors that occurred during the task execution. It is possible to have several errors
242
- * if `expect.soft()` failed multiple times.
243
- */
244
- errors?: TestError[]
245
- /**
246
- * How long in milliseconds the task took to run.
247
- */
248
- duration?: number
249
- /**
250
- * Time in milliseconds when the task started running.
251
- */
252
- startTime?: number
253
- /**
254
- * Heap size in bytes after the task finished.
255
- * Only available if `logHeapUsage` option is set and `process.memoryUsage` is defined.
256
- */
257
- heap?: number
258
- /**
259
- * State of related to this task hooks. Useful during reporting.
260
- */
261
- hooks?: Partial<Record<'afterAll' | 'beforeAll' | 'beforeEach' | 'afterEach', TaskState>>
262
- /**
263
- * The amount of times the task was retried. The task is retried only if it
264
- * failed and `retry` option is set.
265
- */
266
- retryCount?: number
267
- /**
268
- * The amount of times the task was repeated. The task is repeated only if
269
- * `repeats` option is set. This number also contains `retryCount`.
270
- */
271
- repeatCount?: number
272
- }
273
- ```
274
-
275
- ## Your Task Function
276
-
277
- Vitest exposes `createTaskCollector` utility to create your own `test` method. It behaves the same way as a test, but calls a custom method during collection.
278
-
279
- A task is an object that is part of a suite. It is automatically added to the current suite with a `suite.task` method:
280
-
281
- ```js [custom.js]
282
- export { afterAll, beforeAll, describe, TestRunner } from 'vitest'
283
-
284
- // this function will be called during collection phase:
285
- // don't call function handler here, add it to suite tasks
286
- // with "getCurrentSuite().task()" method
287
- // note: createTaskCollector provides support for "todo"/"each"/...
288
- export const myCustomTask = TestRunner.createTaskCollector(
289
- function (name, fn, timeout) {
290
- TestRunner.getCurrentSuite().task(name, {
291
- ...this, // so "todo"/"skip"/... is tracked correctly
292
- meta: {
293
- customPropertyToDifferentiateTask: true
294
- },
295
- handler: fn,
296
- timeout,
297
- })
298
- }
299
- )
300
- ```
301
-
302
- ```js [tasks.test.js]
303
- import {
304
- afterAll,
305
- beforeAll,
306
- describe,
307
- myCustomTask
308
- } from './custom.js'
309
- import { gardener } from './gardener.js'
310
-
311
- describe('take care of the garden', () => {
312
- beforeAll(() => {
313
- gardener.putWorkingClothes()
314
- })
315
-
316
- myCustomTask('weed the grass', () => {
317
- gardener.weedTheGrass()
318
- })
319
- myCustomTask.todo('mow the lawn', () => {
320
- gardener.mowerTheLawn()
321
- })
322
- myCustomTask('water flowers', () => {
323
- gardener.waterFlowers()
324
- })
325
-
326
- afterAll(() => {
327
- gardener.goHome()
328
- })
329
- })
330
- ```
331
-
332
- ```bash
333
- vitest ./garden/tasks.test.js
334
- ```