@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,89 +0,0 @@
1
- # TestCollection
2
-
3
- `TestCollection` represents a collection of top-level [suites](/api/advanced/test-suite) and [tests](/api/advanced/test-case) in a suite or a module. It also provides useful methods to iterate over itself.
4
-
5
- ::: info
6
- Most methods return an iterator instead of an array for better performance in case you don't need every item in the collection. If you prefer working with array, you can spread the iterator: `[...children.allSuites()]`.
7
-
8
- Also note that the collection itself is an iterator:
9
-
10
- ```ts
11
- for (const child of module.children) {
12
- console.log(child.type, child.name)
13
- }
14
- ```
15
- :::
16
-
17
- ## size
18
-
19
- The number of tests and suites in the collection.
20
-
21
- ::: warning
22
- This number includes only tests and suites at the top-level, it doesn't include nested suites and tests.
23
- :::
24
-
25
- ## at
26
-
27
- ```ts
28
- function at(index: number): TestCase | TestSuite | undefined
29
- ```
30
-
31
- Returns the test or suite at a specific index. This method accepts negative indexes.
32
-
33
- ## array
34
-
35
- ```ts
36
- function array(): (TestCase | TestSuite)[]
37
- ```
38
-
39
- The same collection but as an array. This is useful if you want to use `Array` methods like `map` and `filter` that are not supported by the `TaskCollection` implementation.
40
-
41
- ## allSuites
42
-
43
- ```ts
44
- function allSuites(): Generator<TestSuite, undefined, void>
45
- ```
46
-
47
- Filters all suites that are part of this collection and its children.
48
-
49
- ```ts
50
- for (const suite of module.children.allSuites()) {
51
- if (suite.errors().length) {
52
- console.log('failed to collect', suite.errors())
53
- }
54
- }
55
- ```
56
-
57
- ## allTests
58
-
59
- ```ts
60
- function allTests(state?: TestState): Generator<TestCase, undefined, void>
61
- ```
62
-
63
- Filters all tests that are part of this collection and its children.
64
-
65
- ```ts
66
- for (const test of module.children.allTests()) {
67
- if (test.result().state === 'pending') {
68
- console.log('test', test.fullName, 'did not finish')
69
- }
70
- }
71
- ```
72
-
73
- You can pass down a `state` value to filter tests by the state.
74
-
75
- ## tests
76
-
77
- ```ts
78
- function tests(state?: TestState): Generator<TestCase, undefined, void>
79
- ```
80
-
81
- Filters only the tests that are part of this collection. You can pass down a `state` value to filter tests by the state.
82
-
83
- ## suites
84
-
85
- ```ts
86
- function suites(): Generator<TestSuite, undefined, void>
87
- ```
88
-
89
- Filters only the suites that are part of this collection.
@@ -1,140 +0,0 @@
1
- # TestModule
2
-
3
- The `TestModule` class represents a single module in a single project. This class is only available in the main thread. Refer to the ["Runner API"](/api/advanced/runner#tasks) if you are working with runtime tasks.
4
-
5
- The `TestModule` instance always has a `type` property with the value of `module`. You can use it to distinguish between different task types:
6
-
7
- ```ts
8
- if (task.type === 'module') {
9
- task // TestModule
10
- }
11
- ```
12
-
13
- ::: warning Extending Suite Methods
14
- The `TestModule` class inherits all methods and properties from the [`TestSuite`](/api/advanced/test-suite). This guide will only list methods and properties unique to the `TestModule`.
15
- :::
16
-
17
- ## moduleId
18
-
19
- This is usually an absolute unix file path (even on Windows). It can be a virtual id if the file is not on the disk. This value corresponds to Vite's `ModuleGraph` id.
20
-
21
- ```ts
22
- 'C:/Users/Documents/project/example.test.ts' // ✅
23
- '/Users/mac/project/example.test.ts' // ✅
24
- 'C:\\Users\\Documents\\project\\example.test.ts' // ❌
25
- ```
26
-
27
- ## relativeModuleId
28
-
29
- Module id relative to the project. This is the same as `task.name` in the deprecated API.
30
-
31
- ```ts
32
- 'project/example.test.ts' // ✅
33
- 'example.test.ts' // ✅
34
- 'project\\example.test.ts' // ❌
35
- ```
36
-
37
- ## state
38
-
39
- ```ts
40
- function state(): TestModuleState
41
- ```
42
-
43
- Works the same way as [`testSuite.state()`](/api/advanced/test-suite#state), but can also return `queued` if module wasn't executed yet.
44
-
45
- ## meta <Version>3.1.0</Version> {#meta}
46
-
47
- ```ts
48
- function meta(): TaskMeta
49
- ```
50
-
51
- Custom [metadata](/api/advanced/metadata) that was attached to the module during its execution or collection. The meta can be attached by assigning a property to the `task.meta` object during a test run:
52
-
53
- ```ts {5,10}
54
- import { test } from 'vitest'
55
-
56
- describe('the validation works correctly', (task) => {
57
- // assign "decorated" during collection
58
- task.file.meta.decorated = false
59
-
60
- test('some test', ({ task }) => {
61
- // assign "decorated" during test run, it will be available
62
- // only in onTestCaseReady hook
63
- task.file.meta.decorated = false
64
- })
65
- })
66
- ```
67
-
68
- :::tip
69
- If metadata was attached during collection (outside of the `test` function), then it will be available in [`onTestModuleCollected`](./reporters#ontestmodulecollected) hook in the custom reporter.
70
- :::
71
-
72
- ## diagnostic
73
-
74
- ```ts
75
- function diagnostic(): ModuleDiagnostic
76
- ```
77
-
78
- Useful information about the module like duration, memory usage, etc. If the module was not executed yet, all diagnostic values will return `0`.
79
-
80
- ```ts
81
- interface ModuleDiagnostic {
82
- /**
83
- * The time it takes to import and initiate an environment.
84
- */
85
- readonly environmentSetupDuration: number
86
- /**
87
- * The time it takes Vitest to setup test harness (runner, mocks, etc.).
88
- */
89
- readonly prepareDuration: number
90
- /**
91
- * The time it takes to import the test module.
92
- * This includes importing everything in the module and executing suite callbacks.
93
- */
94
- readonly collectDuration: number
95
- /**
96
- * The time it takes to import the setup module.
97
- */
98
- readonly setupDuration: number
99
- /**
100
- * Accumulated duration of all tests and hooks in the module.
101
- */
102
- readonly duration: number
103
- /**
104
- * The amount of memory used by the module in bytes.
105
- * This value is only available if the test was executed with `logHeapUsage` flag.
106
- */
107
- readonly heap: number | undefined
108
- /**
109
- * The time spent importing every non-externalized dependency that Vitest has processed.
110
- */
111
- readonly importDurations: Record<string, ImportDuration>
112
- }
113
-
114
- /** The time spent importing & executing a non-externalized file. */
115
- interface ImportDuration {
116
- /** The time spent importing & executing the file itself, not counting all non-externalized imports that the file does. */
117
- selfTime: number
118
-
119
- /** The time spent importing & executing the file and all its imports. */
120
- totalTime: number
121
- }
122
- ```
123
-
124
- ## viteEnvironment <Version>4.1.0</Version> {#viteenvironment}
125
-
126
- This is a Vite's [`DevEnvironment`](https://vite.dev/guide/api-environment) that transforms all files inside of the test module.
127
-
128
- ::: details History
129
- - `v4.0.15`: added as experimental
130
- :::
131
-
132
- ## toTestSpecification <Version>4.1.0</Version> {#totestspecification}
133
-
134
- ```ts
135
- function toTestSpecification(testCases?: TestCase[]): TestSpecification
136
- ```
137
-
138
- Returns a new [test specification](/api/advanced/test-specification) that can be used to filter or run this specific test module.
139
-
140
- It accepts an optional array of test cases that should be filtered.
@@ -1,321 +0,0 @@
1
- # TestProject <Version>3.0.0</Version> {#testproject}
2
-
3
- ::: warning
4
- This guide describes the advanced Node.js API. If you just want to define projects, follow the ["Test Projects"](/guide/projects) guide.
5
- :::
6
-
7
- ## name
8
-
9
- The name is a unique string assigned by the user or interpreted by Vitest. If user did not provide a name, Vitest tries to load a `package.json` in the root of the project and takes the `name` property from there. If there is no `package.json`, Vitest uses the name of the folder by default. Inline projects use numbers as the name (converted to string).
10
-
11
- ::: code-group
12
- ```ts [node.js]
13
- import { createVitest } from 'vitest/node'
14
-
15
- const vitest = await createVitest('test')
16
- vitest.projects.map(p => p.name) === [
17
- '@pkg/server',
18
- 'utils',
19
- '2',
20
- 'custom'
21
- ]
22
- ```
23
- ```ts [vitest.config.js]
24
- import { defineConfig } from 'vitest/config'
25
-
26
- export default defineConfig({
27
- test: {
28
- projects: [
29
- './packages/server', // has package.json with "@pkg/server"
30
- './utils', // doesn't have a package.json file
31
- {
32
- // doesn't customize the name
33
- test: {
34
- pool: 'threads',
35
- },
36
- },
37
- {
38
- // customized the name
39
- test: {
40
- name: 'custom',
41
- },
42
- },
43
- ],
44
- },
45
- })
46
- ```
47
- :::
48
-
49
- ::: info
50
- If the [root project](/api/advanced/vitest#getrootproject) is not part of user projects, its `name` will not be resolved.
51
- :::
52
-
53
- ## vitest
54
-
55
- `vitest` references the global [`Vitest`](/api/advanced/vitest) process.
56
-
57
- ## serializedConfig
58
-
59
- This is the config that test processes receive. Vitest [serializes config](https://github.com/vitest-dev/vitest/blob/main/packages/vitest/src/node/config/serializeConfig.ts) manually by removing all functions and properties that are not possible to serialize. Since this value is available in both tests and node, its type is exported from the main entry point.
60
-
61
- ```ts
62
- import type { SerializedConfig } from 'vitest'
63
-
64
- const config: SerializedConfig = vitest.projects[0].serializedConfig
65
- ```
66
-
67
- ::: warning
68
- The `serializedConfig` property is a getter. Every time it's accessed Vitest serializes the config again in case it was changed. This also means that it always returns a different reference:
69
-
70
- ```ts
71
- project.serializedConfig === project.serializedConfig // ❌
72
- ```
73
- :::
74
-
75
- ## globalConfig
76
-
77
- The test config that [`Vitest`](/api/advanced/vitest) was initialized with. If this is the [root project](/api/advanced/vitest#getrootproject), `globalConfig` and `config` will reference the same object. This config is useful for values that cannot be set on the project level, like `coverage` or `reporters`.
78
-
79
- ```ts
80
- import type { ResolvedConfig } from 'vitest/node'
81
-
82
- vitest.config === vitest.projects[0].globalConfig
83
- ```
84
-
85
- ## config
86
-
87
- This is the project's resolved test config.
88
-
89
- ## hash <Version>3.2.0</Version> {#hash}
90
-
91
- The unique hash of this project. This value is consistent between the reruns.
92
-
93
- It is based on the root of the project and its name. Note that the root path is not consistent between different OS, so the hash will also be different.
94
-
95
- ## vite
96
-
97
- This is project's [`ViteDevServer`](https://vite.dev/guide/api-javascript#vitedevserver). All projects have their own Vite servers.
98
-
99
- ## browser
100
-
101
- This value will be set only if tests are running in the browser. If `browser` is enabled, but tests didn't run yet, this will be `undefined`. If you need to check if the project supports browser tests, use `project.isBrowserEnabled()` method.
102
-
103
- ::: warning
104
- The browser API is even more experimental and doesn't follow SemVer. The browser API will be standardized separately from the rest of the APIs.
105
- :::
106
-
107
- ## provide
108
-
109
- ```ts
110
- function provide<T extends keyof ProvidedContext & string>(
111
- key: T,
112
- value: ProvidedContext[T],
113
- ): void
114
- ```
115
-
116
- A way to provide custom values to tests in addition to [`config.provide`](/config/provide) field. All values are validated with [`structuredClone`](https://developer.mozilla.org/en-US/docs/Web/API/Window/structuredClone) before they are stored, but the values on `providedContext` themselves are not cloned.
117
-
118
- ::: code-group
119
- ```ts [node.js]
120
- import { createVitest } from 'vitest/node'
121
-
122
- const vitest = await createVitest('test')
123
- const project = vitest.projects.find(p => p.name === 'custom')
124
- project.provide('key', 'value')
125
- await vitest.start()
126
- ```
127
- ```ts [test.spec.js]
128
- import { inject } from 'vitest'
129
- const value = inject('key')
130
- ```
131
- :::
132
-
133
- The values can be provided dynamically. Provided value in tests will be updated on their next run.
134
-
135
- ::: tip
136
- This method is also available to [global setup files](/config/globalsetup) for cases where you cannot use the public API:
137
-
138
- ```js
139
- export default function setup({ provide }) {
140
- provide('wsPort', 3000)
141
- }
142
- ```
143
- :::
144
-
145
- ## getProvidedContext
146
-
147
- ```ts
148
- function getProvidedContext(): ProvidedContext
149
- ```
150
-
151
- This returns the context object. Every project also inherits the global context set by `vitest.provide`.
152
-
153
- ```ts
154
- import { createVitest } from 'vitest/node'
155
-
156
- const vitest = await createVitest('test')
157
- vitest.provide('global', true)
158
- const project = vitest.projects.find(p => p.name === 'custom')
159
- project.provide('key', 'value')
160
-
161
- // { global: true, key: 'value' }
162
- const context = project.getProvidedContext()
163
- ```
164
-
165
- ::: tip
166
- Project context values will always override root project's context.
167
- :::
168
-
169
- ## createSpecification
170
-
171
- ```ts
172
- function createSpecification(
173
- moduleId: string,
174
- locations?: number[],
175
- ): TestSpecification
176
- ```
177
-
178
- Create a [test specification](/api/advanced/test-specification) that can be used in [`vitest.runTestSpecifications`](/api/advanced/vitest#runtestspecifications). Specification scopes the test file to a specific `project` and test `locations` (optional). Test [locations](/api/advanced/test-case#location) are code lines where the test is defined in the source code. If locations are provided, Vitest will only run tests defined on those lines. Note that if [`testNamePattern`](/config/testnamepattern) is defined, then it will also be applied.
179
-
180
- ```ts
181
- import { createVitest } from 'vitest/node'
182
- import { resolve } from 'node:path/posix'
183
-
184
- const vitest = await createVitest('test')
185
- const project = vitest.projects[0]
186
- const specification = project.createSpecification(
187
- resolve('./example.test.ts'),
188
- [20, 40], // optional test lines
189
- )
190
- await vitest.runTestSpecifications([specification])
191
- ```
192
-
193
- ::: warning
194
- `createSpecification` expects resolved [module ID](/api/advanced/test-specification#moduleid). It doesn't auto-resolve the file or check that it exists on the file system.
195
-
196
- Also note that `project.createSpecification` always returns a new instance.
197
- :::
198
-
199
- ## isRootProject
200
-
201
- ```ts
202
- function isRootProject(): boolean
203
- ```
204
-
205
- Checks if the current project is the root project. You can also get the root project by calling [`vitest.getRootProject()`](/api/advanced/vitest#getrootproject).
206
-
207
- ## globTestFiles
208
-
209
- ```ts
210
- function globTestFiles(filters?: string[]): {
211
- /**
212
- * Test files that match the filters.
213
- */
214
- testFiles: string[]
215
- /**
216
- * Typecheck test files that match the filters. This will be empty unless `typecheck.enabled` is `true`.
217
- */
218
- typecheckTestFiles: string[]
219
- }
220
- ```
221
-
222
- Globs all test files. This function returns an object with regular tests and typecheck tests.
223
-
224
- This method accepts `filters`. Filters can only a part of the file path, unlike in other methods on the [`Vitest`](/api/advanced/vitest) instance:
225
-
226
- ```js
227
- project.globTestFiles(['foo']) // ✅
228
- project.globTestFiles(['basic/foo.js:10']) // ❌
229
- ```
230
-
231
- ::: tip
232
- Vitest uses [fast-glob](https://npmx.dev/package/fast-glob) to find test files. `test.dir`, `test.root`, `root` or `process.cwd()` define the `cwd` option.
233
-
234
- This method looks at several config options:
235
-
236
- - `test.include`, `test.exclude` to find regular test files
237
- - `test.includeSource`, `test.exclude` to find in-source tests
238
- - `test.typecheck.include`, `test.typecheck.exclude` to find typecheck tests
239
- :::
240
-
241
- ## matchesTestGlob
242
-
243
- ```ts
244
- function matchesTestGlob(
245
- moduleId: string,
246
- source?: () => string
247
- ): boolean
248
- ```
249
-
250
- This method checks if the file is a regular test file. It uses the same config properties that `globTestFiles` uses for validation.
251
-
252
- This method also accepts a second parameter, which is the source code. This is used to validate if the file is an in-source test. If you are calling this method several times for several projects it is recommended to read the file once and pass it down directly. If the file is not a test file, but matches the `includeSource` glob, Vitest will synchronously read the file unless the `source` is provided.
253
-
254
- ```ts
255
- import { createVitest } from 'vitest/node'
256
- import { resolve } from 'node:path/posix'
257
-
258
- const vitest = await createVitest('test')
259
- const project = vitest.projects[0]
260
-
261
- project.matchesTestGlob(resolve('./basic.test.ts')) // true
262
- project.matchesTestGlob(resolve('./basic.ts')) // false
263
- project.matchesTestGlob(resolve('./basic.ts'), () => `
264
- if (import.meta.vitest) {
265
- // ...
266
- }
267
- `) // true if `includeSource` is set
268
- ```
269
-
270
- ## import
271
-
272
- <!--@include: ./import-example.md-->
273
-
274
- Import a file using Vite module runner. The file will be transformed by Vite with provided project's config and executed in a separate context. Note that `moduleId` will be relative to the `config.root`.
275
-
276
- ::: danger
277
- `project.import` reuses Vite's module graph, so importing the same module using a regular import will return a different module:
278
-
279
- ```ts
280
- import * as staticExample from './example.js'
281
- const dynamicExample = await project.import('./example.js')
282
-
283
- dynamicExample !== staticExample // ✅
284
- ```
285
- :::
286
-
287
- ::: info
288
- Internally, Vitest uses this method to import global setups, custom coverage providers and custom reporters, meaning all of them share the same module graph as long as they belong to the same Vite server.
289
- :::
290
-
291
- ## onTestsRerun
292
-
293
- ```ts
294
- function onTestsRerun(cb: OnTestsRerunHandler): void
295
- ```
296
-
297
- This is a shorthand for [`project.vitest.onTestsRerun`](/api/advanced/vitest#ontestsrerun). It accepts a callback that will be awaited when the tests have been scheduled to rerun (usually, due to a file change).
298
-
299
- ```ts
300
- project.onTestsRerun((specs) => {
301
- console.log(specs)
302
- })
303
- ```
304
-
305
- ## isBrowserEnabled
306
-
307
- ```ts
308
- function isBrowserEnabled(): boolean
309
- ```
310
-
311
- Returns `true` if this project runs tests in the browser.
312
-
313
- ## close
314
-
315
- ```ts
316
- function close(): Promise<void>
317
- ```
318
-
319
- Closes the project and all associated resources. This can only be called once; the closing promise is cached until the server restarts. If the resources are needed again, create a new project.
320
-
321
- In detail, this method closes the Vite server, stops the typechecker service, closes the browser if it's running, deletes the temporary directory that holds the source code, and resets the provided context.
@@ -1,96 +0,0 @@
1
- # TestSpecification
2
-
3
- The `TestSpecification` class describes what module to run as a test and its parameters.
4
-
5
- You can only create a specification by calling [`createSpecification`](/api/advanced/test-project#createspecification) method on a test project:
6
-
7
- ```ts
8
- const specification = project.createSpecification(
9
- resolve('./example.test.ts'),
10
- {
11
- testLines: [20, 40],
12
- testNamePattern: /hello world/,
13
- testIds: ['1223128da3_0_0_0', '1223128da3_0_0'],
14
- testTagsFilter: ['frontend and backend'],
15
- } // optional test filters
16
- )
17
- ```
18
-
19
- `createSpecification` expects resolved module identifier. It doesn't auto-resolve the file or check that it exists on the file system.
20
-
21
- ## taskId
22
-
23
- [Test module's](/api/advanced/test-suite#id) identifier.
24
-
25
- ## project
26
-
27
- This references the [`TestProject`](/api/advanced/test-project) that the test module belongs to.
28
-
29
- ## moduleId
30
-
31
- The ID of the module in Vite's module graph. Usually, it's an absolute file path using posix separator:
32
-
33
- ```ts
34
- 'C:/Users/Documents/project/example.test.ts' // ✅
35
- '/Users/mac/project/example.test.ts' // ✅
36
- 'C:\\Users\\Documents\\project\\example.test.ts' // ❌
37
- ```
38
-
39
- ## testModule
40
-
41
- Instance of [`TestModule`](/api/advanced/test-module) associated with the specification. If test wasn't queued yet, this will be `undefined`.
42
-
43
- ## pool {#pool}
44
-
45
- The [`pool`](/config/pool) in which the test module will run.
46
-
47
- ::: danger
48
- It's possible to have multiple pools in a single test project with [`typecheck.enabled`](/config/typecheck#typecheck-enabled). This means it's possible to have several specifications with the same `moduleId` but different `pool`. In later versions, the project will only support a single pool.
49
- :::
50
-
51
- ## testLines
52
-
53
- This is an array of lines in the source code where the test files are defined. This field is defined only if the `createSpecification` method received an array.
54
-
55
- Note that if there is no test on at least one of the lines, the whole suite will fail. An example of a correct `testLines` configuration:
56
-
57
- ::: code-group
58
- ```ts [script.js]
59
- const specification = project.createSpecification(
60
- resolve('./example.test.ts'),
61
- [3, 8, 9],
62
- )
63
- ```
64
- ```ts:line-numbers{3,8,9} [example.test.js]
65
- import { test, describe } from 'vitest'
66
-
67
- test('verification works')
68
-
69
- describe('a group of tests', () => { // [!code error]
70
- // ...
71
-
72
- test('nested test')
73
- test.skip('skipped test')
74
- })
75
- ```
76
- :::
77
-
78
- ## testNamePattern <Version>4.1.0</Version> {#testnamepattern}
79
-
80
- A regexp that matches the name of the test in this module. This value will override the global [`testNamePattern`](/config/testnamepattern) option if it's set.
81
-
82
- ## testIds <Version>4.1.0</Version> {#testids}
83
-
84
- The ids of tasks inside of this specification to run.
85
-
86
- ## testTagsFilter <Version>4.1.0</Version> {#testtagsfilter}
87
-
88
- The [tags filter](/guide/test-tags#syntax) that a test must pass in order to be included in the run. Multiple filters are treated as `AND`.
89
-
90
- ## toJSON
91
-
92
- ```ts
93
- function toJSON(): SerializedTestSpecification
94
- ```
95
-
96
- `toJSON` generates a JSON-friendly object that can be consumed by the [Browser Mode](/guide/browser/) or [Vitest UI](/guide/ui).