@vitest-agent/mcp 1.0.0 → 1.1.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.
- package/bin/vitest-agent-mcp.js +1 -17
- package/index.d.ts +322 -313
- package/index.js +2 -4
- package/middleware/idempotency.js +1 -1
- package/package.json +2 -2
- package/patterns/_meta.json +67 -0
- package/patterns/authoring-a-custom-vitest-agent-reporter.md +82 -0
- package/patterns/known-issues-and-caveats.md +52 -0
- package/patterns/operating-vitest-agent-as-an-agent.md +62 -0
- package/patterns/running-tests-via-mcp.md +103 -0
- package/patterns/silencing-leaking-output-in-tests.md +91 -0
- package/patterns/testing-effect-schema-definitions.md +71 -0
- package/patterns/testing-effect-services-with-mock-layers.md +63 -0
- package/resources/index.js +16 -5
- package/resources/manifest-schema.js +1 -1
- package/tools/acceptance-metrics.js +1 -1
- package/tools/cache-health.js +1 -1
- package/tools/commit-changes.js +1 -1
- package/tools/configure.js +1 -1
- package/tools/coverage.js +1 -1
- package/tools/errors.js +1 -1
- package/tools/failure-signature-get.js +1 -1
- package/tools/file-coverage.js +1 -1
- package/tools/history.js +1 -1
- package/tools/inventory.js +1 -1
- package/tools/overview.js +1 -1
- package/tools/run-tests.js +15 -3
- package/tools/settings-list.js +1 -1
- package/tools/status.js +1 -1
- package/tools/tdd-artifact.js +1 -1
- package/tools/tdd-task.js +1 -1
- package/tools/test.js +1 -1
- package/tools/trends.js +1 -1
- package/tools/turn-search.js +1 -1
- package/vendor/vitest-docs/ATTRIBUTION.md +5 -0
- package/vendor/vitest-docs/api/advanced/artifacts.md +189 -0
- package/vendor/vitest-docs/api/advanced/metadata.md +68 -0
- package/vendor/vitest-docs/api/advanced/plugin.md +168 -0
- package/vendor/vitest-docs/api/advanced/reporters.md +342 -0
- package/vendor/vitest-docs/api/advanced/runner.md +334 -0
- package/vendor/vitest-docs/api/advanced/test-case.md +302 -0
- package/vendor/vitest-docs/api/advanced/test-collection.md +89 -0
- package/vendor/vitest-docs/api/advanced/test-module.md +140 -0
- package/vendor/vitest-docs/api/advanced/test-project.md +321 -0
- package/vendor/vitest-docs/api/advanced/test-specification.md +96 -0
- package/vendor/vitest-docs/api/advanced/test-suite.md +230 -0
- package/vendor/vitest-docs/api/advanced/vitest.md +684 -0
- package/vendor/vitest-docs/api/assert-type.md +22 -0
- package/vendor/vitest-docs/api/assert.md +1960 -0
- package/vendor/vitest-docs/api/browser/assertions.md +1277 -0
- package/vendor/vitest-docs/api/browser/commands.md +154 -0
- package/vendor/vitest-docs/api/browser/context.md +338 -0
- package/vendor/vitest-docs/api/browser/interactivity.md +681 -0
- package/vendor/vitest-docs/api/browser/locators.md +1171 -0
- package/vendor/vitest-docs/api/browser/react.md +346 -0
- package/vendor/vitest-docs/api/browser/svelte.md +292 -0
- package/vendor/vitest-docs/api/browser/vue.md +222 -0
- package/vendor/vitest-docs/api/describe.md +374 -0
- package/vendor/vitest-docs/api/expect-typeof.md +571 -0
- package/vendor/vitest-docs/api/expect.md +2304 -0
- package/vendor/vitest-docs/api/hooks.md +463 -0
- package/vendor/vitest-docs/api/mock.md +701 -0
- package/vendor/vitest-docs/api/test.md +926 -0
- package/vendor/vitest-docs/api/vi.md +1372 -0
- package/vendor/vitest-docs/config/alias.md +13 -0
- package/vendor/vitest-docs/config/allowonly.md +32 -0
- package/vendor/vitest-docs/config/api.md +27 -0
- package/vendor/vitest-docs/config/attachmentsdir.md +6 -0
- package/vendor/vitest-docs/config/bail.md +9 -0
- package/vendor/vitest-docs/config/benchmark.md +65 -0
- package/vendor/vitest-docs/config/browser/api.md +23 -0
- package/vendor/vitest-docs/config/browser/commands.md +6 -0
- package/vendor/vitest-docs/config/browser/connecttimeout.md +10 -0
- package/vendor/vitest-docs/config/browser/detailspanelposition.md +38 -0
- package/vendor/vitest-docs/config/browser/enabled.md +40 -0
- package/vendor/vitest-docs/config/browser/expect.md +250 -0
- package/vendor/vitest-docs/config/browser/headless.md +7 -0
- package/vendor/vitest-docs/config/browser/instances.md +47 -0
- package/vendor/vitest-docs/config/browser/isolate.md +11 -0
- package/vendor/vitest-docs/config/browser/locators.md +24 -0
- package/vendor/vitest-docs/config/browser/orchestratorscripts.md +39 -0
- package/vendor/vitest-docs/config/browser/playwright.md +214 -0
- package/vendor/vitest-docs/config/browser/preview.md +32 -0
- package/vendor/vitest-docs/config/browser/provider.md +79 -0
- package/vendor/vitest-docs/config/browser/screenshotdirectory.md +6 -0
- package/vendor/vitest-docs/config/browser/screenshotfailures.md +6 -0
- package/vendor/vitest-docs/config/browser/testerhtmlpath.md +5 -0
- package/vendor/vitest-docs/config/browser/trace.md +43 -0
- package/vendor/vitest-docs/config/browser/trackunhandlederrors.md +10 -0
- package/vendor/vitest-docs/config/browser/ui.md +7 -0
- package/vendor/vitest-docs/config/browser/viewport.md +6 -0
- package/vendor/vitest-docs/config/browser/webdriverio.md +64 -0
- package/vendor/vitest-docs/config/cache.md +26 -0
- package/vendor/vitest-docs/config/chaiconfig.md +29 -0
- package/vendor/vitest-docs/config/clearmocks.md +22 -0
- package/vendor/vitest-docs/config/coverage.md +455 -0
- package/vendor/vitest-docs/config/css.md +47 -0
- package/vendor/vitest-docs/config/dangerouslyignoreunhandlederrors.md +23 -0
- package/vendor/vitest-docs/config/deps.md +127 -0
- package/vendor/vitest-docs/config/detectasyncleaks.md +39 -0
- package/vendor/vitest-docs/config/diff.md +96 -0
- package/vendor/vitest-docs/config/dir.md +7 -0
- package/vendor/vitest-docs/config/disableconsoleintercept.md +15 -0
- package/vendor/vitest-docs/config/env.md +5 -0
- package/vendor/vitest-docs/config/environment.md +96 -0
- package/vendor/vitest-docs/config/environmentoptions.md +30 -0
- package/vendor/vitest-docs/config/exclude.md +49 -0
- package/vendor/vitest-docs/config/execargv.md +10 -0
- package/vendor/vitest-docs/config/expandsnapshotdiff.md +7 -0
- package/vendor/vitest-docs/config/expect.md +38 -0
- package/vendor/vitest-docs/config/experimental.md +510 -0
- package/vendor/vitest-docs/config/faketimers.md +51 -0
- package/vendor/vitest-docs/config/fileparallelism.md +11 -0
- package/vendor/vitest-docs/config/forcereruntriggers.md +19 -0
- package/vendor/vitest-docs/config/globals.md +42 -0
- package/vendor/vitest-docs/config/globalsetup.md +72 -0
- package/vendor/vitest-docs/config/hideskippedtests.md +7 -0
- package/vendor/vitest-docs/config/hooktimeout.md +7 -0
- package/vendor/vitest-docs/config/include-source.md +115 -0
- package/vendor/vitest-docs/config/include.md +71 -0
- package/vendor/vitest-docs/config/includetasklocation.md +17 -0
- package/vendor/vitest-docs/config/index.md +85 -0
- package/vendor/vitest-docs/config/isolate.md +13 -0
- package/vendor/vitest-docs/config/logheapusage.md +7 -0
- package/vendor/vitest-docs/config/maxconcurrency.md +9 -0
- package/vendor/vitest-docs/config/maxworkers.md +49 -0
- package/vendor/vitest-docs/config/mockreset.md +22 -0
- package/vendor/vitest-docs/config/mode.md +7 -0
- package/vendor/vitest-docs/config/name.md +111 -0
- package/vendor/vitest-docs/config/onconsolelog.md +25 -0
- package/vendor/vitest-docs/config/onstacktrace.md +32 -0
- package/vendor/vitest-docs/config/onunhandlederror.md +35 -0
- package/vendor/vitest-docs/config/open.md +7 -0
- package/vendor/vitest-docs/config/outputfile.md +7 -0
- package/vendor/vitest-docs/config/passwithnotests.md +7 -0
- package/vendor/vitest-docs/config/pool.md +45 -0
- package/vendor/vitest-docs/config/printconsoletrace.md +6 -0
- package/vendor/vitest-docs/config/projects.md +6 -0
- package/vendor/vitest-docs/config/provide.md +45 -0
- package/vendor/vitest-docs/config/reporters.md +69 -0
- package/vendor/vitest-docs/config/resolvesnapshotpath.md +36 -0
- package/vendor/vitest-docs/config/restoremocks.md +22 -0
- package/vendor/vitest-docs/config/retry.md +140 -0
- package/vendor/vitest-docs/config/root.md +6 -0
- package/vendor/vitest-docs/config/runner.md +6 -0
- package/vendor/vitest-docs/config/sequence.md +158 -0
- package/vendor/vitest-docs/config/server.md +68 -0
- package/vendor/vitest-docs/config/setupfiles.md +40 -0
- package/vendor/vitest-docs/config/silent.md +9 -0
- package/vendor/vitest-docs/config/slowtestthreshold.md +7 -0
- package/vendor/vitest-docs/config/snapshotenvironment.md +27 -0
- package/vendor/vitest-docs/config/snapshotformat.md +28 -0
- package/vendor/vitest-docs/config/snapshotserializers.md +6 -0
- package/vendor/vitest-docs/config/stricttags.md +30 -0
- package/vendor/vitest-docs/config/tags.md +141 -0
- package/vendor/vitest-docs/config/teardowntimeout.md +7 -0
- package/vendor/vitest-docs/config/testnamepattern.md +21 -0
- package/vendor/vitest-docs/config/testtimeout.md +7 -0
- package/vendor/vitest-docs/config/typecheck.md +77 -0
- package/vendor/vitest-docs/config/ui.md +15 -0
- package/vendor/vitest-docs/config/unstubenvs.md +20 -0
- package/vendor/vitest-docs/config/unstubglobals.md +20 -0
- package/vendor/vitest-docs/config/update.md +16 -0
- package/vendor/vitest-docs/config/vmmemorylimit.md +30 -0
- package/vendor/vitest-docs/config/watch.md +11 -0
- package/vendor/vitest-docs/config/watchtriggerpatterns.md +29 -0
- package/vendor/vitest-docs/guide/advanced/index.md +147 -0
- package/vendor/vitest-docs/guide/advanced/pool.md +148 -0
- package/vendor/vitest-docs/guide/advanced/reporters.md +93 -0
- package/vendor/vitest-docs/guide/advanced/tests.md +125 -0
- package/vendor/vitest-docs/guide/browser/aria-snapshots.md +470 -0
- package/vendor/vitest-docs/guide/browser/component-testing.md +571 -0
- package/vendor/vitest-docs/guide/browser/index.md +630 -0
- package/vendor/vitest-docs/guide/browser/multiple-setups.md +121 -0
- package/vendor/vitest-docs/guide/browser/trace-view.md +126 -0
- package/vendor/vitest-docs/guide/browser/visual-regression-testing.md +734 -0
- package/vendor/vitest-docs/guide/cli-generated.md +972 -0
- package/vendor/vitest-docs/guide/cli.md +234 -0
- package/vendor/vitest-docs/guide/common-errors.md +163 -0
- package/vendor/vitest-docs/guide/coverage.md +515 -0
- package/vendor/vitest-docs/guide/debugging.md +127 -0
- package/vendor/vitest-docs/guide/environment.md +101 -0
- package/vendor/vitest-docs/guide/extending-matchers.md +160 -0
- package/vendor/vitest-docs/guide/features.md +310 -0
- package/vendor/vitest-docs/guide/filtering.md +175 -0
- package/vendor/vitest-docs/guide/ide.md +43 -0
- package/vendor/vitest-docs/guide/improving-performance.md +245 -0
- package/vendor/vitest-docs/guide/in-source.md +159 -0
- package/vendor/vitest-docs/guide/index.md +128 -0
- package/vendor/vitest-docs/guide/learn/async.md +147 -0
- package/vendor/vitest-docs/guide/learn/debugging-tests.md +210 -0
- package/vendor/vitest-docs/guide/learn/matchers.md +277 -0
- package/vendor/vitest-docs/guide/learn/mock-functions.md +277 -0
- package/vendor/vitest-docs/guide/learn/setup-teardown.md +240 -0
- package/vendor/vitest-docs/guide/learn/snapshots.md +166 -0
- package/vendor/vitest-docs/guide/learn/testing-in-practice.md +430 -0
- package/vendor/vitest-docs/guide/learn/writing-tests-with-ai.md +127 -0
- package/vendor/vitest-docs/guide/learn/writing-tests.md +231 -0
- package/vendor/vitest-docs/guide/lifecycle.md +379 -0
- package/vendor/vitest-docs/guide/migration.md +863 -0
- package/vendor/vitest-docs/guide/mocking/classes.md +158 -0
- package/vendor/vitest-docs/guide/mocking/dates.md +52 -0
- package/vendor/vitest-docs/guide/mocking/file-system.md +74 -0
- package/vendor/vitest-docs/guide/mocking/functions.md +61 -0
- package/vendor/vitest-docs/guide/mocking/globals.md +20 -0
- package/vendor/vitest-docs/guide/mocking/modules.md +414 -0
- package/vendor/vitest-docs/guide/mocking/requests.md +114 -0
- package/vendor/vitest-docs/guide/mocking/timers.md +48 -0
- package/vendor/vitest-docs/guide/mocking.md +239 -0
- package/vendor/vitest-docs/guide/open-telemetry.md +156 -0
- package/vendor/vitest-docs/guide/parallelism.md +82 -0
- package/vendor/vitest-docs/guide/profiling-test-performance.md +243 -0
- package/vendor/vitest-docs/guide/projects.md +291 -0
- package/vendor/vitest-docs/guide/recipes.md +59 -0
- package/vendor/vitest-docs/guide/reporters.md +723 -0
- package/vendor/vitest-docs/guide/snapshot.md +620 -0
- package/vendor/vitest-docs/guide/test-annotations.md +103 -0
- package/vendor/vitest-docs/guide/test-context.md +902 -0
- package/vendor/vitest-docs/guide/test-tags.md +314 -0
- package/vendor/vitest-docs/guide/testing-types.md +149 -0
- package/vendor/vitest-docs/guide/ui.md +160 -0
- package/vendor/vitest-docs/guide/using-plugins.md +5 -0
- package/vendor/vitest-docs/manifest.json +1691 -0
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
# vitest-browser-vue
|
|
2
|
+
|
|
3
|
+
The community [`vitest-browser-vue`](https://npmx.dev/package/vitest-browser-vue) package renders [Vue](https://vuejs.org/) components in [Browser Mode](/guide/browser/).
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { render } from 'vitest-browser-vue'
|
|
7
|
+
import { expect, test } from 'vitest'
|
|
8
|
+
import Component from './Component.vue'
|
|
9
|
+
|
|
10
|
+
test('counter button increments the count', async () => {
|
|
11
|
+
const screen = await render(Component, {
|
|
12
|
+
props: {
|
|
13
|
+
initialCount: 1,
|
|
14
|
+
}
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
await screen.getByRole('button', { name: 'Increment' }).click()
|
|
18
|
+
|
|
19
|
+
await expect.element(screen.getByText('Count is 2')).toBeVisible()
|
|
20
|
+
})
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
::: warning
|
|
24
|
+
This library takes inspiration from [`@testing-library/vue`](https://github.com/testing-library/vue-testing-library).
|
|
25
|
+
|
|
26
|
+
If you have used `@testing-library/vue` in your tests before, you can keep using it, however the `vitest-browser-vue` package provides certain benefits unique to the Browser Mode that `@testing-library/vue` lacks:
|
|
27
|
+
|
|
28
|
+
`vitest-browser-vue` returns APIs that interact well with built-in [locators](/api/browser/locators), [user events](/api/browser/interactivity) and [assertions](/api/browser/assertions): for example, Vitest will automatically retry the element until the assertion is successful, even if it was rerendered between the assertions.
|
|
29
|
+
:::
|
|
30
|
+
|
|
31
|
+
The package exposes two entry points: `vitest-browser-vue` and `vitest-browser-vue/pure`. They expose identical API, but the `pure` entry point doesn't add a handler to remove the component before the next test has started.
|
|
32
|
+
|
|
33
|
+
## render
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
export function render(
|
|
37
|
+
component: Component,
|
|
38
|
+
options?: ComponentRenderOptions,
|
|
39
|
+
): RenderResult & PromiseLike<RenderResult>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The `render` function records a `vue.render` trace mark, visible in the [Trace View](/guide/browser/trace-view).
|
|
43
|
+
|
|
44
|
+
::: warning
|
|
45
|
+
Synchronous usage of `render` is deprecated and will be removed in the next major version. Please always `await` the result:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
const screen = render(Component) // [!code --]
|
|
49
|
+
const screen = await render(Component) // [!code ++]
|
|
50
|
+
```
|
|
51
|
+
:::
|
|
52
|
+
|
|
53
|
+
### Options
|
|
54
|
+
|
|
55
|
+
The `render` function supports all [`mount` options](https://test-utils.vuejs.org/api/#mount) from `@vue/test-utils` (except `attachTo` - use `container` instead). In addition to them, there are also `container` and `baseElement`.
|
|
56
|
+
|
|
57
|
+
#### container
|
|
58
|
+
|
|
59
|
+
By default, Vitest will create a `div`, append it to `document.body`, and render your component there. If you provide your own `HTMLElement` container, it will not be appended automatically — you'll need to call `document.body.appendChild(container)` before `render`.
|
|
60
|
+
|
|
61
|
+
For example, if you are unit testing a `tbody` element, it cannot be a child of a `div`. In this case, you can specify a `table` as the render container.
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
const table = document.createElement('table')
|
|
65
|
+
|
|
66
|
+
const { container } = await render(TableBody, {
|
|
67
|
+
props,
|
|
68
|
+
// ⚠️ appending the element to `body` manually before rendering
|
|
69
|
+
container: document.body.appendChild(table),
|
|
70
|
+
})
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
#### baseElement
|
|
74
|
+
|
|
75
|
+
If the `container` is specified, then this defaults to that, otherwise this defaults to `document.body`. This is used as the base element for the queries as well as what is printed when you use `debug()`.
|
|
76
|
+
|
|
77
|
+
### Render Result
|
|
78
|
+
|
|
79
|
+
In addition to documented return value, the `render` function also returns all available [locators](/api/browser/locators) relative to the [`baseElement`](#baseelement), including [custom ones](/api/browser/locators#custom-locators).
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
const screen = await render(TableBody, { props })
|
|
83
|
+
|
|
84
|
+
await screen.getByRole('link', { name: 'Expand' }).click()
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
#### container
|
|
88
|
+
|
|
89
|
+
The containing DOM node where your Vue component is rendered. This is a regular DOM node, so you technically could call `container.querySelector` etc. to inspect the children.
|
|
90
|
+
|
|
91
|
+
:::danger
|
|
92
|
+
If you find yourself using `container` to query for rendered elements then you should reconsider! The [locators](/api/browser/locators) are designed to be more resilient to changes that will be made to the component you're testing. Avoid using `container` to query for elements!
|
|
93
|
+
:::
|
|
94
|
+
|
|
95
|
+
#### baseElement
|
|
96
|
+
|
|
97
|
+
The containing DOM node where your Vue component is rendered in the `container`. If you don't specify the `baseElement` in the options of render, it will default to `document.body`.
|
|
98
|
+
|
|
99
|
+
This is useful when the component you want to test renders something outside the container `div`, e.g. when you want to snapshot test your portal component which renders its HTML directly in the body.
|
|
100
|
+
|
|
101
|
+
:::tip
|
|
102
|
+
The queries returned by the `render` looks into `baseElement`, so you can use queries to test your portal component without the `baseElement`.
|
|
103
|
+
:::
|
|
104
|
+
|
|
105
|
+
#### locator
|
|
106
|
+
|
|
107
|
+
The [locator](/api/browser/locators) of your `container`. It is useful to use queries scoped only to your component, or pass it down to other assertions:
|
|
108
|
+
|
|
109
|
+
```js
|
|
110
|
+
import { render } from 'vitest-browser-vue'
|
|
111
|
+
|
|
112
|
+
const { locator } = await render(NumberDisplay, {
|
|
113
|
+
props: { number: 2 }
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
await locator.getByRole('button').click()
|
|
117
|
+
await expect.element(locator).toHaveTextContent('Hello World')
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
#### debug
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
function debug(
|
|
124
|
+
el?: HTMLElement | HTMLElement[] | Locator | Locator[],
|
|
125
|
+
maxLength?: number,
|
|
126
|
+
options?: PrettyDOMOptions,
|
|
127
|
+
): void
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
This method is a shortcut for `console.log(prettyDOM(baseElement))`. It will print the DOM content of the container or specified elements to the console.
|
|
131
|
+
|
|
132
|
+
#### rerender
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
function rerender(props: Partial<Props>): void & PromiseLike<void>
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Also records a `vue.rerender` trace mark in the [Trace View](/guide/browser/trace-view).
|
|
139
|
+
|
|
140
|
+
It is better if you test the component that's doing the prop updating to ensure that the props are being updated correctly to avoid relying on implementation details in your tests. That said, if you'd prefer to update the props of a rendered component in your test, this function can be used to update props of the rendered component.
|
|
141
|
+
|
|
142
|
+
::: warning
|
|
143
|
+
Synchronous usage of `rerender` is deprecated and will be removed in the next major version. Please always `await` the result.
|
|
144
|
+
:::
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
import { render } from 'vitest-browser-vue'
|
|
148
|
+
|
|
149
|
+
const { rerender } = await render(NumberDisplay, { props: { number: 1 } })
|
|
150
|
+
|
|
151
|
+
// re-render the same component with different props
|
|
152
|
+
await rerender({ number: 2 })
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
#### unmount
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
function unmount(): void & PromiseLike<void>
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
This will cause the rendered component to be unmounted. Also records a `vue.unmount` trace mark in the [Trace View](/guide/browser/trace-view). This is useful for testing what happens when your component is removed from the page (like testing that you don't leave event handlers hanging around causing memory leaks).
|
|
162
|
+
|
|
163
|
+
::: warning
|
|
164
|
+
Synchronous usage of `unmount` is deprecated and will be removed in the next major version. Please always `await` the result.
|
|
165
|
+
:::
|
|
166
|
+
|
|
167
|
+
#### emitted
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
function emitted<T = unknown>(): Record<string, T[]>
|
|
171
|
+
function emitted<T = unknown[]>(eventName: string): undefined | T[]
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Returns the emitted events from the Component.
|
|
175
|
+
|
|
176
|
+
::: warning
|
|
177
|
+
Emitted values are an implementation detail not exposed directly to the user, so it is better to test how your emitted values are changing the displayed content by using [locators](/api/browser/locators) instead.
|
|
178
|
+
:::
|
|
179
|
+
|
|
180
|
+
## cleanup
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
export function cleanup(): void
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
Remove all components rendered with [`render`](#render).
|
|
187
|
+
|
|
188
|
+
## Extend Queries
|
|
189
|
+
|
|
190
|
+
To extend locator queries, see [`"Custom Locators"`](/api/browser/locators#custom-locators). For example, to make `render` return a new custom locator, define it using the `locators.extend` API:
|
|
191
|
+
|
|
192
|
+
```js {5-7,12}
|
|
193
|
+
import { locators } from 'vitest/browser'
|
|
194
|
+
import { render } from 'vitest-browser-vue'
|
|
195
|
+
|
|
196
|
+
locators.extend({
|
|
197
|
+
getByArticleTitle(title) {
|
|
198
|
+
return `[data-title="${title}"]`
|
|
199
|
+
},
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
const screen = await render(Component)
|
|
203
|
+
await expect.element(
|
|
204
|
+
screen.getByArticleTitle('Hello World')
|
|
205
|
+
).toBeVisible()
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## Configuration
|
|
209
|
+
|
|
210
|
+
You can configure [Vue Test Utils](https://test-utils.vuejs.org/api/#config) options by assigning properties to the `config` export (available in both `vitest-browser-vue` and `vitest-browser-vue/pure`):
|
|
211
|
+
|
|
212
|
+
```js
|
|
213
|
+
import { config } from 'vitest-browser-vue/pure'
|
|
214
|
+
|
|
215
|
+
config.global.stubs.CustomComponent = {
|
|
216
|
+
template: '<div></div>',
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## See also
|
|
221
|
+
|
|
222
|
+
- [Vue Testing Library documentation](https://testing-library.com/docs/vue-testing-library/intro)
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
# describe
|
|
2
|
+
|
|
3
|
+
- **Alias:** `suite`
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
function describe(
|
|
7
|
+
name: string | Function,
|
|
8
|
+
body?: () => unknown,
|
|
9
|
+
timeout?: number
|
|
10
|
+
): void
|
|
11
|
+
function describe(
|
|
12
|
+
name: string | Function,
|
|
13
|
+
options: SuiteOptions,
|
|
14
|
+
body?: () => unknown,
|
|
15
|
+
): void
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
`describe` is used to group related tests and benchmarks into a suite. Suites help organize your test files by creating logical blocks, making test output easier to read and enabling shared setup/teardown through [lifecycle hooks](/api/hooks).
|
|
19
|
+
|
|
20
|
+
When you use `test` in the top level of file, they are collected as part of the implicit suite for it. Using `describe` you can define a new suite in the current context, as a set of related tests or benchmarks and other nested suites.
|
|
21
|
+
|
|
22
|
+
```ts [basic.spec.ts]
|
|
23
|
+
import { describe, expect, test } from 'vitest'
|
|
24
|
+
|
|
25
|
+
const person = {
|
|
26
|
+
isActive: true,
|
|
27
|
+
age: 32,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe('person', () => {
|
|
31
|
+
test('person is defined', () => {
|
|
32
|
+
expect(person).toBeDefined()
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test('is active', () => {
|
|
36
|
+
expect(person.isActive).toBeTruthy()
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('age limit', () => {
|
|
40
|
+
expect(person.age).toBeLessThanOrEqual(32)
|
|
41
|
+
})
|
|
42
|
+
})
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
You can also nest `describe` blocks if you have a hierarchy of tests:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { describe, expect, test } from 'vitest'
|
|
49
|
+
|
|
50
|
+
function numberToCurrency(value: number | string) {
|
|
51
|
+
if (typeof value !== 'number') {
|
|
52
|
+
throw new TypeError('Value must be a number')
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return value.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
describe('numberToCurrency', () => {
|
|
59
|
+
describe('given an invalid number', () => {
|
|
60
|
+
test('composed of non-numbers to throw error', () => {
|
|
61
|
+
expect(() => numberToCurrency('abc')).toThrow()
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
describe('given a valid number', () => {
|
|
66
|
+
test('returns the correct currency format', () => {
|
|
67
|
+
expect(numberToCurrency(10000)).toBe('10,000.00')
|
|
68
|
+
})
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Test Options
|
|
74
|
+
|
|
75
|
+
You can use [test options](/api/test#test-options) to apply configuration to every test inside a suite, including nested suites. This is useful when you want to set timeouts, retries, or other options for a group of related tests.
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import { describe, test } from 'vitest'
|
|
79
|
+
|
|
80
|
+
describe('slow tests', { timeout: 10_000 }, () => {
|
|
81
|
+
test('test 1', () => { /* ... */ })
|
|
82
|
+
test('test 2', () => { /* ... */ })
|
|
83
|
+
|
|
84
|
+
// nested suites also inherit the timeout
|
|
85
|
+
describe('nested', () => {
|
|
86
|
+
test('test 3', () => { /* ... */ })
|
|
87
|
+
})
|
|
88
|
+
})
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### `shuffle`
|
|
92
|
+
|
|
93
|
+
- **Type:** `boolean`
|
|
94
|
+
- **Default:** `false` (configured by [`sequence.shuffle`](/config/sequence#sequence-shuffle))
|
|
95
|
+
- **Alias:** [`describe.shuffle`](#describe-shuffle)
|
|
96
|
+
|
|
97
|
+
Run tests within the suite in random order. This option is inherited by nested suites.
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import { describe, test } from 'vitest'
|
|
101
|
+
|
|
102
|
+
describe('randomized tests', { shuffle: true }, () => {
|
|
103
|
+
test('test 1', () => { /* ... */ })
|
|
104
|
+
test('test 2', () => { /* ... */ })
|
|
105
|
+
test('test 3', () => { /* ... */ })
|
|
106
|
+
})
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## describe.skip
|
|
110
|
+
|
|
111
|
+
- **Alias:** `suite.skip`
|
|
112
|
+
|
|
113
|
+
Use `describe.skip` in a suite to avoid running a particular describe block.
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
import { assert, describe, test } from 'vitest'
|
|
117
|
+
|
|
118
|
+
describe.skip('skipped suite', () => {
|
|
119
|
+
test('sqrt', () => {
|
|
120
|
+
// Suite skipped, no error
|
|
121
|
+
assert.equal(Math.sqrt(4), 3)
|
|
122
|
+
})
|
|
123
|
+
})
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## describe.skipIf
|
|
127
|
+
|
|
128
|
+
- **Alias:** `suite.skipIf`
|
|
129
|
+
|
|
130
|
+
In some cases, you might run suites multiple times with different environments, and some of the suites might be environment-specific. Instead of wrapping the suite with `if`, you can use `describe.skipIf` to skip the suite whenever the condition is truthy.
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
import { describe, test } from 'vitest'
|
|
134
|
+
|
|
135
|
+
const isDev = process.env.NODE_ENV === 'development'
|
|
136
|
+
|
|
137
|
+
describe.skipIf(isDev)('prod only test suite', () => {
|
|
138
|
+
// this test suite only runs in production
|
|
139
|
+
})
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## describe.runIf
|
|
143
|
+
|
|
144
|
+
- **Alias:** `suite.runIf`
|
|
145
|
+
|
|
146
|
+
Opposite of [describe.skipIf](#describe-skipif).
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
import { assert, describe, test } from 'vitest'
|
|
150
|
+
|
|
151
|
+
const isDev = process.env.NODE_ENV === 'development'
|
|
152
|
+
|
|
153
|
+
describe.runIf(isDev)('dev only test suite', () => {
|
|
154
|
+
// this test suite only runs in development
|
|
155
|
+
})
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## describe.only
|
|
159
|
+
|
|
160
|
+
- **Alias:** `suite.only`
|
|
161
|
+
|
|
162
|
+
Use `describe.only` to only run certain suites
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
import { assert, describe, test } from 'vitest'
|
|
166
|
+
|
|
167
|
+
// Only this suite (and others marked with only) are run
|
|
168
|
+
describe.only('suite', () => {
|
|
169
|
+
test('sqrt', () => {
|
|
170
|
+
assert.equal(Math.sqrt(4), 3)
|
|
171
|
+
})
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
describe('other suite', () => {
|
|
175
|
+
// ... will be skipped
|
|
176
|
+
})
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Sometimes it is very useful to run `only` tests in a certain file, ignoring all other tests from the whole test suite, which pollute the output.
|
|
180
|
+
|
|
181
|
+
In order to do that, run `vitest` with specific file containing the tests in question:
|
|
182
|
+
|
|
183
|
+
```shell
|
|
184
|
+
vitest interesting.test.ts
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## describe.concurrent
|
|
188
|
+
|
|
189
|
+
- **Alias:** `suite.concurrent`
|
|
190
|
+
|
|
191
|
+
`describe.concurrent` runs all inner suites and tests in parallel
|
|
192
|
+
|
|
193
|
+
```ts
|
|
194
|
+
import { describe, test } from 'vitest'
|
|
195
|
+
|
|
196
|
+
// All suites and tests within this suite will be run in parallel
|
|
197
|
+
describe.concurrent('suite', () => {
|
|
198
|
+
test('concurrent test 1', async () => { /* ... */ })
|
|
199
|
+
describe('concurrent suite 2', async () => {
|
|
200
|
+
test('concurrent test inner 1', async () => { /* ... */ })
|
|
201
|
+
test('concurrent test inner 2', async () => { /* ... */ })
|
|
202
|
+
})
|
|
203
|
+
test.concurrent('concurrent test 3', async () => { /* ... */ })
|
|
204
|
+
})
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
`.skip`, `.only`, and `.todo` works with concurrent suites. All the following combinations are valid:
|
|
208
|
+
|
|
209
|
+
```ts
|
|
210
|
+
describe.concurrent(/* ... */)
|
|
211
|
+
describe.skip.concurrent(/* ... */) // or describe.concurrent.skip(/* ... */)
|
|
212
|
+
describe.only.concurrent(/* ... */) // or describe.concurrent.only(/* ... */)
|
|
213
|
+
describe.todo.concurrent(/* ... */) // or describe.concurrent.todo(/* ... */)
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
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.
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
describe.concurrent('suite', () => {
|
|
220
|
+
test('concurrent test 1', async ({ expect }) => {
|
|
221
|
+
expect(foo).toMatchSnapshot()
|
|
222
|
+
})
|
|
223
|
+
test('concurrent test 2', async ({ expect }) => {
|
|
224
|
+
expect(foo).toMatchSnapshot()
|
|
225
|
+
})
|
|
226
|
+
})
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## describe.sequential
|
|
230
|
+
|
|
231
|
+
- **Alias:** `suite.sequential`
|
|
232
|
+
|
|
233
|
+
`describe.sequential` in a suite marks every test as sequential. This is useful if you want to run tests in sequence within `describe.concurrent` or with the `--sequence.concurrent` command option.
|
|
234
|
+
|
|
235
|
+
```ts
|
|
236
|
+
import { describe, test } from 'vitest'
|
|
237
|
+
|
|
238
|
+
describe.concurrent('suite', () => {
|
|
239
|
+
test('concurrent test 1', async () => { /* ... */ })
|
|
240
|
+
test('concurrent test 2', async () => { /* ... */ })
|
|
241
|
+
|
|
242
|
+
describe.sequential('', () => {
|
|
243
|
+
test('sequential test 1', async () => { /* ... */ })
|
|
244
|
+
test('sequential test 2', async () => { /* ... */ })
|
|
245
|
+
})
|
|
246
|
+
})
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
## describe.shuffle
|
|
250
|
+
|
|
251
|
+
- **Alias:** `suite.shuffle`
|
|
252
|
+
|
|
253
|
+
Vitest provides a way to run all tests in random order via CLI flag [`--sequence.shuffle`](/guide/cli) or config option [`sequence.shuffle`](/config/sequence#sequence-shuffle), but if you want to have only part of your test suite to run tests in random order, you can mark it with this flag.
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
import { describe, test } from 'vitest'
|
|
257
|
+
|
|
258
|
+
// or describe('suite', { shuffle: true }, ...)
|
|
259
|
+
describe.shuffle('suite', () => {
|
|
260
|
+
test('random test 1', async () => { /* ... */ })
|
|
261
|
+
test('random test 2', async () => { /* ... */ })
|
|
262
|
+
test('random test 3', async () => { /* ... */ })
|
|
263
|
+
|
|
264
|
+
// `shuffle` is inherited
|
|
265
|
+
describe('still random', () => {
|
|
266
|
+
test('random 4.1', async () => { /* ... */ })
|
|
267
|
+
test('random 4.2', async () => { /* ... */ })
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
// disable shuffle inside
|
|
271
|
+
describe('not random', { shuffle: false }, () => {
|
|
272
|
+
test('in order 5.1', async () => { /* ... */ })
|
|
273
|
+
test('in order 5.2', async () => { /* ... */ })
|
|
274
|
+
})
|
|
275
|
+
})
|
|
276
|
+
// order depends on sequence.seed option in config (Date.now() by default)
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
`.skip`, `.only`, and `.todo` works with random suites.
|
|
280
|
+
|
|
281
|
+
## describe.todo
|
|
282
|
+
|
|
283
|
+
- **Alias:** `suite.todo`
|
|
284
|
+
|
|
285
|
+
Use `describe.todo` to stub suites to be implemented later. An entry will be shown in the report for the tests so you know how many tests you still need to implement.
|
|
286
|
+
|
|
287
|
+
```ts
|
|
288
|
+
// An entry will be shown in the report for this suite
|
|
289
|
+
describe.todo('unimplemented suite')
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
## describe.each
|
|
293
|
+
|
|
294
|
+
- **Alias:** `suite.each`
|
|
295
|
+
|
|
296
|
+
::: tip
|
|
297
|
+
While `describe.each` is provided for Jest compatibility,
|
|
298
|
+
Vitest also has [`describe.for`](#describe-for) which simplifies argument types and aligns with [`test.for`](/api/test#test-for).
|
|
299
|
+
:::
|
|
300
|
+
|
|
301
|
+
Use `describe.each` if you have more than one test that depends on the same data.
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
import { describe, expect, test } from 'vitest'
|
|
305
|
+
|
|
306
|
+
describe.each([
|
|
307
|
+
{ a: 1, b: 1, expected: 2 },
|
|
308
|
+
{ a: 1, b: 2, expected: 3 },
|
|
309
|
+
{ a: 2, b: 1, expected: 3 },
|
|
310
|
+
])('describe object add($a, $b)', ({ a, b, expected }) => {
|
|
311
|
+
test(`returns ${expected}`, () => {
|
|
312
|
+
expect(a + b).toBe(expected)
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
test(`returned value not be greater than ${expected}`, () => {
|
|
316
|
+
expect(a + b).not.toBeGreaterThan(expected)
|
|
317
|
+
})
|
|
318
|
+
|
|
319
|
+
test(`returned value not be less than ${expected}`, () => {
|
|
320
|
+
expect(a + b).not.toBeLessThan(expected)
|
|
321
|
+
})
|
|
322
|
+
})
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
* First row should be column names, separated by `|`;
|
|
326
|
+
* One or more subsequent rows of data supplied as template literal expressions using `${value}` syntax.
|
|
327
|
+
|
|
328
|
+
```ts
|
|
329
|
+
import { describe, expect, test } from 'vitest'
|
|
330
|
+
|
|
331
|
+
describe.each`
|
|
332
|
+
a | b | expected
|
|
333
|
+
${1} | ${1} | ${2}
|
|
334
|
+
${'a'} | ${'b'} | ${'ab'}
|
|
335
|
+
${[]} | ${'b'} | ${'b'}
|
|
336
|
+
${{}} | ${'b'} | ${'[object Object]b'}
|
|
337
|
+
${{ asd: 1 }} | ${'b'} | ${'[object Object]b'}
|
|
338
|
+
`('describe template string add($a, $b)', ({ a, b, expected }) => {
|
|
339
|
+
test(`returns ${expected}`, () => {
|
|
340
|
+
expect(a + b).toBe(expected)
|
|
341
|
+
})
|
|
342
|
+
})
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
## describe.for
|
|
346
|
+
|
|
347
|
+
- **Alias:** `suite.for`
|
|
348
|
+
|
|
349
|
+
The difference from `describe.each` is how array case is provided in the arguments.
|
|
350
|
+
Other non array case (including template string usage) works exactly same.
|
|
351
|
+
|
|
352
|
+
```ts
|
|
353
|
+
// `each` spreads array case
|
|
354
|
+
describe.each([
|
|
355
|
+
[1, 1, 2],
|
|
356
|
+
[1, 2, 3],
|
|
357
|
+
[2, 1, 3],
|
|
358
|
+
])('add(%i, %i) -> %i', (a, b, expected) => { // [!code --]
|
|
359
|
+
test('test', () => {
|
|
360
|
+
expect(a + b).toBe(expected)
|
|
361
|
+
})
|
|
362
|
+
})
|
|
363
|
+
|
|
364
|
+
// `for` doesn't spread array case
|
|
365
|
+
describe.for([
|
|
366
|
+
[1, 1, 2],
|
|
367
|
+
[1, 2, 3],
|
|
368
|
+
[2, 1, 3],
|
|
369
|
+
])('add(%i, %i) -> %i', ([a, b, expected]) => { // [!code ++]
|
|
370
|
+
test('test', () => {
|
|
371
|
+
expect(a + b).toBe(expected)
|
|
372
|
+
})
|
|
373
|
+
})
|
|
374
|
+
```
|