@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,231 +0,0 @@
1
- # Writing Tests
2
-
3
- In the [Getting Started](/guide/) guide, you installed Vitest and ran your first test. This page dives deeper into how to write and organize tests in Vitest.
4
-
5
- ## Your First Test
6
-
7
- A test verifies that a piece of code produces the expected result. In Vitest, you use the [`test`](/api/test) function to define a test, and [`expect`](/api/expect) to make assertions. Each test has a name (a string describing what it checks) and a function that contains one or more assertions. If any assertion fails, the test fails.
8
-
9
- ```js
10
- import { expect, test } from 'vitest'
11
-
12
- test('Math.sqrt works for perfect squares', () => {
13
- expect(Math.sqrt(4)).toBe(2)
14
- expect(Math.sqrt(144)).toBe(12)
15
- expect(Math.sqrt(0)).toBe(0)
16
- })
17
- ```
18
-
19
- ::: details Use `test` or `it`?
20
- You might also see tests written with [`it`](/api/test) instead of `test`. They behave identically. `it` is just an alias that some people prefer because it reads more naturally with a descriptive name:
21
-
22
- ```js
23
- import { expect, it } from 'vitest'
24
-
25
- it('should compute square roots', () => {
26
- expect(Math.sqrt(4)).toBe(2)
27
- })
28
- ```
29
-
30
- Use whichever you prefer. Both work the same way, and you can mix them freely in a project. If you want to enforce a consistent choice across your codebase, the [`consistent-test-it`](https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/consistent-test-it.md) ESLint rule (also available in [oxlint](https://oxc.rs/docs/guide/usage/linter/rules/jest/consistent-test-it.html)) can help with that.
31
- :::
32
-
33
- ## Grouping Tests with `describe`
34
-
35
- As your test files grow, you'll want to organize related tests together. [`describe`](/api/describe) creates a test suite, which is a named group of tests:
36
-
37
- ```js
38
- import { describe, expect, test } from 'vitest'
39
-
40
- describe('Math.sqrt', () => {
41
- test('returns the square root of perfect squares', () => {
42
- expect(Math.sqrt(4)).toBe(2)
43
- expect(Math.sqrt(9)).toBe(3)
44
- })
45
-
46
- test('returns NaN for negative numbers', () => {
47
- expect(Math.sqrt(-1)).toBeNaN()
48
- })
49
-
50
- test('returns 0 for 0', () => {
51
- expect(Math.sqrt(0)).toBe(0)
52
- })
53
- })
54
- ```
55
-
56
- You can nest `describe` blocks for further organization, but keep nesting shallow. Deeply nested tests are harder to read. A flat list of tests is often enough for simple modules, and `describe` becomes more useful when a file tests multiple functions or methods that each need their own group.
57
-
58
- ## Test Files
59
-
60
- By default, Vitest looks for any file that contains `.test.` or `.spec.` in its name, such as `utils.test.js`, `app.spec.js`, or `math.test.jsx`. It searches in all subdirectories, so it doesn't matter where you place them.
61
-
62
- The exact patterns are:
63
-
64
- - `**/*.test.{ts,js,mjs,cjs,tsx,jsx}`
65
- - `**/*.spec.{ts,js,mjs,cjs,tsx,jsx}`
66
-
67
- There's no single "right" way to organize your test files. Some teams prefer placing tests right next to the source code they test, while others keep them in a dedicated directory. Vitest will find them either way:
68
-
69
- ```
70
- src/
71
- utils.js
72
- utils.test.js # co-located with the source
73
- __tests__/
74
- utils.test.js # in a test directory
75
- ```
76
-
77
- If the default patterns don't work for your project, you can customize which files are included with the [`include`](/config/include) and [`exclude`](/config/exclude) config options.
78
-
79
- ## Testing TypeScript
80
-
81
- Because Vitest runs on top of Vite, TypeScript works out of the box. There's no extra compiler to install, no `ts-jest` to configure, and no separate build step for your tests. Just name your test file `.test.ts` instead of `.test.js` and start writing:
82
-
83
- ```ts
84
- import { expect, test } from 'vitest'
85
-
86
- interface User {
87
- name: string
88
- age: number
89
- }
90
-
91
- function createUser(name: string, age: number): User {
92
- return { name, age }
93
- }
94
-
95
- test('creates a user with the correct fields', () => {
96
- const user = createUser('Alice', 30)
97
-
98
- expect(user).toEqual({ name: 'Alice', age: 30 })
99
- expect(user.name).toBe('Alice')
100
- })
101
- ```
102
-
103
- You can import your production types, use generics, and write typed test utilities exactly as you would in the rest of your codebase. Vite transforms TypeScript on the fly, so tests start fast even in large projects.
104
-
105
- ::: tip
106
- Vitest transforms TypeScript for execution but does **not** type-check your tests during the test run. This is the same trade-off Vite makes for speed: you get fast feedback in the terminal, and run `tsc` or `vitest typecheck` separately when you want full type checking. See the [Testing Types](/guide/testing-types) guide for more details.
107
- :::
108
-
109
- ## Reading Test Output
110
-
111
- When you run `vitest` and only a single test file matches, the output is expanded into a tree structure showing `describe` groups and individual tests along with their duration:
112
-
113
- <<< ./snippets/test-output-single.ansi
114
-
115
- When multiple test files run, Vitest collapses each file into a single line to keep the output manageable:
116
-
117
- <<< ./snippets/test-output-multiple.ansi
118
-
119
- When a test fails, Vitest shows you exactly what went wrong. You'll see the expected value, the actual value, a diff highlighting the difference, and a code snippet of the surrounding lines with the failing assertion highlighted. It also includes the file and line number so you can jump straight to the source:
120
-
121
- <<< ./snippets/test-output-fail.ansi
122
-
123
- Between the diff and the code snippet, you can usually understand what went wrong without needing to add extra `console.log` statements or open the file yourself.
124
-
125
- ## Skipping and Focusing Tests
126
-
127
- While developing, you'll often want to run only a subset of tests. Vitest provides modifiers for this:
128
-
129
- [`.only`](/api/test#only) tells Vitest to run only this test (or suite) and skip everything else in the file. This is useful when you're working on a specific test and don't want to wait for the entire suite to finish:
130
-
131
- ```js
132
- test.only('focus on this test', () => {
133
- // only this test runs in the file
134
- })
135
- ```
136
-
137
- [`.skip`](/api/test#skip) does the opposite. It skips a test without removing it, which is handy when a test is temporarily broken or you want to ignore it while you work on something else:
138
-
139
- ```js
140
- test.skip('not ready yet', () => {
141
- // this test is skipped
142
- })
143
- ```
144
-
145
- [`.todo`](/api/test#todo) lets you mark a placeholder for a test you haven't written yet. Vitest will list it in the output so you won't forget about it:
146
-
147
- ```js
148
- test.todo('implement validation later')
149
- ```
150
-
151
- These modifiers are great for quick, local changes while developing. For more permanent ways to filter tests (by filename, line number, or tags), see the [Test Filtering](/guide/filtering) guide.
152
-
153
- ## Parameterized Tests
154
-
155
- When you have several test cases that only differ in their inputs and expected outputs, writing a separate `test` for each one gets repetitive. [`test.for`](/api/test#test-for) lets you define the cases as data and run the same test logic for all of them:
156
-
157
- ```js
158
- import { expect, test } from 'vitest'
159
-
160
- test.for([
161
- [1, 1, 2],
162
- [1, 2, 3],
163
- [2, 1, 3],
164
- ])('add(%i, %i) -> %i', ([a, b, expected]) => {
165
- expect(a + b).toBe(expected)
166
- })
167
- ```
168
-
169
- The placeholders `%i`, `%s`, and `%f` in the test name are replaced with the corresponding values from each row, so the output shows `add(1, 1) -> 2`, `add(1, 2) -> 3`, and so on.
170
-
171
- If your cases have more than two or three values, passing objects is more readable. Use `$property` in the name to interpolate fields:
172
-
173
- ```js
174
- test.for([
175
- { a: 1, b: 1, expected: 2 },
176
- { a: 1, b: 2, expected: 3 },
177
- { a: 2, b: 1, expected: 3 },
178
- ])('add($a, $b) -> $expected', ({ a, b, expected }) => {
179
- expect(a + b).toBe(expected)
180
- })
181
- ```
182
-
183
- The second argument to the test function is the [Test Context](/guide/test-context), which gives you access to fixtures, per-test `expect`, and other utilities. This is especially useful with [`test.concurrent`](/api/test#concurrent), where concurrent tests run in parallel and the global `expect` can't reliably associate a snapshot with the right test. The context-scoped `expect` solves this:
184
-
185
- ```js
186
- test.concurrent.for([
187
- [1, 1],
188
- [1, 2],
189
- [2, 1],
190
- ])('add(%i, %i)', ([a, b], { expect }) => {
191
- expect(a + b).toMatchSnapshot()
192
- })
193
- ```
194
-
195
- [`describe.for`](/api/describe#describe-for) works the same way but creates a suite for each set of parameters, which is useful when multiple tests share the same parameterized setup.
196
-
197
- ::: tip
198
- Vitest also provides [`test.each`](/api/test#each), which you may recognize from Jest. It works similarly but spreads array arguments instead of passing them as a single value, and doesn't provide access to the Test Context. It exists mainly for Jest compatibility. Prefer `test.for` in new code.
199
- :::
200
-
201
- ## Using Global Imports
202
-
203
- By default, you import `test`, `expect`, `describe`, and other functions from `vitest` at the top of every test file. If you'd rather use them as globals without importing (similar to how Jest works), you can enable the [`globals`](/config/globals) option in your config:
204
-
205
- ```js [vitest.config.js]
206
- import { defineConfig } from 'vitest/config'
207
-
208
- export default defineConfig({
209
- test: {
210
- globals: true,
211
- },
212
- })
213
- ```
214
-
215
- With this enabled, you can write tests without the import line:
216
-
217
- ```js
218
- test('no import needed', () => {
219
- expect(1 + 1).toBe(2)
220
- })
221
- ```
222
-
223
- ::: tip
224
- If you use TypeScript, add `"types": ["vitest/globals"]` to your `tsconfig.json` `compilerOptions` for proper type support.
225
- :::
226
-
227
- ## Running Tests
228
-
229
- Vitest runs all test files **in parallel** by default, using [child processes](/config/pool). Each test file runs in its own isolated context, so your test files don't share state with each other. This prevents tests in different files from accidentally interfering.
230
-
231
- Tests **within** a single file run sequentially by default, which is usually what you want since tests in the same file often share setup code. If your tests are truly independent, you can opt into running them concurrently with [`test.concurrent`](/api/test#concurrent) to speed things up. See the [Parallelism](/guide/parallelism) guide for more details on controlling test execution.
@@ -1,379 +0,0 @@
1
- # Test Run Lifecycle
2
-
3
- ::: tip
4
- Looking for a practical introduction to `beforeEach`, `afterEach`, and other hooks? See the [Setup and Teardown](/guide/learn/setup-teardown) tutorial.
5
- :::
6
-
7
- Understanding the test run lifecycle is essential for writing effective tests, debugging issues, and optimizing your test suite. This guide explains when and in what order different lifecycle phases occur in Vitest, from initialization to teardown.
8
-
9
- ## Overview
10
-
11
- A typical Vitest test run goes through these main phases:
12
-
13
- 1. **Initialization:** Configuration loading and project setup
14
- 2. **Global Setup:** One-time setup before any tests run
15
- 3. **Worker Creation:** Test workers are spawned based on the [pool](/config/pool) configuration
16
- 4. **Test File Collection:** Test files are discovered and organized
17
- 5. **Test Execution:** Tests run with their hooks and assertions
18
- 6. **Reporting:** Results are collected and reported
19
- 7. **Global Teardown:** Final cleanup after all tests complete
20
-
21
- Phases 4–6 run once for each test file, so across your test suite they will execute multiple times and may also run in parallel across different files when you use more than [1 worker](/config/maxworkers).
22
-
23
- ## Detailed Lifecycle Phases
24
-
25
- ### 1. Initialization Phase
26
-
27
- When you run `vitest`, the framework first loads your configuration and prepares the test environment.
28
-
29
- **What happens:**
30
- - [Command-line](/guide/cli) arguments are parsed
31
- - [Configuration file](/config/) is loaded
32
- - Project structure is validated
33
-
34
- This phase can run again if the config file or one of its imports changes.
35
-
36
- **Scope:** Main process (before any test workers are created)
37
-
38
- ### 2. Global Setup Phase
39
-
40
- If you have configured [`globalSetup`](/config/globalsetup) files, they run once before any test workers are created.
41
-
42
- **What happens:**
43
- - `setup()` functions (or exported `default` function) from global setup files execute sequentially
44
- - Multiple global setup files run in the order they are defined
45
-
46
- **Scope:** Main process (separate from test workers)
47
-
48
- **Important notes:**
49
- - Global setup runs in a **different global scope** from your tests
50
- - Tests cannot access variables defined in global setup (use [`provide`/`inject`](/config/provide) instead)
51
- - Global setup only runs if there is at least one test queued
52
-
53
- ```ts [globalSetup.ts]
54
- export function setup(project) {
55
- // Runs once before all tests
56
- console.log('Global setup')
57
-
58
- // Share data with tests
59
- project.provide('apiUrl', 'http://localhost:3000')
60
- }
61
-
62
- export function teardown() {
63
- // Runs once after all tests
64
- console.log('Global teardown')
65
- }
66
- ```
67
-
68
- ### 3. Worker Creation Phase
69
-
70
- After global setup completes, Vitest creates test workers based on your [pool configuration](/config/pool).
71
-
72
- **What happens:**
73
- - Workers are spawned according to the `browser.enabled` or `pool` setting (`threads`, `forks`, `vmThreads`, or `vmForks`)
74
- - Each worker gets its own isolated environment (unless [isolation](/config/isolate) is disabled)
75
- - By default, workers are not reused to provide isolation. Workers are reused only if:
76
- - [isolation](/config/isolate) is disabled
77
- - OR pool is `vmThreads` or `vmForks` because [VM](https://nodejs.org/api/vm.html) provides enough isolation
78
-
79
- **Scope:** Worker processes/threads
80
-
81
- ### 4. Test File Setup Phase
82
-
83
- Before each test file runs, [setup files](/config/setupfiles) are executed.
84
-
85
- **What happens:**
86
- - Setup files run in the same process as your tests
87
- - By default, setup files run in **parallel** (configurable via [`sequence.setupFiles`](/config/sequence#sequence-setupfiles))
88
- - Setup files execute before **each test file**
89
- - Any global _state_ or configuration can be initialized here
90
-
91
- **Scope:** Worker process (same as your tests)
92
-
93
- **Important notes:**
94
- - If [isolation](/config/isolate) is disabled, setup files still rerun before each test file to trigger side effects, but imported modules are cached
95
- - Editing a setup file triggers a rerun of all tests in watch mode
96
-
97
- ```ts [setupFile.ts]
98
- import { afterEach } from 'vitest'
99
-
100
- // Runs before each test file
101
- console.log('Setup file executing')
102
-
103
- // Register hooks that apply to all tests
104
- afterEach(() => {
105
- cleanup()
106
- })
107
- ```
108
-
109
- ### 5. Test Collection and Execution Phase
110
-
111
- This is the main phase where your tests actually run.
112
-
113
- #### Test File Execution Order
114
-
115
- Test files are executed based on your configuration:
116
-
117
- - **Sequential by default** within a worker
118
- - Files will run in **parallel** across different workers, configured by [`maxWorkers`](/config/maxworkers)
119
- - Order can be randomized with [`sequence.shuffle`](/config/sequence#sequence-shuffle) or fine-tuned with [`sequence.sequencer`](/config/sequence#sequence-sequencer)
120
- - Long-running tests typically start earlier (based on cache) unless shuffle is enabled
121
-
122
- #### Within Each Test File
123
-
124
- The execution follows this order:
125
-
126
- 1. **File-level code:** All code outside `describe` blocks runs immediately
127
- 2. **Test collection:** `describe` blocks are processed, and tests are registered as side effects of importing the test file
128
- 3. **[`aroundAll`](/api/hooks#aroundall) hooks:** Wrap around all tests in the suite (must call `runSuite()`)
129
- 4. **[`beforeAll`](/api/hooks#beforeall) hooks:** Run once before any tests in the suite
130
- 5. **For each test:**
131
- - [`aroundEach`](/api/hooks#aroundeach) hooks wrap around the test (must call `runTest()`)
132
- - `beforeEach` hooks execute (in order defined, or based on [`sequence.hooks`](/config/sequence#sequence-hooks))
133
- - Test function executes
134
- - `afterEach` hooks execute (reverse order by default with `sequence.hooks: 'stack'`)
135
- - [`onTestFinished`](/api/hooks#ontestfinished) callbacks run (always in reverse order)
136
- - If test failed: [`onTestFailed`](/api/hooks#ontestfailed) callbacks run
137
- - Note: if `repeats` or `retry` are set, all of these steps are executed again
138
- 6. **[`afterAll`](/api/hooks#afterall) hooks:** Run once after all tests in the suite complete
139
-
140
- **Example execution flow:**
141
-
142
- ```ts
143
- // This runs immediately (collection phase)
144
- console.log('File loaded')
145
-
146
- describe('User API', () => {
147
- // This runs immediately (collection phase)
148
- console.log('Suite defined')
149
-
150
- aroundAll(async (runSuite) => {
151
- // Wraps around all tests in this suite
152
- console.log('aroundAll before')
153
- await runSuite()
154
- console.log('aroundAll after')
155
- })
156
-
157
- beforeAll(() => {
158
- // Runs once before all tests in this suite
159
- console.log('beforeAll')
160
- })
161
-
162
- aroundEach(async (runTest) => {
163
- // Wraps around each test
164
- console.log('aroundEach before')
165
- await runTest()
166
- console.log('aroundEach after')
167
- })
168
-
169
- beforeEach(() => {
170
- // Runs before each test
171
- console.log('beforeEach')
172
- })
173
-
174
- test('creates user', () => {
175
- // Test executes
176
- console.log('test 1')
177
- })
178
-
179
- test('updates user', () => {
180
- // Test executes
181
- console.log('test 2')
182
- })
183
-
184
- afterEach(() => {
185
- // Runs after each test
186
- console.log('afterEach')
187
- })
188
-
189
- afterAll(() => {
190
- // Runs once after all tests in this suite
191
- console.log('afterAll')
192
- })
193
- })
194
-
195
- // Output:
196
- // File loaded
197
- // Suite defined
198
- // aroundAll before
199
- // beforeAll
200
- // aroundEach before
201
- // beforeEach
202
- // test 1
203
- // afterEach
204
- // aroundEach after
205
- // aroundEach before
206
- // beforeEach
207
- // test 2
208
- // afterEach
209
- // aroundEach after
210
- // afterAll
211
- // aroundAll after
212
- ```
213
-
214
- #### Nested Suites
215
-
216
- When using nested `describe` blocks, hooks follow a hierarchical pattern. The `aroundAll` and `aroundEach` hooks wrap around their respective scopes, with parent hooks wrapping child hooks:
217
-
218
- ```ts
219
- describe('outer', () => {
220
- aroundAll(async (runSuite) => {
221
- console.log('outer aroundAll before')
222
- await runSuite()
223
- console.log('outer aroundAll after')
224
- })
225
-
226
- beforeAll(() => console.log('outer beforeAll'))
227
-
228
- aroundEach(async (runTest) => {
229
- console.log('outer aroundEach before')
230
- await runTest()
231
- console.log('outer aroundEach after')
232
- })
233
-
234
- beforeEach(() => console.log('outer beforeEach'))
235
-
236
- test('outer test', () => console.log('outer test'))
237
-
238
- describe('inner', () => {
239
- aroundAll(async (runSuite) => {
240
- console.log('inner aroundAll before')
241
- await runSuite()
242
- console.log('inner aroundAll after')
243
- })
244
-
245
- beforeAll(() => console.log('inner beforeAll'))
246
-
247
- aroundEach(async (runTest) => {
248
- console.log('inner aroundEach before')
249
- await runTest()
250
- console.log('inner aroundEach after')
251
- })
252
-
253
- beforeEach(() => console.log('inner beforeEach'))
254
-
255
- test('inner test', () => console.log('inner test'))
256
-
257
- afterEach(() => console.log('inner afterEach'))
258
- afterAll(() => console.log('inner afterAll'))
259
- })
260
-
261
- afterEach(() => console.log('outer afterEach'))
262
- afterAll(() => console.log('outer afterAll'))
263
- })
264
-
265
- // Output:
266
- // outer aroundAll before
267
- // outer beforeAll
268
- // outer aroundEach before
269
- // outer beforeEach
270
- // outer test
271
- // outer afterEach
272
- // outer aroundEach after
273
- // inner aroundAll before
274
- // inner beforeAll
275
- // outer aroundEach before
276
- // inner aroundEach before
277
- // outer beforeEach
278
- // inner beforeEach
279
- // inner test
280
- // inner afterEach
281
- // outer afterEach
282
- // inner aroundEach after
283
- // outer aroundEach after
284
- // inner afterAll
285
- // inner aroundAll after
286
- // outer afterAll
287
- // outer aroundAll after
288
- ```
289
-
290
- #### Concurrent Tests
291
-
292
- When using `test.concurrent` or [`sequence.concurrent`](/config/sequence#sequence-concurrent):
293
-
294
- - Tests within the same file can run in parallel
295
- - Each concurrent test still runs its own `beforeEach` and `afterEach` hooks
296
- - Use [test context](/guide/test-context) for concurrent snapshots: `test.concurrent('name', async ({ expect }) => {})`
297
-
298
- ### 6. Reporting Phase
299
-
300
- Throughout the test run, reporters receive lifecycle events and display results.
301
-
302
- **What happens:**
303
- - Reporters receive events as tests progress
304
- - Results are collected and formatted
305
- - Test summaries are generated
306
- - Coverage reports are generated (if enabled)
307
-
308
- For detailed information about the reporter lifecycle, see the [Reporters](/api/advanced/reporters) guide.
309
-
310
- ### 7. Global Teardown Phase
311
-
312
- After all tests complete, global teardown functions execute.
313
-
314
- **What happens:**
315
- - `teardown()` functions from [`globalSetup`](/config/globalsetup) files run
316
- - Multiple teardown functions run in **reverse order** of their setup
317
- - In watch mode, teardown runs before process exit, not between test reruns
318
-
319
- **Scope:** Main process
320
-
321
- ```ts [globalSetup.ts]
322
- export function teardown() {
323
- // Clean up global resources
324
- console.log('Global teardown complete')
325
- }
326
- ```
327
-
328
- ## Lifecycle in Different Scopes
329
-
330
- Understanding where code executes is crucial for avoiding common pitfalls:
331
-
332
- | Phase | Scope | Access to Test Context | Runs |
333
- |-------|-------|----------------------|------|
334
- | Config File | Main process | ❌ No | Once per Vitest run |
335
- | Global Setup | Main process | ❌ No (use `provide`/`inject`) | Once per Vitest run |
336
- | Setup Files | Worker (same as tests) | ✅ Yes | Before each test file |
337
- | File-level code | Worker | ✅ Yes | Once per test file |
338
- | `aroundAll` | Worker | ✅ Yes | Once per suite (wraps all tests) |
339
- | `beforeAll` / `afterAll` | Worker | ✅ Yes | Once per suite |
340
- | `aroundEach` | Worker | ✅ Yes | Per test (wraps each test) |
341
- | `beforeEach` / `afterEach` | Worker | ✅ Yes | Per test |
342
- | Test function | Worker | ✅ Yes | Once (or more with retries/repeats) |
343
- | Global Teardown | Main process | ❌ No | Once per Vitest run |
344
-
345
- ## Watch Mode Lifecycle
346
-
347
- In watch mode, the lifecycle repeats with some differences:
348
-
349
- 1. **Initial run:** Full lifecycle as described above
350
- 2. **On file change:**
351
- - New [test run](/api/advanced/reporters#ontestrunstart) starts
352
- - Only affected test files are re-run
353
- - [Setup files](/config/setupfiles) run again for those test files
354
- - [Global setup](/config/globalsetup) does **not** re-run (use [`project.onTestsRerun`](/config/globalsetup#handling-test-reruns) for rerun-specific logic)
355
- 3. **On exit:**
356
- - Global teardown executes
357
- - Process terminates
358
-
359
- ## Performance Considerations
360
-
361
- Understanding the lifecycle helps optimize test performance:
362
-
363
- - **Global setup** is ideal for expensive one-time operations (database seeding, server startup)
364
- - **Setup files** run before each test file - avoid heavy operations here if you have many test files
365
- - **`beforeAll`** is better than `beforeEach` for expensive setup that doesn't need isolation
366
- - **Disabling [isolation](/config/isolate)** improves performance, but setup files still execute before each file
367
- - **[Pool configuration](/config/pool)** affects parallelization and available APIs
368
-
369
- For tips on how to improve performance, read the [Improving Performance](/guide/improving-performance) guide.
370
-
371
- ## Related Documentation
372
-
373
- - [Global Setup Configuration](/config/globalsetup)
374
- - [Setup Files Configuration](/config/setupfiles)
375
- - [Test Sequencing Options](/config/sequence)
376
- - [Isolation Configuration](/config/isolate)
377
- - [Pool Configuration](/config/pool)
378
- - [Extending Reporters](/guide/advanced/reporters) - for reporter lifecycle events
379
- - [Test API Reference](/api/hooks) - for hook APIs