@vitest-agent/mcp 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (229) hide show
  1. package/README.md +1 -2
  2. package/bin/vitest-agent-mcp.js +1 -17
  3. package/index.d.ts +324 -315
  4. package/index.js +2 -4
  5. package/middleware/idempotency.js +1 -1
  6. package/package.json +2 -2
  7. package/server.js +2 -4
  8. package/tools/acceptance-metrics.js +1 -1
  9. package/tools/cache-health.js +1 -1
  10. package/tools/commit-changes.js +1 -1
  11. package/tools/configure.js +1 -1
  12. package/tools/coverage.js +1 -1
  13. package/tools/errors.js +1 -1
  14. package/tools/failure-signature-get.js +1 -1
  15. package/tools/file-coverage.js +1 -1
  16. package/tools/history.js +1 -1
  17. package/tools/inventory.js +1 -1
  18. package/tools/overview.js +1 -1
  19. package/tools/run-tests.js +15 -3
  20. package/tools/settings-list.js +1 -1
  21. package/tools/status.js +1 -1
  22. package/tools/tdd-artifact.js +1 -1
  23. package/tools/tdd-task.js +1 -1
  24. package/tools/test.js +1 -1
  25. package/tools/trends.js +1 -1
  26. package/tools/turn-search.js +1 -1
  27. package/public/patterns/_meta.json +0 -67
  28. package/public/patterns/authoring-a-custom-vitest-agent-reporter.md +0 -82
  29. package/public/patterns/known-issues-and-caveats.md +0 -52
  30. package/public/patterns/operating-vitest-agent-as-an-agent.md +0 -53
  31. package/public/patterns/running-tests-via-mcp.md +0 -58
  32. package/public/patterns/silencing-leaking-output-in-tests.md +0 -91
  33. package/public/patterns/testing-effect-schema-definitions.md +0 -71
  34. package/public/patterns/testing-effect-services-with-mock-layers.md +0 -63
  35. package/public/vendor/vitest-docs/ATTRIBUTION.md +0 -5
  36. package/public/vendor/vitest-docs/api/advanced/artifacts.md +0 -189
  37. package/public/vendor/vitest-docs/api/advanced/metadata.md +0 -68
  38. package/public/vendor/vitest-docs/api/advanced/plugin.md +0 -168
  39. package/public/vendor/vitest-docs/api/advanced/reporters.md +0 -342
  40. package/public/vendor/vitest-docs/api/advanced/runner.md +0 -334
  41. package/public/vendor/vitest-docs/api/advanced/test-case.md +0 -302
  42. package/public/vendor/vitest-docs/api/advanced/test-collection.md +0 -89
  43. package/public/vendor/vitest-docs/api/advanced/test-module.md +0 -140
  44. package/public/vendor/vitest-docs/api/advanced/test-project.md +0 -321
  45. package/public/vendor/vitest-docs/api/advanced/test-specification.md +0 -96
  46. package/public/vendor/vitest-docs/api/advanced/test-suite.md +0 -230
  47. package/public/vendor/vitest-docs/api/advanced/vitest.md +0 -684
  48. package/public/vendor/vitest-docs/api/assert-type.md +0 -22
  49. package/public/vendor/vitest-docs/api/assert.md +0 -1960
  50. package/public/vendor/vitest-docs/api/browser/assertions.md +0 -1277
  51. package/public/vendor/vitest-docs/api/browser/commands.md +0 -154
  52. package/public/vendor/vitest-docs/api/browser/context.md +0 -338
  53. package/public/vendor/vitest-docs/api/browser/interactivity.md +0 -681
  54. package/public/vendor/vitest-docs/api/browser/locators.md +0 -1171
  55. package/public/vendor/vitest-docs/api/browser/react.md +0 -346
  56. package/public/vendor/vitest-docs/api/browser/svelte.md +0 -292
  57. package/public/vendor/vitest-docs/api/browser/vue.md +0 -222
  58. package/public/vendor/vitest-docs/api/describe.md +0 -374
  59. package/public/vendor/vitest-docs/api/expect-typeof.md +0 -571
  60. package/public/vendor/vitest-docs/api/expect.md +0 -2304
  61. package/public/vendor/vitest-docs/api/hooks.md +0 -463
  62. package/public/vendor/vitest-docs/api/mock.md +0 -701
  63. package/public/vendor/vitest-docs/api/test.md +0 -926
  64. package/public/vendor/vitest-docs/api/vi.md +0 -1372
  65. package/public/vendor/vitest-docs/config/alias.md +0 -13
  66. package/public/vendor/vitest-docs/config/allowonly.md +0 -32
  67. package/public/vendor/vitest-docs/config/api.md +0 -27
  68. package/public/vendor/vitest-docs/config/attachmentsdir.md +0 -6
  69. package/public/vendor/vitest-docs/config/bail.md +0 -9
  70. package/public/vendor/vitest-docs/config/benchmark.md +0 -65
  71. package/public/vendor/vitest-docs/config/browser/api.md +0 -23
  72. package/public/vendor/vitest-docs/config/browser/commands.md +0 -6
  73. package/public/vendor/vitest-docs/config/browser/connecttimeout.md +0 -10
  74. package/public/vendor/vitest-docs/config/browser/detailspanelposition.md +0 -38
  75. package/public/vendor/vitest-docs/config/browser/enabled.md +0 -40
  76. package/public/vendor/vitest-docs/config/browser/expect.md +0 -250
  77. package/public/vendor/vitest-docs/config/browser/headless.md +0 -7
  78. package/public/vendor/vitest-docs/config/browser/instances.md +0 -47
  79. package/public/vendor/vitest-docs/config/browser/isolate.md +0 -11
  80. package/public/vendor/vitest-docs/config/browser/locators.md +0 -24
  81. package/public/vendor/vitest-docs/config/browser/orchestratorscripts.md +0 -39
  82. package/public/vendor/vitest-docs/config/browser/playwright.md +0 -214
  83. package/public/vendor/vitest-docs/config/browser/preview.md +0 -32
  84. package/public/vendor/vitest-docs/config/browser/provider.md +0 -79
  85. package/public/vendor/vitest-docs/config/browser/screenshotdirectory.md +0 -6
  86. package/public/vendor/vitest-docs/config/browser/screenshotfailures.md +0 -6
  87. package/public/vendor/vitest-docs/config/browser/testerhtmlpath.md +0 -5
  88. package/public/vendor/vitest-docs/config/browser/trace.md +0 -43
  89. package/public/vendor/vitest-docs/config/browser/trackunhandlederrors.md +0 -10
  90. package/public/vendor/vitest-docs/config/browser/ui.md +0 -7
  91. package/public/vendor/vitest-docs/config/browser/viewport.md +0 -6
  92. package/public/vendor/vitest-docs/config/browser/webdriverio.md +0 -64
  93. package/public/vendor/vitest-docs/config/cache.md +0 -26
  94. package/public/vendor/vitest-docs/config/chaiconfig.md +0 -29
  95. package/public/vendor/vitest-docs/config/clearmocks.md +0 -22
  96. package/public/vendor/vitest-docs/config/coverage.md +0 -455
  97. package/public/vendor/vitest-docs/config/css.md +0 -47
  98. package/public/vendor/vitest-docs/config/dangerouslyignoreunhandlederrors.md +0 -23
  99. package/public/vendor/vitest-docs/config/deps.md +0 -127
  100. package/public/vendor/vitest-docs/config/detectasyncleaks.md +0 -39
  101. package/public/vendor/vitest-docs/config/diff.md +0 -96
  102. package/public/vendor/vitest-docs/config/dir.md +0 -7
  103. package/public/vendor/vitest-docs/config/disableconsoleintercept.md +0 -15
  104. package/public/vendor/vitest-docs/config/env.md +0 -5
  105. package/public/vendor/vitest-docs/config/environment.md +0 -96
  106. package/public/vendor/vitest-docs/config/environmentoptions.md +0 -30
  107. package/public/vendor/vitest-docs/config/exclude.md +0 -49
  108. package/public/vendor/vitest-docs/config/execargv.md +0 -10
  109. package/public/vendor/vitest-docs/config/expandsnapshotdiff.md +0 -7
  110. package/public/vendor/vitest-docs/config/expect.md +0 -38
  111. package/public/vendor/vitest-docs/config/experimental.md +0 -510
  112. package/public/vendor/vitest-docs/config/faketimers.md +0 -51
  113. package/public/vendor/vitest-docs/config/fileparallelism.md +0 -11
  114. package/public/vendor/vitest-docs/config/forcereruntriggers.md +0 -19
  115. package/public/vendor/vitest-docs/config/globals.md +0 -42
  116. package/public/vendor/vitest-docs/config/globalsetup.md +0 -72
  117. package/public/vendor/vitest-docs/config/hideskippedtests.md +0 -7
  118. package/public/vendor/vitest-docs/config/hooktimeout.md +0 -7
  119. package/public/vendor/vitest-docs/config/include-source.md +0 -115
  120. package/public/vendor/vitest-docs/config/include.md +0 -71
  121. package/public/vendor/vitest-docs/config/includetasklocation.md +0 -17
  122. package/public/vendor/vitest-docs/config/index.md +0 -85
  123. package/public/vendor/vitest-docs/config/isolate.md +0 -13
  124. package/public/vendor/vitest-docs/config/logheapusage.md +0 -7
  125. package/public/vendor/vitest-docs/config/maxconcurrency.md +0 -9
  126. package/public/vendor/vitest-docs/config/maxworkers.md +0 -49
  127. package/public/vendor/vitest-docs/config/mockreset.md +0 -22
  128. package/public/vendor/vitest-docs/config/mode.md +0 -7
  129. package/public/vendor/vitest-docs/config/name.md +0 -111
  130. package/public/vendor/vitest-docs/config/onconsolelog.md +0 -25
  131. package/public/vendor/vitest-docs/config/onstacktrace.md +0 -32
  132. package/public/vendor/vitest-docs/config/onunhandlederror.md +0 -35
  133. package/public/vendor/vitest-docs/config/open.md +0 -7
  134. package/public/vendor/vitest-docs/config/outputfile.md +0 -7
  135. package/public/vendor/vitest-docs/config/passwithnotests.md +0 -7
  136. package/public/vendor/vitest-docs/config/pool.md +0 -45
  137. package/public/vendor/vitest-docs/config/printconsoletrace.md +0 -6
  138. package/public/vendor/vitest-docs/config/projects.md +0 -6
  139. package/public/vendor/vitest-docs/config/provide.md +0 -45
  140. package/public/vendor/vitest-docs/config/reporters.md +0 -69
  141. package/public/vendor/vitest-docs/config/resolvesnapshotpath.md +0 -36
  142. package/public/vendor/vitest-docs/config/restoremocks.md +0 -22
  143. package/public/vendor/vitest-docs/config/retry.md +0 -140
  144. package/public/vendor/vitest-docs/config/root.md +0 -6
  145. package/public/vendor/vitest-docs/config/runner.md +0 -6
  146. package/public/vendor/vitest-docs/config/sequence.md +0 -158
  147. package/public/vendor/vitest-docs/config/server.md +0 -68
  148. package/public/vendor/vitest-docs/config/setupfiles.md +0 -40
  149. package/public/vendor/vitest-docs/config/silent.md +0 -9
  150. package/public/vendor/vitest-docs/config/slowtestthreshold.md +0 -7
  151. package/public/vendor/vitest-docs/config/snapshotenvironment.md +0 -27
  152. package/public/vendor/vitest-docs/config/snapshotformat.md +0 -28
  153. package/public/vendor/vitest-docs/config/snapshotserializers.md +0 -6
  154. package/public/vendor/vitest-docs/config/stricttags.md +0 -30
  155. package/public/vendor/vitest-docs/config/tags.md +0 -141
  156. package/public/vendor/vitest-docs/config/teardowntimeout.md +0 -7
  157. package/public/vendor/vitest-docs/config/testnamepattern.md +0 -21
  158. package/public/vendor/vitest-docs/config/testtimeout.md +0 -7
  159. package/public/vendor/vitest-docs/config/typecheck.md +0 -77
  160. package/public/vendor/vitest-docs/config/ui.md +0 -15
  161. package/public/vendor/vitest-docs/config/unstubenvs.md +0 -20
  162. package/public/vendor/vitest-docs/config/unstubglobals.md +0 -20
  163. package/public/vendor/vitest-docs/config/update.md +0 -16
  164. package/public/vendor/vitest-docs/config/vmmemorylimit.md +0 -30
  165. package/public/vendor/vitest-docs/config/watch.md +0 -11
  166. package/public/vendor/vitest-docs/config/watchtriggerpatterns.md +0 -29
  167. package/public/vendor/vitest-docs/guide/advanced/index.md +0 -147
  168. package/public/vendor/vitest-docs/guide/advanced/pool.md +0 -148
  169. package/public/vendor/vitest-docs/guide/advanced/reporters.md +0 -93
  170. package/public/vendor/vitest-docs/guide/advanced/tests.md +0 -125
  171. package/public/vendor/vitest-docs/guide/browser/aria-snapshots.md +0 -470
  172. package/public/vendor/vitest-docs/guide/browser/component-testing.md +0 -571
  173. package/public/vendor/vitest-docs/guide/browser/index.md +0 -630
  174. package/public/vendor/vitest-docs/guide/browser/multiple-setups.md +0 -121
  175. package/public/vendor/vitest-docs/guide/browser/trace-view.md +0 -126
  176. package/public/vendor/vitest-docs/guide/browser/visual-regression-testing.md +0 -734
  177. package/public/vendor/vitest-docs/guide/cli-generated.md +0 -972
  178. package/public/vendor/vitest-docs/guide/cli.md +0 -234
  179. package/public/vendor/vitest-docs/guide/common-errors.md +0 -163
  180. package/public/vendor/vitest-docs/guide/coverage.md +0 -515
  181. package/public/vendor/vitest-docs/guide/debugging.md +0 -127
  182. package/public/vendor/vitest-docs/guide/environment.md +0 -101
  183. package/public/vendor/vitest-docs/guide/extending-matchers.md +0 -160
  184. package/public/vendor/vitest-docs/guide/features.md +0 -310
  185. package/public/vendor/vitest-docs/guide/filtering.md +0 -175
  186. package/public/vendor/vitest-docs/guide/ide.md +0 -43
  187. package/public/vendor/vitest-docs/guide/improving-performance.md +0 -245
  188. package/public/vendor/vitest-docs/guide/in-source.md +0 -159
  189. package/public/vendor/vitest-docs/guide/index.md +0 -128
  190. package/public/vendor/vitest-docs/guide/learn/async.md +0 -147
  191. package/public/vendor/vitest-docs/guide/learn/debugging-tests.md +0 -210
  192. package/public/vendor/vitest-docs/guide/learn/matchers.md +0 -277
  193. package/public/vendor/vitest-docs/guide/learn/mock-functions.md +0 -277
  194. package/public/vendor/vitest-docs/guide/learn/setup-teardown.md +0 -240
  195. package/public/vendor/vitest-docs/guide/learn/snapshots.md +0 -166
  196. package/public/vendor/vitest-docs/guide/learn/testing-in-practice.md +0 -430
  197. package/public/vendor/vitest-docs/guide/learn/writing-tests-with-ai.md +0 -127
  198. package/public/vendor/vitest-docs/guide/learn/writing-tests.md +0 -231
  199. package/public/vendor/vitest-docs/guide/lifecycle.md +0 -379
  200. package/public/vendor/vitest-docs/guide/migration.md +0 -863
  201. package/public/vendor/vitest-docs/guide/mocking/classes.md +0 -158
  202. package/public/vendor/vitest-docs/guide/mocking/dates.md +0 -52
  203. package/public/vendor/vitest-docs/guide/mocking/file-system.md +0 -74
  204. package/public/vendor/vitest-docs/guide/mocking/functions.md +0 -61
  205. package/public/vendor/vitest-docs/guide/mocking/globals.md +0 -20
  206. package/public/vendor/vitest-docs/guide/mocking/modules.md +0 -414
  207. package/public/vendor/vitest-docs/guide/mocking/requests.md +0 -114
  208. package/public/vendor/vitest-docs/guide/mocking/timers.md +0 -48
  209. package/public/vendor/vitest-docs/guide/mocking.md +0 -239
  210. package/public/vendor/vitest-docs/guide/open-telemetry.md +0 -156
  211. package/public/vendor/vitest-docs/guide/parallelism.md +0 -82
  212. package/public/vendor/vitest-docs/guide/profiling-test-performance.md +0 -243
  213. package/public/vendor/vitest-docs/guide/projects.md +0 -291
  214. package/public/vendor/vitest-docs/guide/recipes.md +0 -59
  215. package/public/vendor/vitest-docs/guide/reporters.md +0 -723
  216. package/public/vendor/vitest-docs/guide/snapshot.md +0 -620
  217. package/public/vendor/vitest-docs/guide/test-annotations.md +0 -103
  218. package/public/vendor/vitest-docs/guide/test-context.md +0 -902
  219. package/public/vendor/vitest-docs/guide/test-tags.md +0 -314
  220. package/public/vendor/vitest-docs/guide/testing-types.md +0 -149
  221. package/public/vendor/vitest-docs/guide/ui.md +0 -160
  222. package/public/vendor/vitest-docs/guide/using-plugins.md +0 -5
  223. package/public/vendor/vitest-docs/manifest.json +0 -1691
  224. package/resources/index.js +0 -166
  225. package/resources/indexes.js +0 -77
  226. package/resources/manifest-schema.js +0 -46
  227. package/resources/paths.js +0 -20
  228. package/resources/patterns.js +0 -22
  229. package/resources/upstream-docs.js +0 -22
