@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,630 +0,0 @@
1
- # Browser Mode {#browser-mode}
2
-
3
- This page provides information about the browser mode feature in the Vitest API, which allows you to run your tests in the browser natively, providing access to browser globals like window and document.
4
-
5
- ::: tip
6
- If you are looking for documentation for `expect`, `vi` or any general API like test projects or type testing, refer to the ["Getting Started" guide](/guide/).
7
- :::
8
-
9
- <img alt="Vitest UI" img-light src="/ui-browser-1-light.png">
10
- <img alt="Vitest UI" img-dark src="/ui-browser-1-dark.png">
11
-
12
- ## Installation
13
-
14
- For easier setup, you can use `vitest init browser` command to install required dependencies and create browser configuration.
15
-
16
- ::: code-group
17
- ```bash [npm]
18
- npx vitest init browser
19
- ```
20
- ```bash [yarn]
21
- yarn exec vitest init browser
22
- ```
23
- ```bash [pnpm]
24
- pnpx vitest init browser
25
- ```
26
- ```bash [bun]
27
- bunx vitest init browser
28
- ```
29
- :::
30
-
31
- ### Manual Installation
32
-
33
- You can also install packages manually. Vitest always requires a provider to be defined. You can chose either [`preview`](/config/browser/preview), [`playwright`](/config/browser/playwright) or [`webdriverio`](/config/browser/webdriverio).
34
-
35
- If you want to just preview how your tests look, you can use the `preview` provider:
36
-
37
- ::: code-group
38
- ```bash [npm]
39
- npm install -D vitest @vitest/browser-preview
40
- ```
41
- ```bash [yarn]
42
- yarn add -D vitest @vitest/browser-preview
43
- ```
44
- ```bash [pnpm]
45
- pnpm add -D vitest @vitest/browser-preview
46
- ```
47
- ```bash [bun]
48
- bun add -D vitest @vitest/browser-preview
49
- ```
50
- :::
51
-
52
- ::: warning
53
- However, to run tests in CI you need to install either [`playwright`](https://npmx.dev/package/playwright) or [`webdriverio`](https://npmx.dev/package/webdriverio). We also recommend switching to either one of them for testing locally instead of using the default `preview` provider since it relies on simulating events instead of using Chrome DevTools Protocol.
54
-
55
- If you don't already use one of these tools, we recommend starting with Playwright because it supports parallel execution, which makes your tests run faster.
56
-
57
- ::: tabs key:provider
58
- == Playwright
59
- [Playwright](https://npmx.dev/package/playwright) is a framework for Web Testing and Automation.
60
-
61
- ::: code-group
62
- ```bash [npm]
63
- npm install -D vitest @vitest/browser-playwright
64
- ```
65
- ```bash [yarn]
66
- yarn add -D vitest @vitest/browser-playwright
67
- ```
68
- ```bash [pnpm]
69
- pnpm add -D vitest @vitest/browser-playwright
70
- ```
71
- ```bash [bun]
72
- bun add -D vitest @vitest/browser-playwright
73
- ```
74
- == WebdriverIO
75
-
76
- [WebdriverIO](https://npmx.dev/package/webdriverio) allows you to run tests locally using the WebDriver protocol.
77
-
78
- ::: code-group
79
- ```bash [npm]
80
- npm install -D vitest @vitest/browser-webdriverio
81
- ```
82
- ```bash [yarn]
83
- yarn add -D vitest @vitest/browser-webdriverio
84
- ```
85
- ```bash [pnpm]
86
- pnpm add -D vitest @vitest/browser-webdriverio
87
- ```
88
- ```bash [bun]
89
- bun add -D vitest @vitest/browser-webdriverio
90
- ```
91
- :::
92
-
93
- ## Configuration
94
-
95
- To activate browser mode in your Vitest configuration, set the `browser.enabled` field to `true` in your Vitest configuration file. Here is an example configuration using the browser field:
96
-
97
- ```ts [vitest.config.ts]
98
- import { defineConfig } from 'vitest/config'
99
- import { playwright } from '@vitest/browser-playwright'
100
-
101
- export default defineConfig({
102
- test: {
103
- browser: {
104
- provider: playwright(),
105
- enabled: true,
106
- // at least one instance is required
107
- instances: [
108
- { browser: 'chromium' },
109
- ],
110
- },
111
- }
112
- })
113
- ```
114
-
115
- ::: info
116
- Vitest assigns port `63315` to avoid conflicts with the development server, allowing you to run both in parallel. You can change that with the [`browser.api`](/config/browser/api) option.
117
-
118
- The CLI does not print the Vite server URL automatically. You can press "b" to print the URL when running in watch mode.
119
- :::
120
-
121
- If you have not used Vite before, make sure you have your framework's plugin installed and specified in the config. Some frameworks might require extra configuration to work - check their Vite related documentation to be sure.
122
-
123
- ::: code-group
124
- ```ts [react]
125
- import { defineConfig } from 'vitest/config'
126
- import react from '@vitejs/plugin-react'
127
- import { playwright } from '@vitest/browser-playwright'
128
-
129
- export default defineConfig({
130
- plugins: [react()],
131
- test: {
132
- browser: {
133
- enabled: true,
134
- provider: playwright(),
135
- instances: [
136
- { browser: 'chromium' },
137
- ],
138
- }
139
- }
140
- })
141
- ```
142
- ```ts [vue]
143
- import { defineConfig } from 'vitest/config'
144
- import { playwright } from '@vitest/browser-playwright'
145
- import vue from '@vitejs/plugin-vue'
146
-
147
- export default defineConfig({
148
- plugins: [vue()],
149
- test: {
150
- browser: {
151
- enabled: true,
152
- provider: playwright(),
153
- instances: [
154
- { browser: 'chromium' },
155
- ],
156
- }
157
- }
158
- })
159
- ```
160
- ```ts [svelte]
161
- import { defineConfig } from 'vitest/config'
162
- import { svelte } from '@sveltejs/vite-plugin-svelte'
163
- import { playwright } from '@vitest/browser-playwright'
164
-
165
- export default defineConfig({
166
- plugins: [svelte()],
167
- test: {
168
- browser: {
169
- enabled: true,
170
- provider: playwright(),
171
- instances: [
172
- { browser: 'chromium' },
173
- ],
174
- }
175
- }
176
- })
177
- ```
178
- ```ts [solid]
179
- import { defineConfig } from 'vitest/config'
180
- import solidPlugin from 'vite-plugin-solid'
181
- import { playwright } from '@vitest/browser-playwright'
182
-
183
- export default defineConfig({
184
- plugins: [solidPlugin()],
185
- test: {
186
- browser: {
187
- enabled: true,
188
- provider: playwright(),
189
- instances: [
190
- { browser: 'chromium' },
191
- ],
192
- }
193
- }
194
- })
195
- ```
196
- ```ts [marko]
197
- import { defineConfig } from 'vitest/config'
198
- import marko from '@marko/vite'
199
- import { playwright } from '@vitest/browser-playwright'
200
-
201
- export default defineConfig({
202
- plugins: [marko()],
203
- test: {
204
- browser: {
205
- enabled: true,
206
- provider: playwright(),
207
- instances: [
208
- { browser: 'chromium' },
209
- ],
210
- }
211
- }
212
- })
213
- ```
214
- ```ts [qwik]
215
- import { defineConfig } from 'vitest/config'
216
- import { qwikVite } from '@builder.io/qwik/optimizer'
217
- import { playwright } from '@vitest/browser-playwright'
218
-
219
- // optional, run the tests in SSR mode
220
- import { testSSR } from 'vitest-browser-qwik/ssr-plugin'
221
-
222
- export default defineConfig({
223
- plugins: [testSSR(), qwikVite()],
224
- test: {
225
- browser: {
226
- enabled: true,
227
- provider: playwright(),
228
- instances: [{ browser: 'chromium' }]
229
- },
230
- },
231
- })
232
- ```
233
- :::
234
-
235
- If you need to run some tests using Node-based runner, you can define a [`projects`](/guide/projects) option with separate configurations for different testing strategies:
236
-
237
- {#projects-config}
238
-
239
- ```ts [vitest.config.ts]
240
- import { defineConfig } from 'vitest/config'
241
- import { playwright } from '@vitest/browser-playwright'
242
-
243
- export default defineConfig({
244
- test: {
245
- projects: [
246
- {
247
- test: {
248
- // an example of file based convention,
249
- // you don't have to follow it
250
- include: [
251
- 'tests/unit/**/*.{test,spec}.ts',
252
- 'tests/**/*.unit.{test,spec}.ts',
253
- ],
254
- name: 'unit',
255
- environment: 'node',
256
- },
257
- },
258
- {
259
- test: {
260
- // an example of file based convention,
261
- // you don't have to follow it
262
- include: [
263
- 'tests/browser/**/*.{test,spec}.ts',
264
- 'tests/**/*.browser.{test,spec}.ts',
265
- ],
266
- name: 'browser',
267
- browser: {
268
- enabled: true,
269
- provider: playwright(),
270
- instances: [
271
- { browser: 'chromium' },
272
- ],
273
- },
274
- },
275
- },
276
- ],
277
- },
278
- })
279
- ```
280
-
281
- ## Browser Option Types
282
-
283
- The browser option in Vitest depends on the provider. Vitest will fail, if you pass `--browser` and don't specify its name in the config file. Available options:
284
-
285
- - `webdriverio` supports these browsers:
286
- - `firefox`
287
- - `chrome`
288
- - `edge`
289
- - `safari`
290
- - `playwright` supports these browsers:
291
- - `firefox`
292
- - `webkit`
293
- - `chromium`
294
-
295
- ## Browser Compatibility
296
-
297
- Vitest uses [Vite dev server](https://vitejs.dev/guide/#browser-support) to run your tests, so we only support features specified in the [`esbuild.target`](https://vitejs.dev/config/shared-options.html#esbuild) option (`esnext` by default).
298
-
299
- By default, Vite targets browsers which support the native [ES Modules](https://caniuse.com/es6-module), native [ESM dynamic import](https://caniuse.com/es6-module-dynamic-import), and [`import.meta`](https://caniuse.com/mdn-javascript_operators_import_meta). On top of that, we utilize [`BroadcastChannel`](https://caniuse.com/?search=BroadcastChannel) to communicate between iframes:
300
-
301
- - Chrome >=87
302
- - Firefox >=78
303
- - Safari >=15.4
304
- - Edge >=88
305
-
306
- ## Running Tests
307
-
308
- When you specify a browser name in the browser option, Vitest will try to run the specified browser using `preview` by default, and then run the tests there. If you don't want to use `preview`, you can configure the custom browser provider by using `browser.provider` option.
309
-
310
- To specify a browser using the CLI, use the `--browser` flag followed by the browser name, like this:
311
-
312
- ```sh
313
- npx vitest --browser=chromium
314
- ```
315
-
316
- Or you can provide browser options to CLI with dot notation:
317
-
318
- ```sh
319
- npx vitest --browser.headless
320
- ```
321
-
322
- ::: warning
323
- Since Vitest 3.2, if you don't have the `browser` option in your config but specify the `--browser` flag, Vitest will fail because it can't assume that config is meant for the browser and not Node.js tests.
324
- :::
325
-
326
- By default, Vitest will automatically open the browser UI for development. Your tests will run inside an iframe in the center. You can configure the viewport by selecting the preferred dimensions, calling `page.viewport` inside the test, or setting default values in [the config](/config/browser/viewport).
327
-
328
- ## Headless
329
-
330
- Headless mode is another option available in the browser mode. In headless mode, the browser runs in the background without a user interface, which makes it useful for running automated tests. The headless option in Vitest can be set to a boolean value to enable or disable headless mode.
331
-
332
- When using headless mode, Vitest won't open the UI automatically. If you want to continue using the UI but have tests run headlessly, you can install the [`@vitest/ui`](/guide/ui) package and pass the `--ui` flag when running Vitest.
333
-
334
- Here's an example configuration enabling headless mode:
335
-
336
- ```ts [vitest.config.ts]
337
- import { defineConfig } from 'vitest/config'
338
- import { playwright } from '@vitest/browser-playwright'
339
-
340
- export default defineConfig({
341
- test: {
342
- browser: {
343
- provider: playwright(),
344
- enabled: true,
345
- headless: true,
346
- },
347
- }
348
- })
349
- ```
350
-
351
- You can also set headless mode using the `--browser.headless` flag in the CLI, like this:
352
-
353
- ```sh
354
- npx vitest --browser.headless
355
- ```
356
-
357
- In this case, Vitest will run in headless mode using the Chrome browser.
358
-
359
- ::: warning
360
- Headless mode is not available by default. You need to use either [`playwright`](https://npmx.dev/package/playwright) or [`webdriverio`](https://npmx.dev/package/webdriverio) providers to enable this feature.
361
- :::
362
-
363
- ## Examples
364
-
365
- By default, you don't need any external packages to work with the Browser Mode:
366
-
367
- ```js [example.test.js]
368
- import { expect, test } from 'vitest'
369
- import { page } from 'vitest/browser'
370
- import { render } from './my-render-function.js'
371
-
372
- test('properly handles form inputs', async () => {
373
- render() // mount DOM elements
374
-
375
- // Asserts initial state.
376
- await expect.element(page.getByText('Hi, my name is Alice')).toBeInTheDocument()
377
-
378
- // Get the input DOM node by querying the associated label.
379
- const usernameInput = page.getByLabelText(/username/i)
380
-
381
- // Type the name into the input. This already validates that the input
382
- // is filled correctly, no need to check the value manually.
383
- await usernameInput.fill('Bob')
384
-
385
- await expect.element(page.getByText('Hi, my name is Bob')).toBeInTheDocument()
386
- })
387
- ```
388
-
389
- However, Vitest also provides packages to render components for several popular frameworks out of the box:
390
-
391
- - [`vitest-browser-vue`](https://github.com/vitest-dev/vitest-browser-vue) to render [vue](https://vuejs.org) components
392
- - [`vitest-browser-svelte`](https://github.com/vitest-dev/vitest-browser-svelte) to render [svelte](https://svelte.dev) components
393
- - [`vitest-browser-react`](https://github.com/vitest-dev/vitest-browser-react) to render [react](https://react.dev) components
394
- - [`vitest-browser-angular`](https://github.com/vitest-community/vitest-browser-angular) to render [Angular](https://angular.dev) components
395
-
396
- Community packages are available for other frameworks:
397
-
398
- - [`vitest-browser-lit`](https://github.com/EskiMojo14/vitest-browser-lit) to render [lit](https://lit.dev) components
399
- - [`vitest-browser-preact`](https://github.com/JoviDeCroock/vitest-browser-preact) to render [preact](https://preactjs.com) components
400
- - [`vitest-browser-qwik`](https://github.com/QwikDev/vitest-browser-qwik) to render [qwik](https://qwik.dev) components
401
-
402
- If your framework is not represented, feel free to create your own package - it is a simple wrapper around the framework renderer and `page.elementLocator` API. We will add a link to it on this page. Make sure it has a name starting with `vitest-browser-`.
403
-
404
- Besides rendering components and locating elements, you will also need to make assertions. Vitest forks the [`@testing-library/jest-dom`](https://github.com/testing-library/jest-dom) library to provide a wide range of DOM assertions out of the box. Read more at the [Assertions API](/api/browser/assertions).
405
-
406
- ```ts
407
- import { expect } from 'vitest'
408
- import { page } from 'vitest/browser'
409
- // element is rendered correctly
410
- await expect.element(page.getByText('Hello World')).toBeInTheDocument()
411
- ```
412
-
413
- Vitest exposes a [Context API](/api/browser/context) with a small set of utilities that might be useful to you in tests. For example, if you need to make an interaction, like clicking an element or typing text into an input, you can use `userEvent` from `vitest/browser`. Read more at the [Interactivity API](/api/browser/interactivity).
414
-
415
- ```ts
416
- import { page, userEvent } from 'vitest/browser'
417
- await userEvent.fill(page.getByLabelText(/username/i), 'Alice')
418
- // or just locator.fill
419
- await page.getByLabelText(/username/i).fill('Alice')
420
- ```
421
-
422
- ::: code-group
423
- ```ts [vue]
424
- import { render } from 'vitest-browser-vue'
425
- import Component from './Component.vue'
426
-
427
- test('properly handles v-model', async () => {
428
- const screen = render(Component)
429
-
430
- // Asserts initial state.
431
- await expect.element(screen.getByText('Hi, my name is Alice')).toBeInTheDocument()
432
-
433
- // Get the input DOM node by querying the associated label.
434
- const usernameInput = screen.getByLabelText(/username/i)
435
-
436
- // Type the name into the input. This already validates that the input
437
- // is filled correctly, no need to check the value manually.
438
- await usernameInput.fill('Bob')
439
-
440
- await expect.element(screen.getByText('Hi, my name is Bob')).toBeInTheDocument()
441
- })
442
- ```
443
- ```ts [svelte]
444
- import { render } from 'vitest-browser-svelte'
445
- import { expect, test } from 'vitest'
446
-
447
- import Greeter from './greeter.svelte'
448
-
449
- test('greeting appears on click', async () => {
450
- const screen = render(Greeter, { name: 'World' })
451
-
452
- const button = screen.getByRole('button')
453
- await button.click()
454
- const greeting = screen.getByText(/hello world/iu)
455
-
456
- await expect.element(greeting).toBeInTheDocument()
457
- })
458
- ```
459
- ```tsx [react]
460
- import { render } from 'vitest-browser-react'
461
- import Fetch from './fetch'
462
-
463
- test('loads and displays greeting', async () => {
464
- // Render a React element into the DOM
465
- const screen = render(<Fetch url="/greeting" />)
466
-
467
- await screen.getByText('Load Greeting').click()
468
- // wait before throwing an error if it cannot find an element
469
- const heading = screen.getByRole('heading')
470
-
471
- // assert that the alert message is correct
472
- await expect.element(heading).toHaveTextContent('hello there')
473
- await expect.element(screen.getByRole('button')).toBeDisabled()
474
- })
475
- ```
476
- ```ts [lit]
477
- import { render } from 'vitest-browser-lit'
478
- import { html } from 'lit'
479
- import './greeter-button'
480
-
481
- test('greeting appears on click', async () => {
482
- const screen = render(html`<greeter-button name="World"></greeter-button>`)
483
-
484
- const button = screen.getByRole('button')
485
- await button.click()
486
- const greeting = screen.getByText(/hello world/iu)
487
-
488
- await expect.element(greeting).toBeInTheDocument()
489
- })
490
- ```
491
- ```tsx [preact]
492
- import { render } from 'vitest-browser-preact'
493
- import { createElement } from 'preact'
494
- import Greeting from '.Greeting'
495
-
496
- test('greeting appears on click', async () => {
497
- const screen = render(<Greeting />)
498
-
499
- const button = screen.getByRole('button')
500
- await button.click()
501
- const greeting = screen.getByText(/hello world/iu)
502
-
503
- await expect.element(greeting).toBeInTheDocument()
504
- })
505
- ```
506
- ```tsx [qwik]
507
- import { render } from 'vitest-browser-qwik'
508
- import Greeting from './greeting'
509
-
510
- test('greeting appears on click', async () => {
511
- // renderSSR and renderHook are also available
512
- const screen = render(<Greeting />)
513
-
514
- const button = screen.getByRole('button')
515
- await button.click()
516
- const greeting = screen.getByText(/hello world/iu)
517
-
518
- await expect.element(greeting).toBeInTheDocument()
519
- })
520
- ```
521
- :::
522
-
523
- Vitest doesn't support all frameworks out of the box, but you can use external tools to run tests with these frameworks. We also encourage the community to create their own `vitest-browser` wrappers - if you have one, feel free to add it to the examples above.
524
-
525
- For unsupported frameworks, we recommend using `testing-library` packages:
526
-
527
- - [`@solidjs/testing-library`](https://testing-library.com/docs/solid-testing-library/intro) to render [solid](https://www.solidjs.com) components
528
- - [`@marko/testing-library`](https://testing-library.com/docs/marko-testing-library/intro) to render [marko](https://markojs.com) components
529
-
530
- You can also see more examples in [`browser-examples`](https://github.com/vitest-tests/browser-examples) repository.
531
-
532
- ::: warning
533
- `testing-library` provides a package `@testing-library/user-event`. We do not recommend using it directly because it simulates events instead of actually triggering them - instead, use [`userEvent`](/api/browser/interactivity) imported from `vitest/browser` that uses Chrome DevTools Protocol or Webdriver (depending on the provider) under the hood.
534
- :::
535
-
536
- ::: code-group
537
- ```tsx [solid]
538
- // based on @testing-library/solid API
539
- // https://testing-library.com/docs/solid-testing-library/api
540
-
541
- import { render } from '@testing-library/solid'
542
-
543
- it('uses params', async () => {
544
- const App = () => (
545
- <>
546
- <Route
547
- path="/ids/:id"
548
- component={() => (
549
- <p>
550
- Id:
551
- {useParams()?.id}
552
- </p>
553
- )}
554
- />
555
- <Route path="/" component={() => <p>Start</p>} />
556
- </>
557
- )
558
- const { baseElement } = render(() => <App />, { location: 'ids/1234' })
559
- const screen = page.elementLocator(baseElement)
560
-
561
- await expect.screen(screen.getByText('Id: 1234')).toBeInTheDocument()
562
- })
563
- ```
564
- ```ts [marko]
565
- // based on @testing-library/marko API
566
- // https://testing-library.com/docs/marko-testing-library/api
567
-
568
- import { render, screen } from '@marko/testing-library'
569
- import Greeting from './greeting.marko'
570
-
571
- test('renders a message', async () => {
572
- const { baseElement } = await render(Greeting, { name: 'Marko' })
573
- const screen = page.elementLocator(baseElement)
574
- await expect.element(screen.getByText(/Marko/)).toBeInTheDocument()
575
- expect(container.firstChild).toMatchInlineSnapshot(`
576
- <h1>Hello, Marko!</h1>
577
- `)
578
- })
579
- ```
580
- :::
581
-
582
- ## Limitations
583
-
584
- ### Thread Blocking Dialogs
585
-
586
- When using Vitest Browser, it's important to note that thread blocking dialogs like `alert` or `confirm` cannot be used natively. This is because they block the web page, which means Vitest cannot continue communicating with the page, causing the execution to hang.
587
-
588
- In such situations, Vitest provides default mocks with default returned values for these APIs. This ensures that if the user accidentally uses synchronous popup web APIs, the execution would not hang. However, it's still recommended for the user to mock these web APIs for a better experience. Read more in [Mocking](/guide/mocking).
589
-
590
- ### Spying on Module Exports
591
-
592
- Browser Mode uses the browser's native ESM support to serve modules. The module namespace object is sealed and can't be reconfigured, unlike in Node.js tests where Vitest can patch the Module Runner. This means you can't call `vi.spyOn` on an imported object:
593
-
594
- ```ts
595
- import { vi } from 'vitest'
596
- import * as module from './module.js'
597
-
598
- vi.spyOn(module, 'method') // ❌ throws an error
599
- ```
600
-
601
- To bypass this limitation, Vitest supports `{ spy: true }` option in `vi.mock('./module.js')`. This will automatically spy on every export in the module without replacing them with fake ones.
602
-
603
- ```ts
604
- import { vi } from 'vitest'
605
- import * as module from './module.js'
606
-
607
- vi.mock('./module.js', { spy: true })
608
-
609
- vi.mocked(module.method).mockImplementation(() => {
610
- // ...
611
- })
612
- ```
613
-
614
- However, the only way to mock exported _variables_ is to export a method that will change the internal value:
615
-
616
- ::: code-group
617
- ```js [module.js]
618
- export let MODE = 'test'
619
- export function changeMode(newMode) {
620
- MODE = newMode
621
- }
622
- ```
623
- ```js [module.test.ts]
624
- import { expect } from 'vitest'
625
- import { changeMode, MODE } from './module.js'
626
-
627
- changeMode('production')
628
- expect(MODE).toBe('production')
629
- ```
630
- :::