@vitest-agent/mcp 1.1.0 → 1.3.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 (209) hide show
  1. package/README.md +1 -2
  2. package/index.d.ts +3 -2
  3. package/index.js +1 -1
  4. package/package.json +1 -1
  5. package/server.js +2 -4
  6. package/tools/run-tests.js +17 -3
  7. package/patterns/_meta.json +0 -67
  8. package/patterns/authoring-a-custom-vitest-agent-reporter.md +0 -82
  9. package/patterns/known-issues-and-caveats.md +0 -52
  10. package/patterns/operating-vitest-agent-as-an-agent.md +0 -62
  11. package/patterns/running-tests-via-mcp.md +0 -103
  12. package/patterns/silencing-leaking-output-in-tests.md +0 -91
  13. package/patterns/testing-effect-schema-definitions.md +0 -71
  14. package/patterns/testing-effect-services-with-mock-layers.md +0 -63
  15. package/resources/index.js +0 -166
  16. package/resources/indexes.js +0 -77
  17. package/resources/manifest-schema.js +0 -46
  18. package/resources/paths.js +0 -20
  19. package/resources/patterns.js +0 -22
  20. package/resources/upstream-docs.js +0 -22
  21. package/vendor/vitest-docs/ATTRIBUTION.md +0 -5
  22. package/vendor/vitest-docs/api/advanced/artifacts.md +0 -189
  23. package/vendor/vitest-docs/api/advanced/metadata.md +0 -68
  24. package/vendor/vitest-docs/api/advanced/plugin.md +0 -168
  25. package/vendor/vitest-docs/api/advanced/reporters.md +0 -342
  26. package/vendor/vitest-docs/api/advanced/runner.md +0 -334
  27. package/vendor/vitest-docs/api/advanced/test-case.md +0 -302
  28. package/vendor/vitest-docs/api/advanced/test-collection.md +0 -89
  29. package/vendor/vitest-docs/api/advanced/test-module.md +0 -140
  30. package/vendor/vitest-docs/api/advanced/test-project.md +0 -321
  31. package/vendor/vitest-docs/api/advanced/test-specification.md +0 -96
  32. package/vendor/vitest-docs/api/advanced/test-suite.md +0 -230
  33. package/vendor/vitest-docs/api/advanced/vitest.md +0 -684
  34. package/vendor/vitest-docs/api/assert-type.md +0 -22
  35. package/vendor/vitest-docs/api/assert.md +0 -1960
  36. package/vendor/vitest-docs/api/browser/assertions.md +0 -1277
  37. package/vendor/vitest-docs/api/browser/commands.md +0 -154
  38. package/vendor/vitest-docs/api/browser/context.md +0 -338
  39. package/vendor/vitest-docs/api/browser/interactivity.md +0 -681
  40. package/vendor/vitest-docs/api/browser/locators.md +0 -1171
  41. package/vendor/vitest-docs/api/browser/react.md +0 -346
  42. package/vendor/vitest-docs/api/browser/svelte.md +0 -292
  43. package/vendor/vitest-docs/api/browser/vue.md +0 -222
  44. package/vendor/vitest-docs/api/describe.md +0 -374
  45. package/vendor/vitest-docs/api/expect-typeof.md +0 -571
  46. package/vendor/vitest-docs/api/expect.md +0 -2304
  47. package/vendor/vitest-docs/api/hooks.md +0 -463
  48. package/vendor/vitest-docs/api/mock.md +0 -701
  49. package/vendor/vitest-docs/api/test.md +0 -926
  50. package/vendor/vitest-docs/api/vi.md +0 -1372
  51. package/vendor/vitest-docs/config/alias.md +0 -13
  52. package/vendor/vitest-docs/config/allowonly.md +0 -32
  53. package/vendor/vitest-docs/config/api.md +0 -27
  54. package/vendor/vitest-docs/config/attachmentsdir.md +0 -6
  55. package/vendor/vitest-docs/config/bail.md +0 -9
  56. package/vendor/vitest-docs/config/benchmark.md +0 -65
  57. package/vendor/vitest-docs/config/browser/api.md +0 -23
  58. package/vendor/vitest-docs/config/browser/commands.md +0 -6
  59. package/vendor/vitest-docs/config/browser/connecttimeout.md +0 -10
  60. package/vendor/vitest-docs/config/browser/detailspanelposition.md +0 -38
  61. package/vendor/vitest-docs/config/browser/enabled.md +0 -40
  62. package/vendor/vitest-docs/config/browser/expect.md +0 -250
  63. package/vendor/vitest-docs/config/browser/headless.md +0 -7
  64. package/vendor/vitest-docs/config/browser/instances.md +0 -47
  65. package/vendor/vitest-docs/config/browser/isolate.md +0 -11
  66. package/vendor/vitest-docs/config/browser/locators.md +0 -24
  67. package/vendor/vitest-docs/config/browser/orchestratorscripts.md +0 -39
  68. package/vendor/vitest-docs/config/browser/playwright.md +0 -214
  69. package/vendor/vitest-docs/config/browser/preview.md +0 -32
  70. package/vendor/vitest-docs/config/browser/provider.md +0 -79
  71. package/vendor/vitest-docs/config/browser/screenshotdirectory.md +0 -6
  72. package/vendor/vitest-docs/config/browser/screenshotfailures.md +0 -6
  73. package/vendor/vitest-docs/config/browser/testerhtmlpath.md +0 -5
  74. package/vendor/vitest-docs/config/browser/trace.md +0 -43
  75. package/vendor/vitest-docs/config/browser/trackunhandlederrors.md +0 -10
  76. package/vendor/vitest-docs/config/browser/ui.md +0 -7
  77. package/vendor/vitest-docs/config/browser/viewport.md +0 -6
  78. package/vendor/vitest-docs/config/browser/webdriverio.md +0 -64
  79. package/vendor/vitest-docs/config/cache.md +0 -26
  80. package/vendor/vitest-docs/config/chaiconfig.md +0 -29
  81. package/vendor/vitest-docs/config/clearmocks.md +0 -22
  82. package/vendor/vitest-docs/config/coverage.md +0 -455
  83. package/vendor/vitest-docs/config/css.md +0 -47
  84. package/vendor/vitest-docs/config/dangerouslyignoreunhandlederrors.md +0 -23
  85. package/vendor/vitest-docs/config/deps.md +0 -127
  86. package/vendor/vitest-docs/config/detectasyncleaks.md +0 -39
  87. package/vendor/vitest-docs/config/diff.md +0 -96
  88. package/vendor/vitest-docs/config/dir.md +0 -7
  89. package/vendor/vitest-docs/config/disableconsoleintercept.md +0 -15
  90. package/vendor/vitest-docs/config/env.md +0 -5
  91. package/vendor/vitest-docs/config/environment.md +0 -96
  92. package/vendor/vitest-docs/config/environmentoptions.md +0 -30
  93. package/vendor/vitest-docs/config/exclude.md +0 -49
  94. package/vendor/vitest-docs/config/execargv.md +0 -10
  95. package/vendor/vitest-docs/config/expandsnapshotdiff.md +0 -7
  96. package/vendor/vitest-docs/config/expect.md +0 -38
  97. package/vendor/vitest-docs/config/experimental.md +0 -510
  98. package/vendor/vitest-docs/config/faketimers.md +0 -51
  99. package/vendor/vitest-docs/config/fileparallelism.md +0 -11
  100. package/vendor/vitest-docs/config/forcereruntriggers.md +0 -19
  101. package/vendor/vitest-docs/config/globals.md +0 -42
  102. package/vendor/vitest-docs/config/globalsetup.md +0 -72
  103. package/vendor/vitest-docs/config/hideskippedtests.md +0 -7
  104. package/vendor/vitest-docs/config/hooktimeout.md +0 -7
  105. package/vendor/vitest-docs/config/include-source.md +0 -115
  106. package/vendor/vitest-docs/config/include.md +0 -71
  107. package/vendor/vitest-docs/config/includetasklocation.md +0 -17
  108. package/vendor/vitest-docs/config/index.md +0 -85
  109. package/vendor/vitest-docs/config/isolate.md +0 -13
  110. package/vendor/vitest-docs/config/logheapusage.md +0 -7
  111. package/vendor/vitest-docs/config/maxconcurrency.md +0 -9
  112. package/vendor/vitest-docs/config/maxworkers.md +0 -49
  113. package/vendor/vitest-docs/config/mockreset.md +0 -22
  114. package/vendor/vitest-docs/config/mode.md +0 -7
  115. package/vendor/vitest-docs/config/name.md +0 -111
  116. package/vendor/vitest-docs/config/onconsolelog.md +0 -25
  117. package/vendor/vitest-docs/config/onstacktrace.md +0 -32
  118. package/vendor/vitest-docs/config/onunhandlederror.md +0 -35
  119. package/vendor/vitest-docs/config/open.md +0 -7
  120. package/vendor/vitest-docs/config/outputfile.md +0 -7
  121. package/vendor/vitest-docs/config/passwithnotests.md +0 -7
  122. package/vendor/vitest-docs/config/pool.md +0 -45
  123. package/vendor/vitest-docs/config/printconsoletrace.md +0 -6
  124. package/vendor/vitest-docs/config/projects.md +0 -6
  125. package/vendor/vitest-docs/config/provide.md +0 -45
  126. package/vendor/vitest-docs/config/reporters.md +0 -69
  127. package/vendor/vitest-docs/config/resolvesnapshotpath.md +0 -36
  128. package/vendor/vitest-docs/config/restoremocks.md +0 -22
  129. package/vendor/vitest-docs/config/retry.md +0 -140
  130. package/vendor/vitest-docs/config/root.md +0 -6
  131. package/vendor/vitest-docs/config/runner.md +0 -6
  132. package/vendor/vitest-docs/config/sequence.md +0 -158
  133. package/vendor/vitest-docs/config/server.md +0 -68
  134. package/vendor/vitest-docs/config/setupfiles.md +0 -40
  135. package/vendor/vitest-docs/config/silent.md +0 -9
  136. package/vendor/vitest-docs/config/slowtestthreshold.md +0 -7
  137. package/vendor/vitest-docs/config/snapshotenvironment.md +0 -27
  138. package/vendor/vitest-docs/config/snapshotformat.md +0 -28
  139. package/vendor/vitest-docs/config/snapshotserializers.md +0 -6
  140. package/vendor/vitest-docs/config/stricttags.md +0 -30
  141. package/vendor/vitest-docs/config/tags.md +0 -141
  142. package/vendor/vitest-docs/config/teardowntimeout.md +0 -7
  143. package/vendor/vitest-docs/config/testnamepattern.md +0 -21
  144. package/vendor/vitest-docs/config/testtimeout.md +0 -7
  145. package/vendor/vitest-docs/config/typecheck.md +0 -77
  146. package/vendor/vitest-docs/config/ui.md +0 -15
  147. package/vendor/vitest-docs/config/unstubenvs.md +0 -20
  148. package/vendor/vitest-docs/config/unstubglobals.md +0 -20
  149. package/vendor/vitest-docs/config/update.md +0 -16
  150. package/vendor/vitest-docs/config/vmmemorylimit.md +0 -30
  151. package/vendor/vitest-docs/config/watch.md +0 -11
  152. package/vendor/vitest-docs/config/watchtriggerpatterns.md +0 -29
  153. package/vendor/vitest-docs/guide/advanced/index.md +0 -147
  154. package/vendor/vitest-docs/guide/advanced/pool.md +0 -148
  155. package/vendor/vitest-docs/guide/advanced/reporters.md +0 -93
  156. package/vendor/vitest-docs/guide/advanced/tests.md +0 -125
  157. package/vendor/vitest-docs/guide/browser/aria-snapshots.md +0 -470
  158. package/vendor/vitest-docs/guide/browser/component-testing.md +0 -571
  159. package/vendor/vitest-docs/guide/browser/index.md +0 -630
  160. package/vendor/vitest-docs/guide/browser/multiple-setups.md +0 -121
  161. package/vendor/vitest-docs/guide/browser/trace-view.md +0 -126
  162. package/vendor/vitest-docs/guide/browser/visual-regression-testing.md +0 -734
  163. package/vendor/vitest-docs/guide/cli-generated.md +0 -972
  164. package/vendor/vitest-docs/guide/cli.md +0 -234
  165. package/vendor/vitest-docs/guide/common-errors.md +0 -163
  166. package/vendor/vitest-docs/guide/coverage.md +0 -515
  167. package/vendor/vitest-docs/guide/debugging.md +0 -127
  168. package/vendor/vitest-docs/guide/environment.md +0 -101
  169. package/vendor/vitest-docs/guide/extending-matchers.md +0 -160
  170. package/vendor/vitest-docs/guide/features.md +0 -310
  171. package/vendor/vitest-docs/guide/filtering.md +0 -175
  172. package/vendor/vitest-docs/guide/ide.md +0 -43
  173. package/vendor/vitest-docs/guide/improving-performance.md +0 -245
  174. package/vendor/vitest-docs/guide/in-source.md +0 -159
  175. package/vendor/vitest-docs/guide/index.md +0 -128
  176. package/vendor/vitest-docs/guide/learn/async.md +0 -147
  177. package/vendor/vitest-docs/guide/learn/debugging-tests.md +0 -210
  178. package/vendor/vitest-docs/guide/learn/matchers.md +0 -277
  179. package/vendor/vitest-docs/guide/learn/mock-functions.md +0 -277
  180. package/vendor/vitest-docs/guide/learn/setup-teardown.md +0 -240
  181. package/vendor/vitest-docs/guide/learn/snapshots.md +0 -166
  182. package/vendor/vitest-docs/guide/learn/testing-in-practice.md +0 -430
  183. package/vendor/vitest-docs/guide/learn/writing-tests-with-ai.md +0 -127
  184. package/vendor/vitest-docs/guide/learn/writing-tests.md +0 -231
  185. package/vendor/vitest-docs/guide/lifecycle.md +0 -379
  186. package/vendor/vitest-docs/guide/migration.md +0 -863
  187. package/vendor/vitest-docs/guide/mocking/classes.md +0 -158
  188. package/vendor/vitest-docs/guide/mocking/dates.md +0 -52
  189. package/vendor/vitest-docs/guide/mocking/file-system.md +0 -74
  190. package/vendor/vitest-docs/guide/mocking/functions.md +0 -61
  191. package/vendor/vitest-docs/guide/mocking/globals.md +0 -20
  192. package/vendor/vitest-docs/guide/mocking/modules.md +0 -414
  193. package/vendor/vitest-docs/guide/mocking/requests.md +0 -114
  194. package/vendor/vitest-docs/guide/mocking/timers.md +0 -48
  195. package/vendor/vitest-docs/guide/mocking.md +0 -239
  196. package/vendor/vitest-docs/guide/open-telemetry.md +0 -156
  197. package/vendor/vitest-docs/guide/parallelism.md +0 -82
  198. package/vendor/vitest-docs/guide/profiling-test-performance.md +0 -243
  199. package/vendor/vitest-docs/guide/projects.md +0 -291
  200. package/vendor/vitest-docs/guide/recipes.md +0 -59
  201. package/vendor/vitest-docs/guide/reporters.md +0 -723
  202. package/vendor/vitest-docs/guide/snapshot.md +0 -620
  203. package/vendor/vitest-docs/guide/test-annotations.md +0 -103
  204. package/vendor/vitest-docs/guide/test-context.md +0 -902
  205. package/vendor/vitest-docs/guide/test-tags.md +0 -314
  206. package/vendor/vitest-docs/guide/testing-types.md +0 -149
  207. package/vendor/vitest-docs/guide/ui.md +0 -160
  208. package/vendor/vitest-docs/guide/using-plugins.md +0 -5
  209. package/vendor/vitest-docs/manifest.json +0 -1691
