@vitest-agent/mcp 1.1.0 → 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 (208) hide show
  1. package/README.md +1 -2
  2. package/index.d.ts +2 -2
  3. package/index.js +1 -1
  4. package/package.json +1 -1
  5. package/server.js +2 -4
  6. package/patterns/_meta.json +0 -67
  7. package/patterns/authoring-a-custom-vitest-agent-reporter.md +0 -82
  8. package/patterns/known-issues-and-caveats.md +0 -52
  9. package/patterns/operating-vitest-agent-as-an-agent.md +0 -62
  10. package/patterns/running-tests-via-mcp.md +0 -103
  11. package/patterns/silencing-leaking-output-in-tests.md +0 -91
  12. package/patterns/testing-effect-schema-definitions.md +0 -71
  13. package/patterns/testing-effect-services-with-mock-layers.md +0 -63
  14. package/resources/index.js +0 -166
  15. package/resources/indexes.js +0 -77
  16. package/resources/manifest-schema.js +0 -46
  17. package/resources/paths.js +0 -20
  18. package/resources/patterns.js +0 -22
  19. package/resources/upstream-docs.js +0 -22
  20. package/vendor/vitest-docs/ATTRIBUTION.md +0 -5
  21. package/vendor/vitest-docs/api/advanced/artifacts.md +0 -189
  22. package/vendor/vitest-docs/api/advanced/metadata.md +0 -68
  23. package/vendor/vitest-docs/api/advanced/plugin.md +0 -168
  24. package/vendor/vitest-docs/api/advanced/reporters.md +0 -342
  25. package/vendor/vitest-docs/api/advanced/runner.md +0 -334
  26. package/vendor/vitest-docs/api/advanced/test-case.md +0 -302
  27. package/vendor/vitest-docs/api/advanced/test-collection.md +0 -89
  28. package/vendor/vitest-docs/api/advanced/test-module.md +0 -140
  29. package/vendor/vitest-docs/api/advanced/test-project.md +0 -321
  30. package/vendor/vitest-docs/api/advanced/test-specification.md +0 -96
  31. package/vendor/vitest-docs/api/advanced/test-suite.md +0 -230
  32. package/vendor/vitest-docs/api/advanced/vitest.md +0 -684
  33. package/vendor/vitest-docs/api/assert-type.md +0 -22
  34. package/vendor/vitest-docs/api/assert.md +0 -1960
  35. package/vendor/vitest-docs/api/browser/assertions.md +0 -1277
  36. package/vendor/vitest-docs/api/browser/commands.md +0 -154
  37. package/vendor/vitest-docs/api/browser/context.md +0 -338
  38. package/vendor/vitest-docs/api/browser/interactivity.md +0 -681
  39. package/vendor/vitest-docs/api/browser/locators.md +0 -1171
  40. package/vendor/vitest-docs/api/browser/react.md +0 -346
  41. package/vendor/vitest-docs/api/browser/svelte.md +0 -292
  42. package/vendor/vitest-docs/api/browser/vue.md +0 -222
  43. package/vendor/vitest-docs/api/describe.md +0 -374
  44. package/vendor/vitest-docs/api/expect-typeof.md +0 -571
  45. package/vendor/vitest-docs/api/expect.md +0 -2304
  46. package/vendor/vitest-docs/api/hooks.md +0 -463
  47. package/vendor/vitest-docs/api/mock.md +0 -701
  48. package/vendor/vitest-docs/api/test.md +0 -926
  49. package/vendor/vitest-docs/api/vi.md +0 -1372
  50. package/vendor/vitest-docs/config/alias.md +0 -13
  51. package/vendor/vitest-docs/config/allowonly.md +0 -32
  52. package/vendor/vitest-docs/config/api.md +0 -27
  53. package/vendor/vitest-docs/config/attachmentsdir.md +0 -6
  54. package/vendor/vitest-docs/config/bail.md +0 -9
  55. package/vendor/vitest-docs/config/benchmark.md +0 -65
  56. package/vendor/vitest-docs/config/browser/api.md +0 -23
  57. package/vendor/vitest-docs/config/browser/commands.md +0 -6
  58. package/vendor/vitest-docs/config/browser/connecttimeout.md +0 -10
  59. package/vendor/vitest-docs/config/browser/detailspanelposition.md +0 -38
  60. package/vendor/vitest-docs/config/browser/enabled.md +0 -40
  61. package/vendor/vitest-docs/config/browser/expect.md +0 -250
  62. package/vendor/vitest-docs/config/browser/headless.md +0 -7
  63. package/vendor/vitest-docs/config/browser/instances.md +0 -47
  64. package/vendor/vitest-docs/config/browser/isolate.md +0 -11
  65. package/vendor/vitest-docs/config/browser/locators.md +0 -24
  66. package/vendor/vitest-docs/config/browser/orchestratorscripts.md +0 -39
  67. package/vendor/vitest-docs/config/browser/playwright.md +0 -214
  68. package/vendor/vitest-docs/config/browser/preview.md +0 -32
  69. package/vendor/vitest-docs/config/browser/provider.md +0 -79
  70. package/vendor/vitest-docs/config/browser/screenshotdirectory.md +0 -6
  71. package/vendor/vitest-docs/config/browser/screenshotfailures.md +0 -6
  72. package/vendor/vitest-docs/config/browser/testerhtmlpath.md +0 -5
  73. package/vendor/vitest-docs/config/browser/trace.md +0 -43
  74. package/vendor/vitest-docs/config/browser/trackunhandlederrors.md +0 -10
  75. package/vendor/vitest-docs/config/browser/ui.md +0 -7
  76. package/vendor/vitest-docs/config/browser/viewport.md +0 -6
  77. package/vendor/vitest-docs/config/browser/webdriverio.md +0 -64
  78. package/vendor/vitest-docs/config/cache.md +0 -26
  79. package/vendor/vitest-docs/config/chaiconfig.md +0 -29
  80. package/vendor/vitest-docs/config/clearmocks.md +0 -22
  81. package/vendor/vitest-docs/config/coverage.md +0 -455
  82. package/vendor/vitest-docs/config/css.md +0 -47
  83. package/vendor/vitest-docs/config/dangerouslyignoreunhandlederrors.md +0 -23
  84. package/vendor/vitest-docs/config/deps.md +0 -127
  85. package/vendor/vitest-docs/config/detectasyncleaks.md +0 -39
  86. package/vendor/vitest-docs/config/diff.md +0 -96
  87. package/vendor/vitest-docs/config/dir.md +0 -7
  88. package/vendor/vitest-docs/config/disableconsoleintercept.md +0 -15
  89. package/vendor/vitest-docs/config/env.md +0 -5
  90. package/vendor/vitest-docs/config/environment.md +0 -96
  91. package/vendor/vitest-docs/config/environmentoptions.md +0 -30
  92. package/vendor/vitest-docs/config/exclude.md +0 -49
  93. package/vendor/vitest-docs/config/execargv.md +0 -10
  94. package/vendor/vitest-docs/config/expandsnapshotdiff.md +0 -7
  95. package/vendor/vitest-docs/config/expect.md +0 -38
  96. package/vendor/vitest-docs/config/experimental.md +0 -510
  97. package/vendor/vitest-docs/config/faketimers.md +0 -51
  98. package/vendor/vitest-docs/config/fileparallelism.md +0 -11
  99. package/vendor/vitest-docs/config/forcereruntriggers.md +0 -19
  100. package/vendor/vitest-docs/config/globals.md +0 -42
  101. package/vendor/vitest-docs/config/globalsetup.md +0 -72
  102. package/vendor/vitest-docs/config/hideskippedtests.md +0 -7
  103. package/vendor/vitest-docs/config/hooktimeout.md +0 -7
  104. package/vendor/vitest-docs/config/include-source.md +0 -115
  105. package/vendor/vitest-docs/config/include.md +0 -71
  106. package/vendor/vitest-docs/config/includetasklocation.md +0 -17
  107. package/vendor/vitest-docs/config/index.md +0 -85
  108. package/vendor/vitest-docs/config/isolate.md +0 -13
  109. package/vendor/vitest-docs/config/logheapusage.md +0 -7
  110. package/vendor/vitest-docs/config/maxconcurrency.md +0 -9
  111. package/vendor/vitest-docs/config/maxworkers.md +0 -49
  112. package/vendor/vitest-docs/config/mockreset.md +0 -22
  113. package/vendor/vitest-docs/config/mode.md +0 -7
  114. package/vendor/vitest-docs/config/name.md +0 -111
  115. package/vendor/vitest-docs/config/onconsolelog.md +0 -25
  116. package/vendor/vitest-docs/config/onstacktrace.md +0 -32
  117. package/vendor/vitest-docs/config/onunhandlederror.md +0 -35
  118. package/vendor/vitest-docs/config/open.md +0 -7
  119. package/vendor/vitest-docs/config/outputfile.md +0 -7
  120. package/vendor/vitest-docs/config/passwithnotests.md +0 -7
  121. package/vendor/vitest-docs/config/pool.md +0 -45
  122. package/vendor/vitest-docs/config/printconsoletrace.md +0 -6
  123. package/vendor/vitest-docs/config/projects.md +0 -6
  124. package/vendor/vitest-docs/config/provide.md +0 -45
  125. package/vendor/vitest-docs/config/reporters.md +0 -69
  126. package/vendor/vitest-docs/config/resolvesnapshotpath.md +0 -36
  127. package/vendor/vitest-docs/config/restoremocks.md +0 -22
  128. package/vendor/vitest-docs/config/retry.md +0 -140
  129. package/vendor/vitest-docs/config/root.md +0 -6
  130. package/vendor/vitest-docs/config/runner.md +0 -6
  131. package/vendor/vitest-docs/config/sequence.md +0 -158
  132. package/vendor/vitest-docs/config/server.md +0 -68
  133. package/vendor/vitest-docs/config/setupfiles.md +0 -40
  134. package/vendor/vitest-docs/config/silent.md +0 -9
  135. package/vendor/vitest-docs/config/slowtestthreshold.md +0 -7
  136. package/vendor/vitest-docs/config/snapshotenvironment.md +0 -27
  137. package/vendor/vitest-docs/config/snapshotformat.md +0 -28
  138. package/vendor/vitest-docs/config/snapshotserializers.md +0 -6
  139. package/vendor/vitest-docs/config/stricttags.md +0 -30
  140. package/vendor/vitest-docs/config/tags.md +0 -141
  141. package/vendor/vitest-docs/config/teardowntimeout.md +0 -7
  142. package/vendor/vitest-docs/config/testnamepattern.md +0 -21
  143. package/vendor/vitest-docs/config/testtimeout.md +0 -7
  144. package/vendor/vitest-docs/config/typecheck.md +0 -77
  145. package/vendor/vitest-docs/config/ui.md +0 -15
  146. package/vendor/vitest-docs/config/unstubenvs.md +0 -20
  147. package/vendor/vitest-docs/config/unstubglobals.md +0 -20
  148. package/vendor/vitest-docs/config/update.md +0 -16
  149. package/vendor/vitest-docs/config/vmmemorylimit.md +0 -30
  150. package/vendor/vitest-docs/config/watch.md +0 -11
  151. package/vendor/vitest-docs/config/watchtriggerpatterns.md +0 -29
  152. package/vendor/vitest-docs/guide/advanced/index.md +0 -147
  153. package/vendor/vitest-docs/guide/advanced/pool.md +0 -148
  154. package/vendor/vitest-docs/guide/advanced/reporters.md +0 -93
  155. package/vendor/vitest-docs/guide/advanced/tests.md +0 -125
  156. package/vendor/vitest-docs/guide/browser/aria-snapshots.md +0 -470
  157. package/vendor/vitest-docs/guide/browser/component-testing.md +0 -571
  158. package/vendor/vitest-docs/guide/browser/index.md +0 -630
  159. package/vendor/vitest-docs/guide/browser/multiple-setups.md +0 -121
  160. package/vendor/vitest-docs/guide/browser/trace-view.md +0 -126
  161. package/vendor/vitest-docs/guide/browser/visual-regression-testing.md +0 -734
  162. package/vendor/vitest-docs/guide/cli-generated.md +0 -972
  163. package/vendor/vitest-docs/guide/cli.md +0 -234
  164. package/vendor/vitest-docs/guide/common-errors.md +0 -163
  165. package/vendor/vitest-docs/guide/coverage.md +0 -515
  166. package/vendor/vitest-docs/guide/debugging.md +0 -127
  167. package/vendor/vitest-docs/guide/environment.md +0 -101
  168. package/vendor/vitest-docs/guide/extending-matchers.md +0 -160
  169. package/vendor/vitest-docs/guide/features.md +0 -310
  170. package/vendor/vitest-docs/guide/filtering.md +0 -175
  171. package/vendor/vitest-docs/guide/ide.md +0 -43
  172. package/vendor/vitest-docs/guide/improving-performance.md +0 -245
  173. package/vendor/vitest-docs/guide/in-source.md +0 -159
  174. package/vendor/vitest-docs/guide/index.md +0 -128
  175. package/vendor/vitest-docs/guide/learn/async.md +0 -147
  176. package/vendor/vitest-docs/guide/learn/debugging-tests.md +0 -210
  177. package/vendor/vitest-docs/guide/learn/matchers.md +0 -277
  178. package/vendor/vitest-docs/guide/learn/mock-functions.md +0 -277
  179. package/vendor/vitest-docs/guide/learn/setup-teardown.md +0 -240
  180. package/vendor/vitest-docs/guide/learn/snapshots.md +0 -166
  181. package/vendor/vitest-docs/guide/learn/testing-in-practice.md +0 -430
  182. package/vendor/vitest-docs/guide/learn/writing-tests-with-ai.md +0 -127
  183. package/vendor/vitest-docs/guide/learn/writing-tests.md +0 -231
  184. package/vendor/vitest-docs/guide/lifecycle.md +0 -379
  185. package/vendor/vitest-docs/guide/migration.md +0 -863
  186. package/vendor/vitest-docs/guide/mocking/classes.md +0 -158
  187. package/vendor/vitest-docs/guide/mocking/dates.md +0 -52
  188. package/vendor/vitest-docs/guide/mocking/file-system.md +0 -74
  189. package/vendor/vitest-docs/guide/mocking/functions.md +0 -61
  190. package/vendor/vitest-docs/guide/mocking/globals.md +0 -20
  191. package/vendor/vitest-docs/guide/mocking/modules.md +0 -414
  192. package/vendor/vitest-docs/guide/mocking/requests.md +0 -114
  193. package/vendor/vitest-docs/guide/mocking/timers.md +0 -48
  194. package/vendor/vitest-docs/guide/mocking.md +0 -239
  195. package/vendor/vitest-docs/guide/open-telemetry.md +0 -156
  196. package/vendor/vitest-docs/guide/parallelism.md +0 -82
  197. package/vendor/vitest-docs/guide/profiling-test-performance.md +0 -243
  198. package/vendor/vitest-docs/guide/projects.md +0 -291
  199. package/vendor/vitest-docs/guide/recipes.md +0 -59
  200. package/vendor/vitest-docs/guide/reporters.md +0 -723
  201. package/vendor/vitest-docs/guide/snapshot.md +0 -620
  202. package/vendor/vitest-docs/guide/test-annotations.md +0 -103
  203. package/vendor/vitest-docs/guide/test-context.md +0 -902
  204. package/vendor/vitest-docs/guide/test-tags.md +0 -314
  205. package/vendor/vitest-docs/guide/testing-types.md +0 -149
  206. package/vendor/vitest-docs/guide/ui.md +0 -160
  207. package/vendor/vitest-docs/guide/using-plugins.md +0 -5
  208. package/vendor/vitest-docs/manifest.json +0 -1691
