@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.
- package/README.md +1 -2
- package/index.d.ts +2 -2
- package/index.js +1 -1
- package/package.json +1 -1
- package/server.js +2 -4
- package/patterns/_meta.json +0 -67
- package/patterns/authoring-a-custom-vitest-agent-reporter.md +0 -82
- package/patterns/known-issues-and-caveats.md +0 -52
- package/patterns/operating-vitest-agent-as-an-agent.md +0 -62
- package/patterns/running-tests-via-mcp.md +0 -103
- package/patterns/silencing-leaking-output-in-tests.md +0 -91
- package/patterns/testing-effect-schema-definitions.md +0 -71
- package/patterns/testing-effect-services-with-mock-layers.md +0 -63
- package/resources/index.js +0 -166
- package/resources/indexes.js +0 -77
- package/resources/manifest-schema.js +0 -46
- package/resources/paths.js +0 -20
- package/resources/patterns.js +0 -22
- package/resources/upstream-docs.js +0 -22
- package/vendor/vitest-docs/ATTRIBUTION.md +0 -5
- package/vendor/vitest-docs/api/advanced/artifacts.md +0 -189
- package/vendor/vitest-docs/api/advanced/metadata.md +0 -68
- package/vendor/vitest-docs/api/advanced/plugin.md +0 -168
- package/vendor/vitest-docs/api/advanced/reporters.md +0 -342
- package/vendor/vitest-docs/api/advanced/runner.md +0 -334
- package/vendor/vitest-docs/api/advanced/test-case.md +0 -302
- package/vendor/vitest-docs/api/advanced/test-collection.md +0 -89
- package/vendor/vitest-docs/api/advanced/test-module.md +0 -140
- package/vendor/vitest-docs/api/advanced/test-project.md +0 -321
- package/vendor/vitest-docs/api/advanced/test-specification.md +0 -96
- package/vendor/vitest-docs/api/advanced/test-suite.md +0 -230
- package/vendor/vitest-docs/api/advanced/vitest.md +0 -684
- package/vendor/vitest-docs/api/assert-type.md +0 -22
- package/vendor/vitest-docs/api/assert.md +0 -1960
- package/vendor/vitest-docs/api/browser/assertions.md +0 -1277
- package/vendor/vitest-docs/api/browser/commands.md +0 -154
- package/vendor/vitest-docs/api/browser/context.md +0 -338
- package/vendor/vitest-docs/api/browser/interactivity.md +0 -681
- package/vendor/vitest-docs/api/browser/locators.md +0 -1171
- package/vendor/vitest-docs/api/browser/react.md +0 -346
- package/vendor/vitest-docs/api/browser/svelte.md +0 -292
- package/vendor/vitest-docs/api/browser/vue.md +0 -222
- package/vendor/vitest-docs/api/describe.md +0 -374
- package/vendor/vitest-docs/api/expect-typeof.md +0 -571
- package/vendor/vitest-docs/api/expect.md +0 -2304
- package/vendor/vitest-docs/api/hooks.md +0 -463
- package/vendor/vitest-docs/api/mock.md +0 -701
- package/vendor/vitest-docs/api/test.md +0 -926
- package/vendor/vitest-docs/api/vi.md +0 -1372
- package/vendor/vitest-docs/config/alias.md +0 -13
- package/vendor/vitest-docs/config/allowonly.md +0 -32
- package/vendor/vitest-docs/config/api.md +0 -27
- package/vendor/vitest-docs/config/attachmentsdir.md +0 -6
- package/vendor/vitest-docs/config/bail.md +0 -9
- package/vendor/vitest-docs/config/benchmark.md +0 -65
- package/vendor/vitest-docs/config/browser/api.md +0 -23
- package/vendor/vitest-docs/config/browser/commands.md +0 -6
- package/vendor/vitest-docs/config/browser/connecttimeout.md +0 -10
- package/vendor/vitest-docs/config/browser/detailspanelposition.md +0 -38
- package/vendor/vitest-docs/config/browser/enabled.md +0 -40
- package/vendor/vitest-docs/config/browser/expect.md +0 -250
- package/vendor/vitest-docs/config/browser/headless.md +0 -7
- package/vendor/vitest-docs/config/browser/instances.md +0 -47
- package/vendor/vitest-docs/config/browser/isolate.md +0 -11
- package/vendor/vitest-docs/config/browser/locators.md +0 -24
- package/vendor/vitest-docs/config/browser/orchestratorscripts.md +0 -39
- package/vendor/vitest-docs/config/browser/playwright.md +0 -214
- package/vendor/vitest-docs/config/browser/preview.md +0 -32
- package/vendor/vitest-docs/config/browser/provider.md +0 -79
- package/vendor/vitest-docs/config/browser/screenshotdirectory.md +0 -6
- package/vendor/vitest-docs/config/browser/screenshotfailures.md +0 -6
- package/vendor/vitest-docs/config/browser/testerhtmlpath.md +0 -5
- package/vendor/vitest-docs/config/browser/trace.md +0 -43
- package/vendor/vitest-docs/config/browser/trackunhandlederrors.md +0 -10
- package/vendor/vitest-docs/config/browser/ui.md +0 -7
- package/vendor/vitest-docs/config/browser/viewport.md +0 -6
- package/vendor/vitest-docs/config/browser/webdriverio.md +0 -64
- package/vendor/vitest-docs/config/cache.md +0 -26
- package/vendor/vitest-docs/config/chaiconfig.md +0 -29
- package/vendor/vitest-docs/config/clearmocks.md +0 -22
- package/vendor/vitest-docs/config/coverage.md +0 -455
- package/vendor/vitest-docs/config/css.md +0 -47
- package/vendor/vitest-docs/config/dangerouslyignoreunhandlederrors.md +0 -23
- package/vendor/vitest-docs/config/deps.md +0 -127
- package/vendor/vitest-docs/config/detectasyncleaks.md +0 -39
- package/vendor/vitest-docs/config/diff.md +0 -96
- package/vendor/vitest-docs/config/dir.md +0 -7
- package/vendor/vitest-docs/config/disableconsoleintercept.md +0 -15
- package/vendor/vitest-docs/config/env.md +0 -5
- package/vendor/vitest-docs/config/environment.md +0 -96
- package/vendor/vitest-docs/config/environmentoptions.md +0 -30
- package/vendor/vitest-docs/config/exclude.md +0 -49
- package/vendor/vitest-docs/config/execargv.md +0 -10
- package/vendor/vitest-docs/config/expandsnapshotdiff.md +0 -7
- package/vendor/vitest-docs/config/expect.md +0 -38
- package/vendor/vitest-docs/config/experimental.md +0 -510
- package/vendor/vitest-docs/config/faketimers.md +0 -51
- package/vendor/vitest-docs/config/fileparallelism.md +0 -11
- package/vendor/vitest-docs/config/forcereruntriggers.md +0 -19
- package/vendor/vitest-docs/config/globals.md +0 -42
- package/vendor/vitest-docs/config/globalsetup.md +0 -72
- package/vendor/vitest-docs/config/hideskippedtests.md +0 -7
- package/vendor/vitest-docs/config/hooktimeout.md +0 -7
- package/vendor/vitest-docs/config/include-source.md +0 -115
- package/vendor/vitest-docs/config/include.md +0 -71
- package/vendor/vitest-docs/config/includetasklocation.md +0 -17
- package/vendor/vitest-docs/config/index.md +0 -85
- package/vendor/vitest-docs/config/isolate.md +0 -13
- package/vendor/vitest-docs/config/logheapusage.md +0 -7
- package/vendor/vitest-docs/config/maxconcurrency.md +0 -9
- package/vendor/vitest-docs/config/maxworkers.md +0 -49
- package/vendor/vitest-docs/config/mockreset.md +0 -22
- package/vendor/vitest-docs/config/mode.md +0 -7
- package/vendor/vitest-docs/config/name.md +0 -111
- package/vendor/vitest-docs/config/onconsolelog.md +0 -25
- package/vendor/vitest-docs/config/onstacktrace.md +0 -32
- package/vendor/vitest-docs/config/onunhandlederror.md +0 -35
- package/vendor/vitest-docs/config/open.md +0 -7
- package/vendor/vitest-docs/config/outputfile.md +0 -7
- package/vendor/vitest-docs/config/passwithnotests.md +0 -7
- package/vendor/vitest-docs/config/pool.md +0 -45
- package/vendor/vitest-docs/config/printconsoletrace.md +0 -6
- package/vendor/vitest-docs/config/projects.md +0 -6
- package/vendor/vitest-docs/config/provide.md +0 -45
- package/vendor/vitest-docs/config/reporters.md +0 -69
- package/vendor/vitest-docs/config/resolvesnapshotpath.md +0 -36
- package/vendor/vitest-docs/config/restoremocks.md +0 -22
- package/vendor/vitest-docs/config/retry.md +0 -140
- package/vendor/vitest-docs/config/root.md +0 -6
- package/vendor/vitest-docs/config/runner.md +0 -6
- package/vendor/vitest-docs/config/sequence.md +0 -158
- package/vendor/vitest-docs/config/server.md +0 -68
- package/vendor/vitest-docs/config/setupfiles.md +0 -40
- package/vendor/vitest-docs/config/silent.md +0 -9
- package/vendor/vitest-docs/config/slowtestthreshold.md +0 -7
- package/vendor/vitest-docs/config/snapshotenvironment.md +0 -27
- package/vendor/vitest-docs/config/snapshotformat.md +0 -28
- package/vendor/vitest-docs/config/snapshotserializers.md +0 -6
- package/vendor/vitest-docs/config/stricttags.md +0 -30
- package/vendor/vitest-docs/config/tags.md +0 -141
- package/vendor/vitest-docs/config/teardowntimeout.md +0 -7
- package/vendor/vitest-docs/config/testnamepattern.md +0 -21
- package/vendor/vitest-docs/config/testtimeout.md +0 -7
- package/vendor/vitest-docs/config/typecheck.md +0 -77
- package/vendor/vitest-docs/config/ui.md +0 -15
- package/vendor/vitest-docs/config/unstubenvs.md +0 -20
- package/vendor/vitest-docs/config/unstubglobals.md +0 -20
- package/vendor/vitest-docs/config/update.md +0 -16
- package/vendor/vitest-docs/config/vmmemorylimit.md +0 -30
- package/vendor/vitest-docs/config/watch.md +0 -11
- package/vendor/vitest-docs/config/watchtriggerpatterns.md +0 -29
- package/vendor/vitest-docs/guide/advanced/index.md +0 -147
- package/vendor/vitest-docs/guide/advanced/pool.md +0 -148
- package/vendor/vitest-docs/guide/advanced/reporters.md +0 -93
- package/vendor/vitest-docs/guide/advanced/tests.md +0 -125
- package/vendor/vitest-docs/guide/browser/aria-snapshots.md +0 -470
- package/vendor/vitest-docs/guide/browser/component-testing.md +0 -571
- package/vendor/vitest-docs/guide/browser/index.md +0 -630
- package/vendor/vitest-docs/guide/browser/multiple-setups.md +0 -121
- package/vendor/vitest-docs/guide/browser/trace-view.md +0 -126
- package/vendor/vitest-docs/guide/browser/visual-regression-testing.md +0 -734
- package/vendor/vitest-docs/guide/cli-generated.md +0 -972
- package/vendor/vitest-docs/guide/cli.md +0 -234
- package/vendor/vitest-docs/guide/common-errors.md +0 -163
- package/vendor/vitest-docs/guide/coverage.md +0 -515
- package/vendor/vitest-docs/guide/debugging.md +0 -127
- package/vendor/vitest-docs/guide/environment.md +0 -101
- package/vendor/vitest-docs/guide/extending-matchers.md +0 -160
- package/vendor/vitest-docs/guide/features.md +0 -310
- package/vendor/vitest-docs/guide/filtering.md +0 -175
- package/vendor/vitest-docs/guide/ide.md +0 -43
- package/vendor/vitest-docs/guide/improving-performance.md +0 -245
- package/vendor/vitest-docs/guide/in-source.md +0 -159
- package/vendor/vitest-docs/guide/index.md +0 -128
- package/vendor/vitest-docs/guide/learn/async.md +0 -147
- package/vendor/vitest-docs/guide/learn/debugging-tests.md +0 -210
- package/vendor/vitest-docs/guide/learn/matchers.md +0 -277
- package/vendor/vitest-docs/guide/learn/mock-functions.md +0 -277
- package/vendor/vitest-docs/guide/learn/setup-teardown.md +0 -240
- package/vendor/vitest-docs/guide/learn/snapshots.md +0 -166
- package/vendor/vitest-docs/guide/learn/testing-in-practice.md +0 -430
- package/vendor/vitest-docs/guide/learn/writing-tests-with-ai.md +0 -127
- package/vendor/vitest-docs/guide/learn/writing-tests.md +0 -231
- package/vendor/vitest-docs/guide/lifecycle.md +0 -379
- package/vendor/vitest-docs/guide/migration.md +0 -863
- package/vendor/vitest-docs/guide/mocking/classes.md +0 -158
- package/vendor/vitest-docs/guide/mocking/dates.md +0 -52
- package/vendor/vitest-docs/guide/mocking/file-system.md +0 -74
- package/vendor/vitest-docs/guide/mocking/functions.md +0 -61
- package/vendor/vitest-docs/guide/mocking/globals.md +0 -20
- package/vendor/vitest-docs/guide/mocking/modules.md +0 -414
- package/vendor/vitest-docs/guide/mocking/requests.md +0 -114
- package/vendor/vitest-docs/guide/mocking/timers.md +0 -48
- package/vendor/vitest-docs/guide/mocking.md +0 -239
- package/vendor/vitest-docs/guide/open-telemetry.md +0 -156
- package/vendor/vitest-docs/guide/parallelism.md +0 -82
- package/vendor/vitest-docs/guide/profiling-test-performance.md +0 -243
- package/vendor/vitest-docs/guide/projects.md +0 -291
- package/vendor/vitest-docs/guide/recipes.md +0 -59
- package/vendor/vitest-docs/guide/reporters.md +0 -723
- package/vendor/vitest-docs/guide/snapshot.md +0 -620
- package/vendor/vitest-docs/guide/test-annotations.md +0 -103
- package/vendor/vitest-docs/guide/test-context.md +0 -902
- package/vendor/vitest-docs/guide/test-tags.md +0 -314
- package/vendor/vitest-docs/guide/testing-types.md +0 -149
- package/vendor/vitest-docs/guide/ui.md +0 -160
- package/vendor/vitest-docs/guide/using-plugins.md +0 -5
- package/vendor/vitest-docs/manifest.json +0 -1691
|
@@ -1,346 +0,0 @@
|
|
|
1
|
-
# vitest-browser-react
|
|
2
|
-
|
|
3
|
-
The community [`vitest-browser-react`](https://npmx.dev/package/vitest-browser-react) package renders [React](https://react.dev/) components in [Browser Mode](/guide/browser/).
|
|
4
|
-
|
|
5
|
-
```jsx
|
|
6
|
-
import { render } from 'vitest-browser-react'
|
|
7
|
-
import { expect, test } from 'vitest'
|
|
8
|
-
import Component from './Component.jsx'
|
|
9
|
-
|
|
10
|
-
test('counter button increments the count', async () => {
|
|
11
|
-
const screen = await render(<Component count={1} />)
|
|
12
|
-
|
|
13
|
-
await screen.getByRole('button', { name: 'Increment' }).click()
|
|
14
|
-
|
|
15
|
-
await expect.element(screen.getByText('Count is 2')).toBeVisible()
|
|
16
|
-
})
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
::: warning
|
|
20
|
-
This library takes inspiration from [`@testing-library/react`](https://github.com/testing-library/react-testing-library).
|
|
21
|
-
|
|
22
|
-
If you have used `@testing-library/react` in your tests before, you can keep using it, however the `vitest-browser-react` package provides certain benefits unique to the Browser Mode that `@testing-library/react` lacks:
|
|
23
|
-
|
|
24
|
-
`vitest-browser-react` 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.
|
|
25
|
-
:::
|
|
26
|
-
|
|
27
|
-
The package exposes two entry points: `vitest-browser-react` and `vitest-browser-react/pure`. They expose almost identical API (`pure` also exposes `configure`), but the `pure` entry point doesn't add a handler to remove the component before the next test has started.
|
|
28
|
-
|
|
29
|
-
## render
|
|
30
|
-
|
|
31
|
-
```ts
|
|
32
|
-
export function render(
|
|
33
|
-
ui: React.ReactNode,
|
|
34
|
-
options?: ComponentRenderOptions,
|
|
35
|
-
): Promise<RenderResult>
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
The `render` function records a `react.render` trace mark, visible in the [Trace View](/guide/browser/trace-view).
|
|
39
|
-
|
|
40
|
-
:::warning
|
|
41
|
-
Note that `render` is asynchronous, unlike in other packages. This is to support [`Suspense`](https://react.dev/reference/react/Suspense) correctly.
|
|
42
|
-
|
|
43
|
-
```tsx
|
|
44
|
-
import { render } from 'vitest-browser-react'
|
|
45
|
-
const screen = render(<Component />) // [!code --]
|
|
46
|
-
const screen = await render(<Component />) // [!code ++]
|
|
47
|
-
```
|
|
48
|
-
:::
|
|
49
|
-
|
|
50
|
-
### Options
|
|
51
|
-
|
|
52
|
-
#### container
|
|
53
|
-
|
|
54
|
-
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`.
|
|
55
|
-
|
|
56
|
-
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.
|
|
57
|
-
|
|
58
|
-
```jsx
|
|
59
|
-
const table = document.createElement('table')
|
|
60
|
-
|
|
61
|
-
const { container } = await render(<TableBody {...props} />, {
|
|
62
|
-
// ⚠️ appending the element to `body` manually before rendering
|
|
63
|
-
container: document.body.appendChild(table),
|
|
64
|
-
})
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
#### baseElement
|
|
68
|
-
|
|
69
|
-
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()`.
|
|
70
|
-
|
|
71
|
-
#### wrapper
|
|
72
|
-
|
|
73
|
-
Pass a React Component as the `wrapper` option to have it rendered around the inner element. This is most useful for creating reusable custom render functions for common data providers. For example:
|
|
74
|
-
|
|
75
|
-
```jsx
|
|
76
|
-
import React from 'react'
|
|
77
|
-
import { render } from 'vitest-browser-react'
|
|
78
|
-
import { ThemeProvider } from 'my-ui-lib'
|
|
79
|
-
import { TranslationProvider } from 'my-i18n-lib'
|
|
80
|
-
|
|
81
|
-
function AllTheProviders({ children }) {
|
|
82
|
-
return (
|
|
83
|
-
<ThemeProvider theme="light">
|
|
84
|
-
<TranslationProvider>
|
|
85
|
-
{children}
|
|
86
|
-
</TranslationProvider>
|
|
87
|
-
</ThemeProvider>
|
|
88
|
-
)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export function customRender(ui, options) {
|
|
92
|
-
return render(ui, { wrapper: AllTheProviders, ...options })
|
|
93
|
-
}
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
### Render Result
|
|
97
|
-
|
|
98
|
-
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).
|
|
99
|
-
|
|
100
|
-
```tsx
|
|
101
|
-
const screen = await render(<TableBody {...props} />)
|
|
102
|
-
|
|
103
|
-
await screen.getByRole('link', { name: 'Expand' }).click()
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
#### container
|
|
107
|
-
|
|
108
|
-
The containing `div` DOM node of your rendered React Element (rendered using `ReactDOM.render`). This is a regular DOM node, so you technically could call `container.querySelector` etc. to inspect the children.
|
|
109
|
-
|
|
110
|
-
:::danger
|
|
111
|
-
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!
|
|
112
|
-
:::
|
|
113
|
-
|
|
114
|
-
#### baseElement
|
|
115
|
-
|
|
116
|
-
The containing DOM node where your React Element is rendered in the `container`. If you don't specify the `baseElement` in the options of render, it will default to `document.body`.
|
|
117
|
-
|
|
118
|
-
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.
|
|
119
|
-
|
|
120
|
-
:::tip
|
|
121
|
-
The queries returned by the `render` looks into `baseElement`, so you can use queries to test your portal component without the `baseElement`.
|
|
122
|
-
:::
|
|
123
|
-
|
|
124
|
-
#### locator
|
|
125
|
-
|
|
126
|
-
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:
|
|
127
|
-
|
|
128
|
-
```jsx
|
|
129
|
-
import { render } from 'vitest-browser-react'
|
|
130
|
-
|
|
131
|
-
const { locator } = await render(<NumberDisplay number={1} />)
|
|
132
|
-
|
|
133
|
-
await locator.getByRole('button').click()
|
|
134
|
-
await expect.element(locator).toHaveTextContent('Hello World')
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
#### debug
|
|
138
|
-
|
|
139
|
-
```ts
|
|
140
|
-
function debug(
|
|
141
|
-
el?: HTMLElement | HTMLElement[] | Locator | Locator[],
|
|
142
|
-
maxLength?: number,
|
|
143
|
-
options?: PrettyDOMOptions,
|
|
144
|
-
): void
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
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.
|
|
148
|
-
|
|
149
|
-
#### rerender
|
|
150
|
-
|
|
151
|
-
```ts
|
|
152
|
-
function rerender(ui: React.ReactNode): Promise<void>
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
Also records a `react.rerender` trace mark in the [Trace View](/guide/browser/trace-view).
|
|
156
|
-
|
|
157
|
-
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.
|
|
158
|
-
|
|
159
|
-
```jsx
|
|
160
|
-
import { render } from 'vitest-browser-react'
|
|
161
|
-
|
|
162
|
-
const { rerender } = await render(<NumberDisplay number={1} />)
|
|
163
|
-
|
|
164
|
-
// re-render the same component with different props
|
|
165
|
-
await rerender(<NumberDisplay number={2} />)
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
#### unmount
|
|
169
|
-
|
|
170
|
-
```ts
|
|
171
|
-
function unmount(): Promise<void>
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
Also records a `react.unmount` trace mark in the [Trace View](/guide/browser/trace-view).
|
|
175
|
-
|
|
176
|
-
This will cause the rendered component to be unmounted. 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).
|
|
177
|
-
|
|
178
|
-
```jsx
|
|
179
|
-
import { render } from 'vitest-browser-react'
|
|
180
|
-
|
|
181
|
-
const { container, unmount } = await render(<Login />)
|
|
182
|
-
await unmount()
|
|
183
|
-
// your component has been unmounted and now: container.innerHTML === ''
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
#### asFragment
|
|
187
|
-
|
|
188
|
-
```ts
|
|
189
|
-
function asFragment(): DocumentFragment
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
Returns a `DocumentFragment` of your rendered component. This can be useful if you need to avoid live bindings and see how your component reacts to events.
|
|
193
|
-
|
|
194
|
-
## cleanup
|
|
195
|
-
|
|
196
|
-
```ts
|
|
197
|
-
export function cleanup(): Promise<void>
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
Remove all components rendered with [`render`](#render).
|
|
201
|
-
|
|
202
|
-
## renderHook
|
|
203
|
-
|
|
204
|
-
```ts
|
|
205
|
-
export function renderHook<Props, Result>(
|
|
206
|
-
renderCallback: (initialProps?: Props) => Result,
|
|
207
|
-
options: RenderHookOptions<Props>,
|
|
208
|
-
): Promise<RenderHookResult<Result, Props>>
|
|
209
|
-
```
|
|
210
|
-
|
|
211
|
-
This is a convenience wrapper around `render` with a custom test component. The API emerged from a popular testing pattern and is mostly interesting for libraries publishing hooks. You should prefer `render` since a custom test component results in more readable and robust tests since the thing you want to test is not hidden behind an abstraction.
|
|
212
|
-
|
|
213
|
-
```jsx
|
|
214
|
-
import { renderHook } from 'vitest-browser-react'
|
|
215
|
-
|
|
216
|
-
test('returns logged in user', async () => {
|
|
217
|
-
const { result } = await renderHook(() => useLoggedInUser())
|
|
218
|
-
expect(result.current).toEqual({ name: 'Alice' })
|
|
219
|
-
})
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
### Options
|
|
223
|
-
|
|
224
|
-
`renderHook` accepts the same options as [`render`](#render) with an addition to `initialProps`:
|
|
225
|
-
|
|
226
|
-
It declares the props that are passed to the render-callback when first invoked. These will not be passed if you call `rerender` without props.
|
|
227
|
-
|
|
228
|
-
```jsx
|
|
229
|
-
import { renderHook } from 'vitest-browser-react'
|
|
230
|
-
|
|
231
|
-
test('returns logged in user', async () => {
|
|
232
|
-
const { result, rerender } = await renderHook((props = {}) => props, {
|
|
233
|
-
initialProps: { name: 'Alice' },
|
|
234
|
-
})
|
|
235
|
-
expect(result.current).toEqual({ name: 'Alice' })
|
|
236
|
-
await rerender()
|
|
237
|
-
expect(result.current).toEqual({ name: undefined })
|
|
238
|
-
})
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
:::warning
|
|
242
|
-
When using `renderHook` in conjunction with the `wrapper` and `initialProps` options, the `initialProps` are not passed to the `wrapper` component. To provide props to the `wrapper` component, consider a solution like this:
|
|
243
|
-
|
|
244
|
-
```jsx
|
|
245
|
-
function createWrapper(Wrapper, props) {
|
|
246
|
-
return function CreatedWrapper({ children }) {
|
|
247
|
-
return <Wrapper {...props}>{children}</Wrapper>
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// ...
|
|
252
|
-
|
|
253
|
-
await renderHook(() => {}, {
|
|
254
|
-
wrapper: createWrapper(Wrapper, { value: 'foo' }),
|
|
255
|
-
})
|
|
256
|
-
```
|
|
257
|
-
:::
|
|
258
|
-
|
|
259
|
-
`renderHook` returns a few useful methods and properties:
|
|
260
|
-
|
|
261
|
-
### Render Hook Result
|
|
262
|
-
|
|
263
|
-
#### result
|
|
264
|
-
|
|
265
|
-
Holds the value of the most recently committed return value of the render-callback:
|
|
266
|
-
|
|
267
|
-
```jsx
|
|
268
|
-
import { useState } from 'react'
|
|
269
|
-
import { renderHook } from 'vitest-browser-react'
|
|
270
|
-
import { expect } from 'vitest'
|
|
271
|
-
|
|
272
|
-
const { result } = await renderHook(() => {
|
|
273
|
-
const [name, setName] = useState('')
|
|
274
|
-
React.useEffect(() => {
|
|
275
|
-
setName('Alice')
|
|
276
|
-
}, [])
|
|
277
|
-
|
|
278
|
-
return name
|
|
279
|
-
})
|
|
280
|
-
|
|
281
|
-
expect(result.current).toBe('Alice')
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
Note that the value is held in `result.current`. Think of result as a [ref](https://react.dev/learn/referencing-values-with-refs) for the most recently committed value.
|
|
285
|
-
|
|
286
|
-
#### rerender {#renderhooks-rerender}
|
|
287
|
-
|
|
288
|
-
Renders the previously rendered render-callback with the new props:
|
|
289
|
-
|
|
290
|
-
```jsx
|
|
291
|
-
import { renderHook } from 'vitest-browser-react'
|
|
292
|
-
|
|
293
|
-
const { rerender } = await renderHook(({ name = 'Alice' } = {}) => name)
|
|
294
|
-
|
|
295
|
-
// re-render the same hook with different props
|
|
296
|
-
await rerender({ name: 'Bob' })
|
|
297
|
-
```
|
|
298
|
-
|
|
299
|
-
#### unmount {#renderhooks-unmount}
|
|
300
|
-
|
|
301
|
-
Unmounts the test hook.
|
|
302
|
-
|
|
303
|
-
```jsx
|
|
304
|
-
import { renderHook } from 'vitest-browser-react'
|
|
305
|
-
|
|
306
|
-
const { unmount } = await renderHook(({ name = 'Alice' } = {}) => name)
|
|
307
|
-
|
|
308
|
-
await unmount()
|
|
309
|
-
```
|
|
310
|
-
|
|
311
|
-
## Extend Queries
|
|
312
|
-
|
|
313
|
-
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:
|
|
314
|
-
|
|
315
|
-
```jsx {5-7,12}
|
|
316
|
-
import { locators } from 'vitest/browser'
|
|
317
|
-
import { render } from 'vitest-browser-react'
|
|
318
|
-
|
|
319
|
-
locators.extend({
|
|
320
|
-
getByArticleTitle(title) {
|
|
321
|
-
return `[data-title="${title}"]`
|
|
322
|
-
},
|
|
323
|
-
})
|
|
324
|
-
|
|
325
|
-
const screen = await render(<Component />)
|
|
326
|
-
await expect.element(
|
|
327
|
-
screen.getByArticleTitle('Hello World')
|
|
328
|
-
).toBeVisible()
|
|
329
|
-
```
|
|
330
|
-
|
|
331
|
-
## Configuration
|
|
332
|
-
|
|
333
|
-
You can configure if the component should be rendered in Strict Mode with configure method from `vitest-browser-react/pure`:
|
|
334
|
-
|
|
335
|
-
```js
|
|
336
|
-
import { configure } from 'vitest-browser-react/pure'
|
|
337
|
-
|
|
338
|
-
configure({
|
|
339
|
-
// disabled by default
|
|
340
|
-
reactStrictMode: true,
|
|
341
|
-
})
|
|
342
|
-
```
|
|
343
|
-
|
|
344
|
-
## See also
|
|
345
|
-
|
|
346
|
-
- [React Testing Library documentation](https://testing-library.com/docs/react-testing-library/intro)
|
|
@@ -1,292 +0,0 @@
|
|
|
1
|
-
# vitest-browser-svelte
|
|
2
|
-
|
|
3
|
-
The community [`vitest-browser-svelte`](https://npmx.dev/package/vitest-browser-svelte) package renders [Svelte](https://svelte.dev/) components in [Browser Mode](/guide/browser/).
|
|
4
|
-
|
|
5
|
-
```ts
|
|
6
|
-
import { render } from 'vitest-browser-svelte'
|
|
7
|
-
import { expect, test } from 'vitest'
|
|
8
|
-
import Component from './Component.svelte'
|
|
9
|
-
|
|
10
|
-
test('counter button increments the count', async () => {
|
|
11
|
-
const screen = await render(Component, {
|
|
12
|
-
initialCount: 1,
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
await screen.getByRole('button', { name: 'Increment' }).click()
|
|
16
|
-
|
|
17
|
-
await expect.element(screen.getByText('Count is 2')).toBeVisible()
|
|
18
|
-
})
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
::: warning
|
|
22
|
-
This library takes inspiration from [`@testing-library/svelte`](https://github.com/testing-library/svelte-testing-library).
|
|
23
|
-
|
|
24
|
-
If you have used `@testing-library/svelte` in your tests before, you can keep using it, however the `vitest-browser-svelte` package provides certain benefits unique to the Browser Mode that `@testing-library/svelte` lacks:
|
|
25
|
-
|
|
26
|
-
`vitest-browser-svelte` 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.
|
|
27
|
-
:::
|
|
28
|
-
|
|
29
|
-
The package exposes two entry points: `vitest-browser-svelte` and `vitest-browser-svelte/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.
|
|
30
|
-
|
|
31
|
-
## render
|
|
32
|
-
|
|
33
|
-
```ts
|
|
34
|
-
export function render<C extends Component>(
|
|
35
|
-
Component: ComponentImport<C>,
|
|
36
|
-
options?: ComponentOptions<C>,
|
|
37
|
-
renderOptions?: SetupOptions
|
|
38
|
-
): RenderResult<C> & PromiseLike<RenderResult<C>>
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
The `render` function records a `svelte.render` trace mark, visible in the [Trace View](/guide/browser/trace-view).
|
|
42
|
-
|
|
43
|
-
::: warning
|
|
44
|
-
Synchronous usage of `render` is deprecated and will be removed in the next major version. Please always `await` the result:
|
|
45
|
-
|
|
46
|
-
```ts
|
|
47
|
-
const screen = render(Component) // [!code --]
|
|
48
|
-
const screen = await render(Component) // [!code ++]
|
|
49
|
-
```
|
|
50
|
-
:::
|
|
51
|
-
|
|
52
|
-
### Options
|
|
53
|
-
|
|
54
|
-
The `render` function supports either options that you can pass down to [`mount`](https://svelte.dev/docs/svelte/imperative-component-api#mount) or props directly:
|
|
55
|
-
|
|
56
|
-
```ts
|
|
57
|
-
const screen = await render(Component, {
|
|
58
|
-
props: { // [!code --]
|
|
59
|
-
initialCount: 1, // [!code --]
|
|
60
|
-
}, // [!code --]
|
|
61
|
-
initialCount: 1, // [!code ++]
|
|
62
|
-
})
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
#### props
|
|
66
|
-
|
|
67
|
-
Component props.
|
|
68
|
-
|
|
69
|
-
#### target
|
|
70
|
-
|
|
71
|
-
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`.
|
|
72
|
-
|
|
73
|
-
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.
|
|
74
|
-
|
|
75
|
-
```ts
|
|
76
|
-
const table = document.createElement('table')
|
|
77
|
-
|
|
78
|
-
const screen = await render(TableBody, {
|
|
79
|
-
props,
|
|
80
|
-
// ⚠️ appending the element to `body` manually before rendering
|
|
81
|
-
target: document.body.appendChild(table),
|
|
82
|
-
})
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
#### baseElement
|
|
86
|
-
|
|
87
|
-
This can be passed down in a third argument. You should rarely, if ever, need to use this option.
|
|
88
|
-
|
|
89
|
-
If the `target` 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()`.
|
|
90
|
-
|
|
91
|
-
### Render Result
|
|
92
|
-
|
|
93
|
-
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).
|
|
94
|
-
|
|
95
|
-
```ts
|
|
96
|
-
const screen = await render(TableBody, props)
|
|
97
|
-
|
|
98
|
-
await screen.getByRole('link', { name: 'Expand' }).click()
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
#### container
|
|
102
|
-
|
|
103
|
-
The containing DOM node where your Svelte component is rendered. This is a regular DOM node, so you technically could call `container.querySelector` etc. to inspect the children.
|
|
104
|
-
|
|
105
|
-
:::danger
|
|
106
|
-
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!
|
|
107
|
-
:::
|
|
108
|
-
|
|
109
|
-
#### component
|
|
110
|
-
|
|
111
|
-
The mounted Svelte component instance. You can use this to access component methods and properties if needed.
|
|
112
|
-
|
|
113
|
-
```ts
|
|
114
|
-
const { component } = await render(Counter, {
|
|
115
|
-
initialCount: 0,
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
// Access component exports if needed
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
#### locator
|
|
122
|
-
|
|
123
|
-
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:
|
|
124
|
-
|
|
125
|
-
```ts
|
|
126
|
-
import { render } from 'vitest-browser-svelte'
|
|
127
|
-
|
|
128
|
-
const { locator } = await render(NumberDisplay, {
|
|
129
|
-
number: 2,
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
await locator.getByRole('button').click()
|
|
133
|
-
await expect.element(locator).toHaveTextContent('Hello World')
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
#### debug
|
|
137
|
-
|
|
138
|
-
```ts
|
|
139
|
-
function debug(
|
|
140
|
-
el?: HTMLElement | HTMLElement[] | Locator | Locator[],
|
|
141
|
-
): void
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
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.
|
|
145
|
-
|
|
146
|
-
#### rerender
|
|
147
|
-
|
|
148
|
-
```ts
|
|
149
|
-
function rerender(props: Partial<ComponentProps<T>>): Promise<void>
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
Updates the component's props and waits for Svelte to apply the changes. Use this to test how your component responds to prop changes. Also records a `svelte.rerender` trace mark in the [Trace View](/guide/browser/trace-view).
|
|
153
|
-
|
|
154
|
-
```ts
|
|
155
|
-
import { render } from 'vitest-browser-svelte'
|
|
156
|
-
|
|
157
|
-
const { rerender } = await render(NumberDisplay, {
|
|
158
|
-
number: 1,
|
|
159
|
-
})
|
|
160
|
-
|
|
161
|
-
// re-render the same component with different props
|
|
162
|
-
await rerender({ number: 2 })
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
#### unmount
|
|
166
|
-
|
|
167
|
-
```ts
|
|
168
|
-
function unmount(): Promise<void>
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
Unmount and destroy the Svelte component. Also records a `svelte.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).
|
|
172
|
-
|
|
173
|
-
::: warning
|
|
174
|
-
Synchronous usage of `unmount` is deprecated and will be removed in the next major version. Please always `await` the result.
|
|
175
|
-
:::
|
|
176
|
-
|
|
177
|
-
```ts
|
|
178
|
-
import { render } from 'vitest-browser-svelte'
|
|
179
|
-
|
|
180
|
-
const { container, unmount } = await render(Component)
|
|
181
|
-
await unmount()
|
|
182
|
-
// your component has been unmounted and now: container.innerHTML === ''
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
## cleanup
|
|
186
|
-
|
|
187
|
-
```ts
|
|
188
|
-
export function cleanup(): void
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
Remove all components rendered with [`render`](#render).
|
|
192
|
-
|
|
193
|
-
## Extend Queries
|
|
194
|
-
|
|
195
|
-
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:
|
|
196
|
-
|
|
197
|
-
```ts {5-7,12}
|
|
198
|
-
import { locators } from 'vitest/browser'
|
|
199
|
-
import { render } from 'vitest-browser-svelte'
|
|
200
|
-
|
|
201
|
-
locators.extend({
|
|
202
|
-
getByArticleTitle(title) {
|
|
203
|
-
return `[data-title="${title}"]`
|
|
204
|
-
},
|
|
205
|
-
})
|
|
206
|
-
|
|
207
|
-
const screen = await render(Component)
|
|
208
|
-
await expect.element(
|
|
209
|
-
screen.getByArticleTitle('Hello World')
|
|
210
|
-
).toBeVisible()
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
## Snippets
|
|
214
|
-
|
|
215
|
-
For simple snippets, you can use a wrapper component and "dummy" children to test them. Setting `data-testid` attributes can be helpful when testing slots in this manner.
|
|
216
|
-
|
|
217
|
-
::: code-group
|
|
218
|
-
```ts [basic.test.js]
|
|
219
|
-
import { render } from 'vitest-browser-svelte'
|
|
220
|
-
import { expect, test } from 'vitest'
|
|
221
|
-
|
|
222
|
-
import SubjectTest from './basic-snippet.test.svelte'
|
|
223
|
-
|
|
224
|
-
test('basic snippet', async () => {
|
|
225
|
-
const screen = await render(SubjectTest)
|
|
226
|
-
|
|
227
|
-
const heading = screen.getByRole('heading')
|
|
228
|
-
const child = heading.getByTestId('child')
|
|
229
|
-
|
|
230
|
-
await expect.element(child).toBeInTheDocument()
|
|
231
|
-
})
|
|
232
|
-
```
|
|
233
|
-
```svelte [basic-snippet.svelte]
|
|
234
|
-
<script>
|
|
235
|
-
let { children } = $props()
|
|
236
|
-
</script>
|
|
237
|
-
|
|
238
|
-
<h1>
|
|
239
|
-
{@render children?.()}
|
|
240
|
-
</h1>
|
|
241
|
-
```
|
|
242
|
-
```svelte [basic-snippet.test.svelte]
|
|
243
|
-
<script>
|
|
244
|
-
import Subject from './basic-snippet.svelte'
|
|
245
|
-
</script>
|
|
246
|
-
|
|
247
|
-
<Subject>
|
|
248
|
-
<span data-testid="child"></span>
|
|
249
|
-
</Subject>
|
|
250
|
-
```
|
|
251
|
-
:::
|
|
252
|
-
|
|
253
|
-
For more complex snippets, e.g. where you want to check arguments, you can use Svelte's [`createRawSnippet`](https://svelte.dev/docs/svelte/svelte#createRawSnippet) API.
|
|
254
|
-
|
|
255
|
-
::: code-group
|
|
256
|
-
```js [complex-snippet.test.js]
|
|
257
|
-
import { render } from 'vitest-browser-svelte'
|
|
258
|
-
import { createRawSnippet } from 'svelte'
|
|
259
|
-
import { expect, test } from 'vitest'
|
|
260
|
-
|
|
261
|
-
import Subject from './complex-snippet.svelte'
|
|
262
|
-
|
|
263
|
-
test('renders greeting in message snippet', async () => {
|
|
264
|
-
const screen = await render(Subject, {
|
|
265
|
-
name: 'Alice',
|
|
266
|
-
message: createRawSnippet(greeting => ({
|
|
267
|
-
render: () => `<span data-testid="message">${greeting()}</span>`,
|
|
268
|
-
})),
|
|
269
|
-
})
|
|
270
|
-
|
|
271
|
-
const message = screen.getByTestId('message')
|
|
272
|
-
|
|
273
|
-
await expect.element(message).toHaveTextContent('Hello, Alice!')
|
|
274
|
-
})
|
|
275
|
-
```
|
|
276
|
-
```svelte [complex-snippet.svelte]
|
|
277
|
-
<script>
|
|
278
|
-
let { name, message } = $props()
|
|
279
|
-
|
|
280
|
-
const greeting = $derived(`Hello, ${name}!`)
|
|
281
|
-
</script>
|
|
282
|
-
|
|
283
|
-
<p>
|
|
284
|
-
{@render message?.(greeting)}
|
|
285
|
-
</p>
|
|
286
|
-
```
|
|
287
|
-
:::
|
|
288
|
-
|
|
289
|
-
## See also
|
|
290
|
-
|
|
291
|
-
- [Svelte Testing Library documentation](https://testing-library.com/docs/svelte-testing-library/intro)
|
|
292
|
-
- [Svelte Testing Library examples](https://github.com/testing-library/svelte-testing-library/tree/main/examples)
|