@@ -1,68 +0,0 @@
1
- # Task Metadata <Badge type="danger">advanced</Badge>
2
-
3
- If you are developing a custom reporter or using Vitest Node.js API, you might find it useful to pass data from tests that are being executed in various contexts to your reporter or custom Vitest handler.
4
-
5
- To accomplish this, relying on the [test context](/guide/test-context) is not feasible since it cannot be serialized. However, with Vitest, you can utilize the `meta` property available on every task (suite or test) to share data between your tests and the Node.js process. It's important to note that this communication is one-way only, as the `meta` property can only be modified from within the test context. Any changes made within the Node.js context will not be visible in your tests.
6
-
7
- You can populate `meta` property on test context or inside `beforeAll`/`afterAll` hooks for suite tasks.
8
-
9
- ```ts
10
- afterAll((suite) => {
11
- suite.meta.done = true
12
- })
13
-
14
- test('custom', ({ task }) => {
15
- task.meta.custom = 'some-custom-handler'
16
- })
17
- ```
18
-
19
- Once a test is completed, Vitest will send a task including the result and `meta` to the Node.js process using RPC, and then report it in `onTestCaseResult` and other hooks that have access to tasks. To process this test case, you can utilize the `onTestCaseResult` method available in your reporter implementation:
20
-
21
- ```ts [custom-reporter.js]
22
- import type { Reporter, TestCase, TestModule } from 'vitest/node'
23
-
24
- export default {
25
- onTestCaseResult(testCase: TestCase) {
26
- // custom === 'some-custom-handler' ✅
27
- const { custom } = testCase.meta()
28
- },
29
- onTestRunEnd(testModule: TestModule) {
30
- testModule.meta().done === true
31
- testModule.children.at(0).meta().custom === 'some-custom-handler'
32
- }
33
- } satisfies Reporter
34
- ```
35
-
36
- ::: danger BEWARE
37
- Vitest uses different methods to communicate with the Node.js process.
38
-
39
- - If Vitest runs tests inside worker threads, it will send data via [message port](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort)
40
- - If Vitest uses child process, the data will be send as a serialized Buffer via [`process.send`](https://nodejs.org/api/process.html#processsendmessage-sendhandle-options-callback) API
41
- - If Vitest runs tests in the browser, the data will be stringified using [flatted](https://npmx.dev/package/flatted) package
42
-
43
- This property is also present on every test in the `json` reporter, so make sure that data can be serialized into JSON.
44
-
45
- Also, make sure you serialize [Error properties](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#error_types) before you set them.
46
- :::
47
-
48
- You can also get this information from Vitest state when tests finished running:
49
-
50
- ```ts
51
- const vitest = await createVitest('test')
52
- const { testModules } = await vitest.start()
53
-
54
- const testModule = testModules[0]
55
- testModule.meta().done === true
56
- testModule.children.at(0).meta().custom === 'some-custom-handler'
57
- ```
58
-
59
- It's also possible to extend type definitions when using TypeScript:
60
-
61
- ```ts
62
- declare module 'vitest' {
63
- interface TaskMeta {
64
- done?: boolean
65
- custom?: string
66
- }
67
- }
68
- ```
@@ -1,168 +0,0 @@
1
- # Plugin API <Version>3.1.0</Version> {#plugin-api}
2
-
3
- ::: warning
4
- This is an advanced API. If you just want to [run tests](/guide/), you probably don't need this. It is primarily used by library authors.
5
-
6
- This guide assumes you know how to work with [Vite plugins](https://vite.dev/guide/api-plugin.html).
7
- :::
8
-
9
- Vitest supports a `configureVitest` [plugin](https://vite.dev/guide/api-plugin.html) hook since version 3.1.
10
-
11
- ::: code-group
12
- ```ts [only vitest]
13
- import type { Vite, VitestPluginContext } from 'vitest/node'
14
-
15
- export function plugin(): Vite.Plugin {
16
- return {
17
- name: 'vitest:my-plugin',
18
- configureVitest(context: VitestPluginContext) {
19
- // ...
20
- }
21
- }
22
- }
23
- ```
24
- ```ts [vite and vitest]
25
- /// <reference types="vitest/config" />
26
-
27
- import type { Plugin } from 'vite'
28
-
29
- export function plugin(): Plugin {
30
- return {
31
- name: 'vitest:my-plugin',
32
- transform() {
33
- // ...
34
- },
35
- configureVitest(context) {
36
- // ...
37
- }
38
- }
39
- }
40
- ```
41
- :::
42
-
43
- ::: tip TypeScript
44
- Vitest re-exports all Vite type-only imports via a `Vite` namespace, which you can use to keep your versions in sync. However, if you are writing a plugin for both Vite and Vitest, you can continue using the `Plugin` type from the `vite` entrypoint. Just make sure you have `vitest/config` referenced somewhere so that `configureVitest` is augmented correctly:
45
-
46
- ```ts
47
- /// <reference types="vitest/config" />
48
- ```
49
- :::
50
-
51
- Unlike [`reporter.onInit`](/api/advanced/reporters#oninit), this hooks runs early in Vitest lifecycle allowing you to make changes to configuration like `coverage` and `reporters`. A more notable change is that you can manipulate the global config from a [test project](/guide/projects) if your plugin is defined in the project and not in the global config.
52
-
53
- ## Context
54
-
55
- ### project
56
-
57
- The current [test project](./test-project) that the plugin belongs to.
58
-
59
- ::: warning Browser Mode
60
- Note that if you are relying on a browser feature, the `project.browser` field is not set yet. Use [`reporter.onBrowserInit`](./reporters#onbrowserinit) event instead.
61
- :::
62
-
63
- ### vitest
64
-
65
- The global [Vitest](./vitest) instance. You can change the global configuration by directly mutating the `vitest.config` property:
66
-
67
- ```ts
68
- vitest.config.coverage.enabled = false
69
- vitest.config.reporters.push([['my-reporter', {}]])
70
- ```
71
-
72
- ::: warning Config is Resolved
73
- Note that Vitest already resolved the config, so some types might be different from the usual user configuration. This also means that some properties will not be resolved again, like `setupFile`. If you are adding new files, make sure to resolve it first.
74
-
75
- At this point reporters are not created yet, so modifying `vitest.reporters` will have no effect because it will be overwritten. If you need to inject your own reporter, modify the config instead.
76
- :::
77
-
78
- ### injectTestProjects
79
-
80
- ```ts
81
- function injectTestProjects(
82
- config: TestProjectConfiguration | TestProjectConfiguration[]
83
- ): Promise<TestProject[]>
84
- ```
85
-
86
- This methods accepts a config glob pattern, a filepath to the config or an inline configuration. It returns an array of resolved [test projects](./test-project).
87
-
88
- ```ts
89
- // inject a single project with a custom alias
90
- const newProjects = await injectTestProjects({
91
- // you can inherit the current project config by referencing `extends`
92
- // note that you cannot have a project with the name that already exists,
93
- // so it's a good practice to define a custom name
94
- extends: project.vite.config.configFile,
95
- test: {
96
- name: 'my-custom-alias',
97
- alias: {
98
- customAlias: resolve('./custom-path.js'),
99
- },
100
- },
101
- })
102
- ```
103
-
104
- ::: warning Projects are Filtered
105
- Vitest filters projects during the config resolution, so if the user defined a filter, injected project might not be resolved unless it [matches the filter](./vitest#matchesprojectfilter). You can update the filter via the `vitest.config.project` option to always include your test project:
106
-
107
- ```ts
108
- vitest.config.project.push('my-project-name')
109
- ```
110
-
111
- Note that this will only affect projects injected with [`injectTestProjects`](#injecttestprojects) method.
112
- :::
113
-
114
- ::: tip Referencing the Current Config
115
- If you want to keep the user configuration, you can specify the `extends` property. All other properties will be merged with the user defined config.
116
-
117
- The project's `configFile` can be accessed in Vite's config: `project.vite.config.configFile`.
118
-
119
- Note that this will also inherit the `name` - Vitest doesn't allow multiple projects with the same name, so this will throw an error. Make sure you specified a different name. You can access the current name via the `project.name` property and all used names are available in the `vitest.projects` array.
120
- :::
121
-
122
- ### experimental_defineCacheKeyGenerator <Version type="experimental">4.0.11</Version> <Experimental /> {#definecachekeygenerator}
123
-
124
- ```ts
125
- interface CacheKeyIdGeneratorContext {
126
- environment: DevEnvironment
127
- id: string
128
- sourceCode: string
129
- }
130
-
131
- function experimental_defineCacheKeyGenerator(
132
- callback: (context: CacheKeyIdGeneratorContext) => string | undefined | null | false
133
- ): void
134
- ```
135
-
136
- Define a generator that will be applied before hashing the cache key.
137
-
138
- Use this to make sure Vitest generates correct hash. It is a good idea to define this function if your plugin can be registered with different options.
139
-
140
- This is called only if [`experimental.fsModuleCache`](/config/experimental#experimental-fsmodulecache) is defined.
141
-
142
- ```ts
143
- interface PluginOptions {
144
- replacePropertyKey: string
145
- replacePropertyValue: string
146
- }
147
-
148
- export function plugin(options: PluginOptions) {
149
- return {
150
- name: 'plugin-that-replaces-property',
151
- transform(code) {
152
- return code.replace(
153
- options.replacePropertyKey,
154
- options.replacePropertyValue
155
- )
156
- },
157
- configureVitest({ experimental_defineCacheKeyGenerator }) {
158
- experimental_defineCacheKeyGenerator(() => {
159
- // since these options affect the transform result,
160
- // return them together as a unique string
161
- return options.replacePropertyKey + options.replacePropertyValue
162
- })
163
- }
164
- }
165
- }
166
- ```
167
-
168
- If `false` is returned, the module will not be cached on the file system.
@@ -1,342 +0,0 @@
1
- # Reporters
2
-
3
- ::: warning
4
- This is an advanced API. If you just want to configure built-in reporters, read the ["Reporters"](/guide/reporters) guide.
5
- :::
6
-
7
- Vitest has its own test run lifecycle. These are represented by reporter's methods:
8
-
9
- - [`onInit`](#oninit)
10
- - [`onTestRunStart`](#ontestrunstart)
11
- - [`onTestModuleQueued`](#ontestmodulequeued)
12
- - [`onTestModuleCollected`](#ontestmodulecollected)
13
- - [`onTestModuleStart`](#ontestmodulestart)
14
- - [`onTestSuiteReady`](#ontestsuiteready)
15
- - [`onHookStart(beforeAll)`](#onhookstart)
16
- - [`onHookEnd(beforeAll)`](#onhookend)
17
- - [`onTestCaseReady`](#ontestcaseready)
18
- - [`onTestCaseAnnotate`](#ontestcaseannotate) <Version>3.2.0</Version>
19
- - [`onTestCaseArtifactRecord`](#ontestcaseartifactrecord) <Version type="experimental">4.0.11</Version>
20
- - [`onHookStart(beforeEach)`](#onhookstart)
21
- - [`onHookEnd(beforeEach)`](#onhookend)
22
- - [`onHookStart(afterEach)`](#onhookstart)
23
- - [`onHookEnd(afterEach)`](#onhookend)
24
- - [`onTestCaseResult`](#ontestcaseresult)
25
- - [`onHookStart(afterAll)`](#onhookstart)
26
- - [`onHookEnd(afterAll)`](#onhookend)
27
- - [`onTestSuiteResult`](#ontestsuiteresult)
28
- - [`onTestModuleEnd`](#ontestmoduleend)
29
- - [`onCoverage`](#oncoverage)
30
- - [`onTestRunEnd`](#ontestrunend)
31
-
32
- Tests and suites within a single module will be reported in order unless they were skipped. All skipped tests are reported at the end of suite/module.
33
-
34
- Note that since test modules can run in parallel, Vitest will report them in parallel.
35
-
36
- This guide lists all supported reporter methods. However, don't forget that instead of creating your own reporter, you can [extend existing one](/guide/advanced/reporters) instead:
37
-
38
- ```ts [custom-reporter.js]
39
- import { BaseReporter } from 'vitest/node'
40
-
41
- export default class CustomReporter extends BaseReporter {
42
- onTestRunEnd(testModules, errors) {
43
- console.log(testModule.length, 'tests finished running')
44
- super.onTestRunEnd(testModules, errors)
45
- }
46
- }
47
- ```
48
-
49
- ## onInit
50
-
51
- ```ts
52
- function onInit(vitest: Vitest): Awaitable<void>
53
- ```
54
-
55
- This method is called when [Vitest](/api/advanced/vitest) was initiated or started, but before the tests were filtered.
56
-
57
- ::: info
58
- Internally this method is called inside [`vitest.start`](/api/advanced/vitest#start), [`vitest.standalone`](/api/advanced/vitest#standalone) or [`vitest.mergeReports`](/api/advanced/vitest#mergereports). If you are using programmatic API, make sure to call either one depending on your needs before calling [`vitest.runTestSpecifications`](/api/advanced/vitest#runtestspecifications), for example. Built-in CLI will always run methods in correct order.
59
- :::
60
-
61
- Note that you can also get access to `vitest` instance from test cases, suites and test modules via a [`project`](/api/advanced/test-project) property, but it might also be useful to store a reference to `vitest` in this method.
62
-
63
- ::: details Example
64
- ```ts
65
- import type { Reporter, TestSpecification, Vitest } from 'vitest/node'
66
-
67
- class MyReporter implements Reporter {
68
- private vitest!: Vitest
69
-
70
- onInit(vitest: Vitest) {
71
- this.vitest = vitest
72
- }
73
-
74
- onTestRunStart(specifications: TestSpecification[]) {
75
- console.log(
76
- specifications.length,
77
- 'test files will run in',
78
- this.vitest.config.root,
79
- )
80
- }
81
- }
82
-
83
- export default new MyReporter()
84
- ```
85
- :::
86
-
87
- ## onBrowserInit {#onbrowserinit}
88
-
89
- ```ts
90
- function onBrowserInit(project: TestProject): Awaitable<void>
91
- ```
92
-
93
- This method is called when the browser instance is initiated. It receives an instance of the project for which the browser is initiated. `project.browser` will always be defined when this method is called.
94
-
95
- ## onTestRunStart
96
-
97
- ```ts
98
- function onTestRunStart(
99
- specifications: TestSpecification[]
100
- ): Awaitable<void>
101
- ```
102
-
103
- This method is called when a new test run has started. It receives an array of [test specifications](/api/advanced/test-specification) scheduled to run. This array is readonly and available only for information purposes.
104
-
105
- If Vitest didn't find any test files to run, this event will be invoked with an empty array, and then [`onTestRunEnd`](#ontestrunend) will be called immediately after.
106
-
107
- ::: details Example
108
- ```ts
109
- import type { Reporter, TestSpecification } from 'vitest/node'
110
-
111
- class MyReporter implements Reporter {
112
- onTestRunStart(specifications: TestSpecification[]) {
113
- console.log(specifications.length, 'test files will run')
114
- }
115
- }
116
-
117
- export default new MyReporter()
118
- ```
119
- :::
120
-
121
- ## onTestRunEnd
122
-
123
- ```ts
124
- function onTestRunEnd(
125
- testModules: ReadonlyArray<TestModule>,
126
- unhandledErrors: ReadonlyArray<SerializedError>,
127
- reason: TestRunEndReason
128
- ): Awaitable<void>
129
- ```
130
-
131
- This method is called after all tests have finished running and the coverage merged all reports, if it's enabled. Note that you can get the coverage information in [`onCoverage`](#oncoverage) hook.
132
-
133
- It receives a readonly list of test modules. You can iterate over it via a [`testModule.children`](/api/advanced/test-collection) property to report the state and errors, if any.
134
-
135
- The second argument is a readonly list of unhandled errors that Vitest wasn't able to attribute to any test. These can happen outside of the test run because of an error in a plugin, or inside the test run as a side-effect of a non-awaited function (for example, a timeout that threw an error after the test has finished running).
136
-
137
- The third argument indicated why the test run was finished:
138
-
139
- - `passed`: test run was finished normally and there are no errors
140
- - `failed`: test run has at least one error (due to a syntax error during collection or an actual error during test execution)
141
- - `interrupted`: test was interrupted by [`vitest.cancelCurrentRun`](/api/advanced/vitest#cancelcurrentrun) call or `Ctrl+C` was pressed in the terminal (note that it's still possible to have failed tests in this case)
142
-
143
- If Vitest didn't find any test files to run, this event will be invoked with empty arrays of modules and errors, and the state will depend on the value of [`config.passWithNoTests`](/config/passwithnotests).
144
-
145
- ::: details Example
146
- ```ts
147
- import type {
148
- Reporter,
149
- SerializedError,
150
- TestModule,
151
- TestRunEndReason,
152
- TestSpecification
153
- } from 'vitest/node'
154
-
155
- class MyReporter implements Reporter {
156
- onTestRunEnd(
157
- testModules: ReadonlyArray<TestModule>,
158
- unhandledErrors: ReadonlyArray<SerializedError>,
159
- reason: TestRunEndReason,
160
- ) {
161
- if (reason === 'passed') {
162
- testModules.forEach(module => console.log(module.moduleId, 'succeeded'))
163
- }
164
- else if (reason === 'failed') {
165
- // note that this will skip possible errors in suites
166
- // you can get them from testSuite.errors()
167
- for (const testCase of testModules.children.allTests()) {
168
- if (testCase.result().state === 'failed') {
169
- console.log(testCase.fullName, 'in', testCase.module.moduleId, 'failed')
170
- console.log(testCase.result().errors)
171
- }
172
- }
173
- }
174
- else {
175
- console.log('test run was interrupted, skipping report')
176
- }
177
- }
178
- }
179
-
180
- export default new MyReporter()
181
- ```
182
- :::
183
-
184
- ## onCoverage
185
-
186
- ```ts
187
- function onCoverage(coverage: unknown): Awaitable<void>
188
- ```
189
-
190
- This hook is called after coverage results have been processed. Coverage provider's reporters are called after this hook. The typings of `coverage` depends on the `coverage.provider`. For Vitest's default built-in providers you can import the types from `istanbul-lib-coverage` package:
191
-
192
- ```ts
193
- import type { CoverageMap } from 'istanbul-lib-coverage'
194
-
195
- declare function onCoverage(coverage: CoverageMap): Awaitable<void>
196
- ```
197
-
198
- If Vitest didn't perform any coverage, this hook is not called.
199
-
200
- ## onTestModuleQueued
201
-
202
- ```ts
203
- function onTestModuleQueued(testModule: TestModule): Awaitable<void>
204
- ```
205
-
206
- This method is called right before Vitest imports the setup file and the test module itself. This means that `testModule` will have no [`children`](/api/advanced/test-suite#children) yet, but you can start reporting it as the next test to run.
207
-
208
- ## onTestModuleCollected
209
-
210
- ```ts
211
- function onTestModuleCollected(testModule: TestModule): Awaitable<void>
212
- ```
213
-
214
- This method is called when all tests inside the file were collected, meaning [`testModule.children`](/api/advanced/test-suite#children) collection is populated, but tests don't have any results yet.
215
-
216
- ## onTestModuleStart
217
-
218
- ```ts
219
- function onTestModuleStart(testModule: TestModule): Awaitable<void>
220
- ```
221
-
222
- This method is called right after [`onTestModuleCollected`](#ontestmodulecollected) unless Vitest runs in collection mode ([`vitest.collect()`](/api/advanced/vitest#collect) or `vitest collect` in the CLI), in this case it will not be called at all because there are no tests to run.
223
-
224
- ## onTestModuleEnd
225
-
226
- ```ts
227
- function onTestModuleEnd(testModule: TestModule): Awaitable<void>
228
- ```
229
-
230
- This method is called when every test in the module finished running. This means, every test inside [`testModule.children`](/api/advanced/test-suite#children) will have a `test.result()` that is not equal to `pending`.
231
-
232
- ## onHookStart
233
-
234
- ```ts
235
- function onHookStart(context: ReportedHookContext): Awaitable<void>
236
- ```
237
-
238
- This method is called when any of these hooks have started running:
239
-
240
- - `beforeAll`
241
- - `afterAll`
242
- - `beforeEach`
243
- - `afterEach`
244
-
245
- If `beforeAll` or `afterAll` are started, the `entity` will be either [`TestSuite`](/api/advanced/test-suite) or [`TestModule`](/api/advanced/test-module).
246
-
247
- If `beforeEach` or `afterEach` are started, the `entity` will always be [`TestCase`](/api/advanced/test-case).
248
-
249
- ::: warning
250
- `onHookStart` method will not be called if the hook did not run during the test run.
251
- :::
252
-
253
- ## onHookEnd
254
-
255
- ```ts
256
- function onHookEnd(context: ReportedHookContext): Awaitable<void>
257
- ```
258
-
259
- This method is called when any of these hooks have finished running:
260
-
261
- - `beforeAll`
262
- - `afterAll`
263
- - `beforeEach`
264
- - `afterEach`
265
-
266
- If `beforeAll` or `afterAll` have finished, the `entity` will be either [`TestSuite`](/api/advanced/test-suite) or [`TestModule`](/api/advanced/test-module).
267
-
268
- If `beforeEach` or `afterEach` have finished, the `entity` will always be [`TestCase`](/api/advanced/test-case).
269
-
270
- ::: warning
271
- `onHookEnd` method will not be called if the hook did not run during the test run.
272
- :::
273
-
274
- ## onTestSuiteReady
275
-
276
- ```ts
277
- function onTestSuiteReady(testSuite: TestSuite): Awaitable<void>
278
- ```
279
-
280
- This method is called before the suite starts to run its tests. This method is also called if the suite was skipped.
281
-
282
- If the file doesn't have any suites, this method will not be called. Consider using `onTestModuleStart` to cover this use case.
283
-
284
- ## onTestSuiteResult
285
-
286
- ```ts
287
- function onTestSuiteResult(testSuite: TestSuite): Awaitable<void>
288
- ```
289
-
290
- This method is called after the suite has finished running tests. This method is also called if the suite was skipped.
291
-
292
- If the file doesn't have any suites, this method will not be called. Consider using `onTestModuleEnd` to cover this use case.
293
-
294
- ## onTestCaseReady
295
-
296
- ```ts
297
- function onTestCaseReady(testCase: TestCase): Awaitable<void>
298
- ```
299
-
300
- This method is called before the test starts to run or it was skipped. Note that `beforeEach` and `afterEach` hooks are considered part of the test because they can influence the result.
301
-
302
- ::: warning
303
- Notice that it's possible to have [`testCase.result()`](/api/advanced/test-case#result) with `passed` or `failed` state already when `onTestCaseReady` is called. This can happen if test was running too fast and both `onTestCaseReady` and `onTestCaseResult` were scheduled to run in the same microtask.
304
- :::
305
-
306
- ## onTestCaseResult
307
-
308
- ```ts
309
- function onTestCaseResult(testCase: TestCase): Awaitable<void>
310
- ```
311
-
312
- This method is called when the test has finished running or was just skipped. Note that this will be called after the `afterEach` hook is finished, if there are any.
313
-
314
- At this point, [`testCase.result()`](/api/advanced/test-case#result) will have non-pending state.
315
-
316
- ## onTestCaseAnnotate <Version>3.2.0</Version> {#ontestcaseannotate}
317
-
318
- ```ts
319
- function onTestCaseAnnotate(
320
- testCase: TestCase,
321
- annotation: TestAnnotation,
322
- ): Awaitable<void>
323
- ```
324
-
325
- The `onTestCaseAnnotate` hook is associated with the [`context.annotate`](/guide/test-context#annotate) method. When `annotate` is invoked, Vitest serialises it and sends the same attachment to the main thread where reporter can interact with it.
326
-
327
- If the path is specified, Vitest stores it in a separate directory (configured by [`attachmentsDir`](/config/attachmentsdir)) and modifies the `path` property to reference it.
328
-
329
- ## onTestCaseArtifactRecord <Version type="experimental">4.0.11</Version> {#ontestcaseartifactrecord}
330
-
331
- ```ts
332
- function onTestCaseArtifactRecord(
333
- testCase: TestCase,
334
- artifact: TestArtifact,
335
- ): Awaitable<void>
336
- ```
337
-
338
- The `onTestCaseArtifactRecord` hook is associated with the [`recordArtifact`](/api/advanced/artifacts#recordartifact) utility. When `recordArtifact` is invoked, Vitest serialises it and sends the same attachment to the main thread where reporter can interact with it.
339
-
340
- If the path is specified, Vitest stores it in a separate directory (configured by [`attachmentsDir`](/config/attachmentsdir)) and modifies the `path` property to reference it.
341
-
342
- Note: annotations, [even though they're built on top of this feature](/api/advanced/artifacts#relationship-with-annotations), won't hit this hook and won't appear in the `task.artifacts` array for backwards compatibility reasons until the next major version.