@@ -1,463 +0,0 @@
1
- # Hooks
2
-
3
- These functions allow you to hook into the life cycle of tests to avoid repeating setup and teardown code. They apply to the current context: the file if they are used at the top-level or the current suite if they are inside a `describe` block. These hooks are not called, when you are running Vitest as a [type checker](/guide/testing-types).
4
-
5
- Test hooks are called in a stack order ("after" hooks are reversed) by default, but you can configure it via [`sequence.hooks`](/config/sequence#sequence-hooks) option.
6
-
7
- ## beforeEach
8
-
9
- ```ts
10
- function beforeEach(
11
- body: (context: TestContext) => unknown,
12
- timeout?: number,
13
- ): void
14
- ```
15
-
16
- Register a callback to be called before each of the tests in the current suite runs.
17
- If the function returns a promise, Vitest waits until the promise resolve before running the test.
18
-
19
- Optionally, you can pass a timeout (in milliseconds) defining how long to wait before terminating. The default is 10 seconds, and can be configured globally with [`hookTimeout`](/config/hooktimeout).
20
-
21
- ```ts
22
- import { beforeEach } from 'vitest'
23
-
24
- beforeEach(async () => {
25
- // Clear mocks and add some testing data before each test run
26
- await stopMocking()
27
- await addUser({ name: 'John' })
28
- })
29
- ```
30
-
31
- Here, the `beforeEach` ensures that user is added for each test.
32
-
33
- `beforeEach` can also return an optional cleanup function (equivalent to [`afterEach`](#aftereach)):
34
-
35
- ```ts
36
- import { beforeEach } from 'vitest'
37
-
38
- beforeEach(async () => {
39
- // called once before each test run
40
- await prepareSomething()
41
-
42
- // clean up function, called once after each test run
43
- return async () => {
44
- await resetSomething()
45
- }
46
- })
47
- ```
48
-
49
- ## afterEach
50
-
51
- ```ts
52
- function afterEach(
53
- body: (context: TestContext) => unknown,
54
- timeout?: number,
55
- ): void
56
- ```
57
-
58
- Register a callback to be called after each one of the tests in the current suite completes.
59
- If the function returns a promise, Vitest waits until the promise resolve before continuing.
60
-
61
- Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before terminating. The default is 10 seconds, and can be configured globally with [`hookTimeout`](/config/hooktimeout).
62
-
63
- ```ts
64
- import { afterEach } from 'vitest'
65
-
66
- afterEach(async () => {
67
- await clearTestingData() // clear testing data after each test run
68
- })
69
- ```
70
-
71
- Here, the `afterEach` ensures that testing data is cleared after each test runs.
72
-
73
- ::: tip
74
- You can also use [`onTestFinished`](#ontestfinished) during the test execution to cleanup any state after the test has finished running.
75
- :::
76
-
77
- ## beforeAll
78
-
79
- ```ts
80
- function beforeAll(
81
- body: (context: ModuleContext) => unknown,
82
- timeout?: number,
83
- ): void
84
- ```
85
-
86
- Register a callback to be called once before starting to run all tests in the current suite.
87
- If the function returns a promise, Vitest waits until the promise resolve before running tests.
88
-
89
- Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before terminating. The default is 10 seconds, and can be configured globally with [`hookTimeout`](/config/hooktimeout).
90
-
91
- ```ts
92
- import { beforeAll } from 'vitest'
93
-
94
- beforeAll(async () => {
95
- await startMocking() // called once before all tests run
96
- })
97
- ```
98
-
99
- Here the `beforeAll` ensures that the mock data is set up before tests run.
100
-
101
- `beforeAll` can also return an optional cleanup function (equivalent to [`afterAll`](#afterall)):
102
-
103
- ```ts
104
- import { beforeAll } from 'vitest'
105
-
106
- beforeAll(async () => {
107
- // called once before all tests run
108
- await startMocking()
109
-
110
- // clean up function, called once after all tests run
111
- return async () => {
112
- await stopMocking()
113
- }
114
- })
115
- ```
116
-
117
- ## afterAll
118
-
119
- ```ts
120
- function afterAll(
121
- body: (context: ModuleContext) => unknown,
122
- timeout?: number,
123
- ): void
124
- ```
125
-
126
- Register a callback to be called once after all tests have run in the current suite.
127
- If the function returns a promise, Vitest waits until the promise resolve before continuing.
128
-
129
- Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before terminating. The default is 10 seconds, and can be configured globally with [`hookTimeout`](/config/hooktimeout).
130
-
131
- ```ts
132
- import { afterAll } from 'vitest'
133
-
134
- afterAll(async () => {
135
- await stopMocking() // this method is called after all tests run
136
- })
137
- ```
138
-
139
- Here the `afterAll` ensures that `stopMocking` method is called after all tests run.
140
-
141
- ## aroundEach
142
-
143
- ```ts
144
- function aroundEach(
145
- body: (
146
- runTest: () => Promise<void>,
147
- context: TestContext,
148
- ) => Promise<void>,
149
- timeout?: number,
150
- ): void
151
- ```
152
-
153
- Register a callback function that wraps around each test within the current suite. The callback receives a `runTest` function that **must** be called to run the test.
154
-
155
- The `runTest()` function runs `beforeEach` hooks, the test itself, fixtures accessed in the test, and `afterEach` hooks. Fixtures that are accessed in the `aroundEach` callback are initialized before `runTest()` is called and are torn down after the aroundEach teardown code completes, allowing you to safely use them in both setup and teardown phases.
156
-
157
- ::: warning
158
- You **must** call `runTest()` within your callback. If `runTest()` is not called, the test will fail with an error.
159
- :::
160
-
161
- Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before terminating. The timeout applies independently to the setup phase (before `runTest()`) and teardown phase (after `runTest()`). The default is 10 seconds, and can be configured globally with [`hookTimeout`](/config/hooktimeout).
162
-
163
- ```ts
164
- import { aroundEach, test } from 'vitest'
165
-
166
- aroundEach(async (runTest) => {
167
- await db.transaction(runTest)
168
- })
169
-
170
- test('insert user', async () => {
171
- await db.insert({ name: 'Alice' })
172
- // transaction is automatically rolled back after the test
173
- })
174
- ```
175
-
176
- ::: tip When to use `aroundEach`
177
- Use `aroundEach` when your test needs to run **inside a context** that wraps around it, such as:
178
- - Wrapping tests in [AsyncLocalStorage](https://nodejs.org/api/async_context.html#class-asynclocalstorage) context
179
- - Wrapping tests with tracing spans
180
- - Database transactions
181
-
182
- If you just need to run code before and after tests, prefer using [`beforeEach`](#beforeeach) with a cleanup return function:
183
- ```ts
184
- beforeEach(async () => {
185
- await database.connect()
186
- return async () => {
187
- await database.disconnect()
188
- }
189
- })
190
- ```
191
- :::
192
-
193
- ### Multiple Hooks
194
-
195
- When multiple `aroundEach` hooks are registered, they are nested inside each other. The first registered hook is the outermost wrapper:
196
-
197
- ```ts
198
- aroundEach(async (runTest) => {
199
- console.log('outer before')
200
- await runTest()
201
- console.log('outer after')
202
- })
203
-
204
- aroundEach(async (runTest) => {
205
- console.log('inner before')
206
- await runTest()
207
- console.log('inner after')
208
- })
209
-
210
- // Output order:
211
- // outer before
212
- // inner before
213
- // test
214
- // inner after
215
- // outer after
216
- ```
217
-
218
- ### Context and Fixtures
219
-
220
- The callback receives the test context as the second argument which means that you can use fixtures with `aroundEach`:
221
-
222
- ```ts
223
- import { aroundEach, test as base } from 'vitest'
224
-
225
- const test = base.extend<{ db: Database; user: User }>({
226
- db: async ({}, use) => {
227
- // db is created before `aroundEach` hook
228
- const db = await createTestDatabase()
229
- await use(db)
230
- await db.close()
231
- },
232
- user: async ({ db }, use) => {
233
- // `user` runs as part of the transaction
234
- // because it's accessed inside the `test`
235
- const user = await db.createUser()
236
- await use(user)
237
- },
238
- })
239
-
240
- // note that `aroundEach` is available on test
241
- // for a better TypeScript support of fixtures
242
- test.aroundEach(async (runTest, { db }) => {
243
- await db.transaction(runTest)
244
- })
245
-
246
- test('insert user', async ({ db, user }) => {
247
- await db.insert(user)
248
- })
249
- ```
250
-
251
- ## aroundAll
252
-
253
- ```ts
254
- function aroundAll(
255
- body: (
256
- runSuite: () => Promise<void>,
257
- context: ModuleContext,
258
- ) => Promise<void>,
259
- timeout?: number,
260
- ): void
261
- ```
262
-
263
- Register a callback function that wraps around all tests within the current suite. The callback receives a `runSuite` function that **must** be called to run the suite's tests.
264
-
265
- The `runSuite()` function runs all tests in the suite, including `beforeAll`/`afterAll`/`beforeEach`/`afterEach` hooks, `aroundEach` hooks, and fixtures.
266
-
267
- ::: warning
268
- You **must** call `runSuite()` within your callback. If `runSuite()` is not called, the hook will fail with an error and all tests in the suite will be skipped.
269
- :::
270
-
271
- Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before terminating. The timeout applies independently to the setup phase (before `runSuite()`) and teardown phase (after `runSuite()`). The default is 10 seconds, and can be configured globally with [`hookTimeout`](/config/hooktimeout).
272
-
273
- ```ts
274
- import { aroundAll, test } from 'vitest'
275
-
276
- aroundAll(async (runSuite) => {
277
- await tracer.trace('test-suite', runSuite)
278
- })
279
-
280
- test('test 1', () => {
281
- // Runs within the tracing span
282
- })
283
-
284
- test('test 2', () => {
285
- // Also runs within the same tracing span
286
- })
287
- ```
288
-
289
- ::: tip When to use `aroundAll`
290
- Use `aroundAll` when your suite needs to run **inside a context** that wraps around all tests, such as:
291
- - Wrapping an entire suite in [AsyncLocalStorage](https://nodejs.org/api/async_context.html#class-asynclocalstorage) context
292
- - Wrapping a suite with tracing spans
293
- - Database transactions
294
-
295
- If you just need to run code once before and after all tests, prefer using [`beforeAll`](#beforeall) with a cleanup return function:
296
- ```ts
297
- beforeAll(async () => {
298
- await server.start()
299
- return async () => {
300
- await server.stop()
301
- }
302
- })
303
- ```
304
- :::
305
-
306
- ### Multiple Hooks
307
-
308
- When multiple `aroundAll` hooks are registered, they are nested inside each other. The first registered hook is the outermost wrapper:
309
-
310
- ```ts
311
- aroundAll(async (runSuite) => {
312
- console.log('outer before')
313
- await runSuite()
314
- console.log('outer after')
315
- })
316
-
317
- aroundAll(async (runSuite) => {
318
- console.log('inner before')
319
- await runSuite()
320
- console.log('inner after')
321
- })
322
-
323
- // Output order: outer before → inner before → tests → inner after → outer after
324
- ```
325
-
326
- Each suite has its own independent `aroundAll` hooks. Parent suite's `aroundAll` wraps around child suite's execution:
327
-
328
- ```ts
329
- import { AsyncLocalStorage } from 'node:async_hooks'
330
- import { aroundAll, describe, test } from 'vitest'
331
-
332
- const context = new AsyncLocalStorage<{ suiteId: string }>()
333
-
334
- aroundAll(async (runSuite) => {
335
- await context.run({ suiteId: 'root' }, runSuite)
336
- })
337
-
338
- test('root test', () => {
339
- // context.getStore() returns { suiteId: 'root' }
340
- })
341
-
342
- describe('nested', () => {
343
- aroundAll(async (runSuite) => {
344
- // Parent's context is available here
345
- await context.run({ suiteId: 'nested' }, runSuite)
346
- })
347
-
348
- test('nested test', () => {
349
- // context.getStore() returns { suiteId: 'nested' }
350
- })
351
- })
352
- ```
353
-
354
- ## Test Hooks
355
-
356
- Vitest provides a few hooks that you can call _during_ the test execution to cleanup the state when the test has finished running.
357
-
358
- ::: warning
359
- These hooks will throw an error if they are called outside of the test body.
360
- :::
361
-
362
- ### onTestFinished {#ontestfinished}
363
-
364
- This hook is always called after the test has finished running. It is called after `afterEach` hooks since they can influence the test result. It receives an `TestContext` object like `beforeEach` and `afterEach`.
365
-
366
- ```ts {1,5}
367
- import { onTestFinished, test } from 'vitest'
368
-
369
- test('performs a query', () => {
370
- const db = connectDb()
371
- onTestFinished(() => db.close())
372
- db.query('SELECT * FROM users')
373
- })
374
- ```
375
-
376
- ::: warning
377
- If you are running tests concurrently, you should always use `onTestFinished` hook from the test context since Vitest doesn't track concurrent tests in global hooks:
378
-
379
- ```ts {3,5}
380
- import { test } from 'vitest'
381
-
382
- test.concurrent('performs a query', ({ onTestFinished }) => {
383
- const db = connectDb()
384
- onTestFinished(() => db.close())
385
- db.query('SELECT * FROM users')
386
- })
387
- ```
388
- :::
389
-
390
- This hook is particularly useful when creating reusable logic:
391
-
392
- ```ts
393
- // this can be in a separate file
394
- function getTestDb() {
395
- const db = connectMockedDb()
396
- onTestFinished(() => db.close())
397
- return db
398
- }
399
-
400
- test('performs a user query', async () => {
401
- const db = getTestDb()
402
- expect(
403
- await db.query('SELECT * from users').perform()
404
- ).toEqual([])
405
- })
406
-
407
- test('performs an organization query', async () => {
408
- const db = getTestDb()
409
- expect(
410
- await db.query('SELECT * from organizations').perform()
411
- ).toEqual([])
412
- })
413
- ```
414
-
415
- It is also a good practice to cleanup your spies after each test, so they don't leak into other tests. You can do so by enabling [`restoreMocks`](/config/restoremocks) config globally, or restoring the spy inside `onTestFinished` (if you try to restore the mock at the end of the test, it won't be restored if one of the assertions fails - using `onTestFinished` ensures the code always runs):
416
-
417
- ```ts
418
- import { onTestFinished, test } from 'vitest'
419
-
420
- test('performs a query', () => {
421
- const spy = vi.spyOn(db, 'query')
422
- onTestFinished(() => spy.mockClear())
423
-
424
- db.query('SELECT * FROM users')
425
- expect(spy).toHaveBeenCalled()
426
- })
427
- ```
428
-
429
- ::: tip
430
- This hook is always called in reverse order and is not affected by [`sequence.hooks`](/config/sequence#sequence-hooks) option.
431
- :::
432
-
433
- ### onTestFailed
434
-
435
- This hook is called only after the test has failed. It is called after `afterEach` hooks since they can influence the test result. It receives a `TestContext` object like `beforeEach` and `afterEach`. This hook is useful for debugging.
436
-
437
- ```ts {1,5-7}
438
- import { onTestFailed, test } from 'vitest'
439
-
440
- test('performs a query', () => {
441
- const db = connectDb()
442
- onTestFailed(({ task }) => {
443
- console.log(task.result.errors)
444
- })
445
- db.query('SELECT * FROM users')
446
- })
447
- ```
448
-
449
- ::: warning
450
- If you are running tests concurrently, you should always use `onTestFailed` hook from the test context since Vitest doesn't track concurrent tests in global hooks:
451
-
452
- ```ts {3,5-7}
453
- import { test } from 'vitest'
454
-
455
- test.concurrent('performs a query', ({ onTestFailed }) => {
456
- const db = connectDb()
457
- onTestFailed(({ task }) => {
458
- console.log(task.result.errors)
459
- })
460
- db.query('SELECT * FROM users')
461
- })
462
- ```
463
- :::