@@ -1,101 +0,0 @@
1
- # Test Environment
2
-
3
- Vitest provides [`environment`](/config/environment) option to run code inside a specific environment. You can modify how environment behaves with [`environmentOptions`](/config/environmentoptions) option.
4
-
5
- By default, you can use these environments:
6
-
7
- - `node` is default environment
8
- - `jsdom` emulates browser environment by providing Browser API, uses [`jsdom`](https://github.com/jsdom/jsdom) package
9
- - `happy-dom` emulates browser environment by providing Browser API, and considered to be faster than jsdom, but lacks some API, uses [`happy-dom`](https://github.com/capricorn86/happy-dom) package
10
- - `edge-runtime` emulates Vercel's [edge-runtime](https://edge-runtime.vercel.app/), uses [`@edge-runtime/vm`](https://npmx.dev/package/@edge-runtime/vm) package
11
-
12
- ::: info
13
- When using `jsdom` or `happy-dom` environments, Vitest follows the same rules that Vite does when importing [CSS](https://vitejs.dev/guide/features.html#css) and [assets](https://vitejs.dev/guide/features.html#static-assets). If importing external dependency fails with `unknown extension .css` error, you need to inline the whole import chain manually by adding all packages to [`server.deps.inline`](/config/server#inline). For example, if the error happens in `package-3` in this import chain: `source code -> package-1 -> package-2 -> package-3`, you need to add all three packages to `server.deps.inline`.
14
-
15
- The `require` of CSS and assets inside the external dependencies are resolved automatically.
16
- :::
17
-
18
- ::: warning
19
- "Environments" exist only when running tests in Node.js.
20
-
21
- `browser` is not considered an environment in Vitest. If you wish to run part of your tests using [Browser Mode](/guide/browser/), you can create a [test project](/guide/browser/#projects-config).
22
- :::
23
-
24
- ## Environments for Specific Files
25
-
26
- When setting `environment` option in your config, it will apply to all the test files in your project. To have more fine-grained control, you can use control comments to specify environment for specific files. Control comments are comments that start with `@vitest-environment` and are followed by the environment name:
27
-
28
- ```ts
29
- // @vitest-environment jsdom
30
-
31
- import { expect, test } from 'vitest'
32
-
33
- test('test', () => {
34
- expect(typeof window).not.toBe('undefined')
35
- })
36
- ```
37
-
38
- ## Custom Environment
39
-
40
- You can create your own package to extend Vitest environment. To do so, create package with the name `vitest-environment-${name}` or specify a path to a valid JS/TS file. That package should export an object with the shape of `Environment`:
41
-
42
- ```ts
43
- import type { Environment } from 'vitest/runtime'
44
-
45
- export default <Environment>{
46
- name: 'custom',
47
- viteEnvironment: 'ssr',
48
- // optional - only if you support "vmForks" or "vmThreads" pools
49
- async setupVM() {
50
- const vm = await import('node:vm')
51
- const context = vm.createContext()
52
- return {
53
- getVmContext() {
54
- return context
55
- },
56
- teardown() {
57
- // called after all tests with this env have been run
58
- }
59
- }
60
- },
61
- setup() {
62
- // custom setup
63
- return {
64
- teardown() {
65
- // called after all tests with this env have been run
66
- }
67
- }
68
- }
69
- }
70
- ```
71
-
72
- ::: warning
73
- Vitest requires `viteEnvironment` option on environment object (fallbacks to the Vitest environment name by default). It should be equal to `ssr`, `client` or any custom [Vite environment](https://vite.dev/guide/api-environment) name. This value determines which environment is used to process file.
74
- :::
75
-
76
- You also have access to default Vitest environments through `vitest/runtime` entry:
77
-
78
- ```ts
79
- import { builtinEnvironments, populateGlobal } from 'vitest/runtime'
80
-
81
- console.log(builtinEnvironments) // { jsdom, happy-dom, node, edge-runtime }
82
- ```
83
-
84
- Vitest also provides `populateGlobal` utility function, which can be used to move properties from object into the global namespace:
85
-
86
- ```ts
87
- interface PopulateOptions {
88
- // should non-class functions be bind to the global namespace
89
- bindFunctions?: boolean
90
- }
91
-
92
- interface PopulateResult {
93
- // a list of all keys that were copied, even if value doesn't exist on original object
94
- keys: Set<string>
95
- // a map of original object that might have been overridden with keys
96
- // you can return these values inside `teardown` function
97
- originals: Map<string | symbol, any>
98
- }
99
-
100
- export function populateGlobal(global: any, original: any, options: PopulateOptions): PopulateResult
101
- ```
@@ -1,160 +0,0 @@
1
- # Extending Matchers
2
-
3
- Since Vitest is compatible with both Chai and Jest, you can use either the [`chai.use`](https://www.chaijs.com/guide/plugins/) API or `expect.extend`, whichever you prefer.
4
-
5
- This guide will explore extending matchers with `expect.extend`. If you are interested in Chai's API, check [their guide](https://www.chaijs.com/guide/plugins/).
6
-
7
- To extend default matchers, call `expect.extend` with an object containing your matchers.
8
-
9
- ```ts
10
- expect.extend({
11
- toBeFoo(received, expected) {
12
- const { isNot } = this
13
- return {
14
- // do not alter your "pass" based on isNot. Vitest does it for you
15
- pass: received === 'foo',
16
- message: () => `${received} is${isNot ? ' not' : ''} foo`
17
- }
18
- }
19
- })
20
- ```
21
-
22
- If you are using TypeScript, you can extend default `Matchers` interface in an ambient declaration file (e.g: `vitest.d.ts`) with the code below:
23
-
24
- ```ts
25
- import 'vitest'
26
-
27
- declare module 'vitest' {
28
- interface Matchers<T = any> {
29
- toBeFoo: () => R
30
- }
31
- }
32
- ```
33
-
34
- ::: tip
35
- Importing `vitest` makes TypeScript think this is an ES module file, type declaration won't work without it.
36
- :::
37
-
38
- Extending the `Matchers` interface will add a type to `expect.extend`, `expect().*`, and `expect.*` methods at the same time.
39
-
40
- ::: warning
41
- Don't forget to include the ambient declaration file in your `tsconfig.json`.
42
- :::
43
-
44
- The return value of a matcher should be compatible with the following interface:
45
-
46
- ```ts
47
- interface MatcherResult {
48
- pass: boolean
49
- message: () => string
50
- // If you pass these, they will automatically appear inside a diff when
51
- // the matcher does not pass, so you don't need to print the diff yourself
52
- actual?: unknown
53
- expected?: unknown
54
- }
55
- ```
56
-
57
- ::: warning
58
- If you create an asynchronous matcher, don't forget to `await` the result (`await expect('foo').toBeFoo()`) in the test itself:
59
-
60
- ```ts
61
- expect.extend({
62
- async toBeAsyncAssertion() {
63
- // ...
64
- }
65
- })
66
-
67
- await expect().toBeAsyncAssertion()
68
- ```
69
- :::
70
-
71
- The first argument inside a matcher's function is the received value (the one inside `expect(received)`). The rest are arguments passed directly to the matcher. Since version 4.1, Vitest exposes several types that can be used by your custom matcher:
72
-
73
- ```ts
74
- import type {
75
- // the function type
76
- Matcher,
77
- // the return value
78
- MatcherResult,
79
- // state available as `this`
80
- MatcherState,
81
- } from 'vitest'
82
- import { expect } from 'vitest'
83
-
84
- // a simple matcher, using "function" to have access to "this"
85
- const customMatcher: Matcher = function (received) {
86
- // ...
87
- }
88
-
89
- // a matcher with arguments
90
- const customMatcher: Matcher<MatcherState, [arg1: unknown, arg2: unknown]> = function (received, arg1, arg2) {
91
- // ...
92
- }
93
-
94
- // a matcher with custom annotations
95
- function customMatcher(this: MatcherState, received: unknown, arg1: unknown, arg2: unknown): MatcherResult {
96
- // ...
97
- return {
98
- pass: false,
99
- message: () => 'something went wrong!',
100
- }
101
- }
102
-
103
- expect.extend({ customMatcher })
104
- ```
105
-
106
- ::: tip
107
- To build custom **snapshot matchers** (wrappers around `toMatchSnapshot()` / `toMatchInlineSnapshot()` / `toMatchFileSnapshot()`), use `Snapshots` exported from `vitest`. See [Custom Snapshot Matchers](/guide/snapshot#custom-snapshot-matchers).
108
- :::
109
-
110
- Matcher function has access to `this` context with the following properties:
111
-
112
- ## `isNot`
113
-
114
- Returns true, if matcher was called on `not` (`expect(received).not.toBeFoo()`). You do not need to respect it, Vitest will reverse the value of `pass` automatically.
115
-
116
- ## `promise`
117
-
118
- If matcher was called on `resolved/rejected`, this value will contain the name of modifier. Otherwise, it will be an empty string.
119
-
120
- ## `equals`
121
-
122
- This is a utility function that allows you to compare two values. It will return `true` if values are equal, `false` otherwise. This function is used internally for almost every matcher. It supports objects with asymmetric matchers by default.
123
-
124
- ## `utils`
125
-
126
- This contains a set of utility functions that you can use to display messages.
127
-
128
- `this` context also contains information about the current test. You can also get it by calling `expect.getState()`. The most useful properties are:
129
-
130
- ## `currentTestName`
131
-
132
- Full name of the current test (including describe block).
133
-
134
- ## `task` <Advanced /> <Version>4.1.0</Version> {#task}
135
-
136
- Contains a reference to [the `Test` runner task](/api/advanced/runner#tasks) when available.
137
-
138
- ::: warning
139
- When using the global `expect` with concurrent tests, `this.task` is `undefined`. Use `context.expect` instead to ensure `task` is available in custom matchers.
140
- :::
141
-
142
- ## `testPath`
143
-
144
- File path to the current test.
145
-
146
- ## `environment`
147
-
148
- The name of the current [`environment`](/config/environment) (for example, `jsdom`).
149
-
150
- ## `soft`
151
-
152
- Was assertion called as a [`soft`](/api/expect#soft) one. You don't need to respect it, Vitest will always catch the error.
153
-
154
- ## `assertion` <Advanced /> <Version type="experimental">4.1.4</Version> {#assertion}
155
-
156
- The underlying [Chai assertion](https://www.chaijs.com/guide/plugins/) object. This is the same instance that Chai plugins receive, giving you access to Chai's flag system and chainable methods. This can be useful for building custom matchers that need to interact with Chai's internals.
157
-
158
- ::: tip
159
- These are not all of the available properties, only the most useful ones. The other state values are used by Vitest internally.
160
- :::
@@ -1,310 +0,0 @@
1
- # Features
2
-
3
- <script setup>
4
- import FeaturesList from '../.vitepress/components/FeaturesList.vue'
5
- </script>
6
-
7
- <FeaturesList class="!gap-1 text-lg" />
8
-
9
- <div h-2 />
10
- <CourseLink href="https://vueschool.io/lessons/your-first-test?friend=vueuse">Learn how to write your first test by Video</CourseLink>
11
-
12
- ::: tip
13
- This page is a high-level overview of Vitest's capabilities. If you're new to Vitest, we recommend reading the [Learn](/guide/learn/writing-tests) tutorial first for a hands-on introduction.
14
- :::
15
-
16
- ## Shared Config between Test, Dev and Build
17
-
18
- Vite's config, transformers, resolvers, and plugins. Use the same setup from your app to run the tests.
19
-
20
- Learn more at [Configuring Vitest](/config/).
21
-
22
- ## Watch Mode
23
-
24
- ```bash
25
- $ vitest
26
- ```
27
-
28
- When you modify your source code or the test files, Vitest smartly searches the module graph and only reruns the related tests, just like how HMR works in Vite!
29
-
30
- `vitest` starts in `watch mode` **by default in development environment** and `run mode` in CI environment (when `process.env.CI` presents) smartly. You can use `vitest watch` or `vitest run` to explicitly specify the desired mode.
31
-
32
- Start Vitest with the `--standalone` flag to keep it running in the background. It won't run any tests until they change. Vitest will not run tests if the source code is changed until the test that imports the source has been run
33
-
34
- ## Common Web Idioms Out-Of-The-Box
35
-
36
- Out-of-the-box ES Module / TypeScript / JSX support / PostCSS
37
-
38
- ## Threads
39
-
40
- By default Vitest runs test files in [multiple processes](/guide/parallelism) using [`node:child_process`](https://nodejs.org/api/child_process.html), allowing tests to run simultaneously. If you want to speed up your test suite even further, consider enabling `--pool=threads` to run tests using [`node:worker_threads`](https://nodejs.org/api/worker_threads.html) (beware that some packages might not work with this setup).
41
- To run tests in a single thread or process, see [`fileParallelism`](/config/fileparallelism).
42
-
43
- Vitest also isolates each file's environment so env mutations in one file don't affect others. Isolation can be disabled by passing `--no-isolate` to the CLI (trading correctness for run performance).
44
-
45
- ## Test Filtering
46
-
47
- Vitest provides many ways to narrow down the tests to run in order to speed up testing so you can focus on development.
48
-
49
- Learn more about [Test Filtering](/guide/filtering).
50
-
51
- ## Running Tests Concurrently
52
-
53
- Use `.concurrent` in consecutive tests to start them in parallel.
54
-
55
- ```ts
56
- import { describe, it } from 'vitest'
57
-
58
- // The two tests marked with concurrent will be started in parallel
59
- describe('suite', () => {
60
- it('serial test', async () => { /* ... */ })
61
- it.concurrent('concurrent test 1', async ({ expect }) => { /* ... */ })
62
- it.concurrent('concurrent test 2', async ({ expect }) => { /* ... */ })
63
- })
64
- ```
65
-
66
- If you use `.concurrent` on a suite, every test in it will be started in parallel.
67
-
68
- ```ts
69
- import { describe, it } from 'vitest'
70
-
71
- // All tests within this suite will be started in parallel
72
- describe.concurrent('suite', () => {
73
- it('concurrent test 1', async ({ expect }) => { /* ... */ })
74
- it('concurrent test 2', async ({ expect }) => { /* ... */ })
75
- it.concurrent('concurrent test 3', async ({ expect }) => { /* ... */ })
76
- })
77
- ```
78
-
79
- You can also use `.skip`, `.only`, and `.todo` with concurrent suites and tests. Read more in the [API Reference](/api/test#test-concurrent).
80
-
81
- ::: warning
82
- When running concurrent tests, Snapshots and Assertions must use `expect` from the local [Test Context](/guide/test-context) to ensure the right test is detected.
83
- :::
84
-
85
- ## Snapshot
86
-
87
- [Jest-compatible](https://jestjs.io/docs/snapshot-testing) snapshot support.
88
-
89
- ```ts
90
- import { expect, it } from 'vitest'
91
-
92
- it('renders correctly', () => {
93
- const result = render()
94
- expect(result).toMatchSnapshot()
95
- })
96
- ```
97
-
98
- Learn more at [Snapshot](/guide/snapshot).
99
-
100
- ## Chai and Jest `expect` Compatibility
101
-
102
- [Chai](https://www.chaijs.com/) is built-in for assertions with [Jest `expect`](https://jestjs.io/docs/expect)-compatible APIs.
103
-
104
- Notice that if you are using third-party libraries that add matchers, setting [`test.globals`](/config/globals) to `true` will provide better compatibility.
105
-
106
- ## Mocking
107
-
108
- Vitest provides `jest`-compatible APIs on `vi` object.
109
-
110
- ```ts
111
- import { expect, vi } from 'vitest'
112
-
113
- const fn = vi.fn()
114
-
115
- fn('hello', 1)
116
-
117
- expect(vi.isMockFunction(fn)).toBe(true)
118
- expect(fn.mock.calls[0]).toEqual(['hello', 1])
119
-
120
- fn.mockImplementation((arg: string) => arg)
121
-
122
- fn('world', 2)
123
-
124
- expect(fn.mock.results[1].value).toBe('world')
125
- ```
126
-
127
- Vitest supports both [happy-dom](https://github.com/capricorn86/happy-dom) or [jsdom](https://github.com/jsdom/jsdom) for mocking DOM and browser APIs. They don't come with Vitest, you will need to install them separately:
128
-
129
- ::: code-group
130
- ```bash [happy-dom]
131
- $ npm i -D happy-dom
132
- ```
133
- ```bash [jsdom]
134
- $ npm i -D jsdom
135
- ```
136
- :::
137
-
138
- After that, change the `environment` option in your config file:
139
-
140
- ```ts [vitest.config.ts]
141
- import { defineConfig } from 'vitest/config'
142
-
143
- export default defineConfig({
144
- test: {
145
- environment: 'happy-dom', // or 'jsdom', 'node'
146
- },
147
- })
148
- ```
149
-
150
- Learn more at [Mocking](/guide/mocking).
151
-
152
- ## Coverage
153
-
154
- Vitest supports Native code coverage via [`v8`](https://v8.dev/blog/javascript-code-coverage) and instrumented code coverage via [`istanbul`](https://istanbul.js.org/).
155
-
156
- ```json [package.json]
157
- {
158
- "scripts": {
159
- "test": "vitest",
160
- "coverage": "vitest run --coverage"
161
- }
162
- }
163
- ```
164
-
165
- Learn more at [Coverage](/guide/coverage).
166
-
167
- ## In-Source Testing
168
-
169
- Vitest also provides a way to run tests within your source code along with the implementation, similar to [Rust's module tests](https://doc.rust-lang.org/book/ch11-03-test-organization.html#the-tests-module-and-cfgtest).
170
-
171
- This makes the tests share the same closure as the implementations and able to test against private states without exporting. Meanwhile, it also brings the feedback loop closer for development.
172
-
173
- ```ts [src/index.ts]
174
- // the implementation
175
- export function add(...args: number[]): number {
176
- return args.reduce((a, b) => a + b, 0)
177
- }
178
-
179
- // in-source test suites
180
- if (import.meta.vitest) {
181
- const { it, expect } = import.meta.vitest
182
- it('add', () => {
183
- expect(add()).toBe(0)
184
- expect(add(1)).toBe(1)
185
- expect(add(1, 2, 3)).toBe(6)
186
- })
187
- }
188
- ```
189
-
190
- Learn more at [In-source testing](/guide/in-source).
191
-
192
- ## Benchmarking <Badge type="warning">Experimental</Badge> {#benchmarking}
193
-
194
- You can run benchmark tests with [`bench`](/api/test#bench) function via [Tinybench](https://github.com/tinylibs/tinybench) to compare performance results.
195
-
196
- ```ts [sort.bench.ts]
197
- import { bench, describe } from 'vitest'
198
-
199
- describe('sort', () => {
200
- bench('normal', () => {
201
- const x = [1, 5, 4, 2, 3]
202
- x.sort((a, b) => {
203
- return a - b
204
- })
205
- })
206
-
207
- bench('reverse', () => {
208
- const x = [1, 5, 4, 2, 3]
209
- x.reverse().sort((a, b) => {
210
- return a - b
211
- })
212
- })
213
- })
214
- ```
215
-
216
- <img alt="Benchmark report" img-dark src="https://github.com/vitest-dev/vitest/assets/4232207/6f0383ea-38ba-4f14-8a05-ab243afea01d">
217
- <img alt="Benchmark report" img-light src="https://github.com/vitest-dev/vitest/assets/4232207/efbcb427-ecf1-4882-88de-210cd73415f6">
218
-
219
- ## Type Testing <Badge type="warning">Experimental</Badge> {#type-testing}
220
-
221
- You can [write tests](/guide/testing-types) to catch type regressions. Vitest comes with [`expect-type`](https://github.com/mmkal/expect-type) package to provide you with a similar and easy to understand API.
222
-
223
- ```ts [types.test-d.ts]
224
- import { assertType, expectTypeOf, test } from 'vitest'
225
- import { mount } from './mount.js'
226
-
227
- test('my types work properly', () => {
228
- expectTypeOf(mount).toBeFunction()
229
- expectTypeOf(mount).parameter(0).toExtend<{ name: string }>()
230
-
231
- // @ts-expect-error name is a string
232
- assertType(mount({ name: 42 }))
233
- })
234
- ```
235
-
236
- ## Sharding
237
-
238
- Run tests on different machines using [`--shard`](/guide/cli#shard) and [`--reporter=blob`](/guide/reporters#blob-reporter) flags.
239
- All test and coverage results can be merged at the end of your CI pipeline using `--merge-reports` command:
240
-
241
- ```bash
242
- vitest --shard=1/2 --reporter=blob --coverage
243
- vitest --shard=2/2 --reporter=blob --coverage
244
- vitest --merge-reports --reporter=junit --coverage
245
- ```
246
-
247
- See [`Improving Performance | Sharding`](/guide/improving-performance#sharding) for more information.
248
-
249
- ## Environment Variables
250
-
251
- Vitest exclusively autoloads environment variables prefixed with `VITE_` from `.env` files to maintain compatibility with frontend-related tests, adhering to [Vite's established convention](https://vitejs.dev/guide/env-and-mode.html#env-files). To load every environmental variable from `.env` files anyway, you can use `loadEnv` method imported from `vite`:
252
-
253
- ```ts [vitest.config.ts]
254
- import { loadEnv } from 'vite'
255
- import { defineConfig } from 'vitest/config'
256
-
257
- export default defineConfig(({ mode }) => ({
258
- test: {
259
- // mode defines what ".env.{mode}" file to choose if exists
260
- env: loadEnv(mode, process.cwd(), ''),
261
- },
262
- }))
263
- ```
264
-
265
- ## Unhandled Errors
266
-
267
- By default, Vitest catches and reports all [unhandled rejections](https://developer.mozilla.org/en-US/docs/Web/API/Window/unhandledrejection_event), [uncaught exceptions](https://nodejs.org/api/process.html#event-uncaughtexception) (in Node.js) and [error](https://developer.mozilla.org/en-US/docs/Web/API/Window/error_event) events (in the [browser](/guide/browser/)).
268
-
269
- You can disable this behaviour by catching them manually. Vitest assumes the callback is handled by you and won't report the error.
270
-
271
- ::: code-group
272
- ```ts [setup.node.js]
273
- // in Node.js
274
- process.on('unhandledRejection', () => {
275
- // your own handler
276
- })
277
-
278
- process.on('uncaughtException', () => {
279
- // your own handler
280
- })
281
- ```
282
- ```ts [setup.browser.js]
283
- // in the browser
284
- window.addEventListener('error', () => {
285
- // your own handler
286
- })
287
-
288
- window.addEventListener('unhandledrejection', () => {
289
- // your own handler
290
- })
291
- ```
292
- :::
293
-
294
- Alternatively, you can also ignore reported errors with a [`dangerouslyIgnoreUnhandledErrors`](/config/dangerouslyignoreunhandlederrors) option. Vitest will still report them, but they won't affect the test result (exit code won't be changed).
295
-
296
- If you need to test that error was not caught, you can create a test that looks like this:
297
-
298
- ```ts
299
- test('my function throws uncaught error', async ({ onTestFinished }) => {
300
- const unhandledRejectionListener = vi.fn()
301
- process.on('unhandledRejection', unhandledRejectionListener)
302
- onTestFinished(() => {
303
- process.off('unhandledRejection', unhandledRejectionListener)
304
- })
305
-
306
- callMyFunctionThatRejectsError()
307
-
308
- await expect.poll(unhandledRejectionListener).toHaveBeenCalled()
309
- })
310
- ```