Skip to content

Commit 228067e

Browse files
hi-ogawaclaude
andauthored
docs: trace mark integration with vitest-browser-react/vue/svelte (#9828)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 99e52fe commit 228067e

File tree

3 files changed

+70
-28
lines changed

3 files changed

+70
-28
lines changed

docs/api/browser/react.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export function render(
3939
): Promise<RenderResult>
4040
```
4141

42+
The `render` function records a `react.render` trace mark, visible in the [Trace View](/guide/browser/trace-view).
43+
4244
:::warning
4345
Note that `render` is asynchronous, unlike in other packages. This is to support [`Suspense`](https://react.dev/reference/react/Suspense) correctly.
4446

@@ -154,6 +156,8 @@ This method is a shortcut for `console.log(prettyDOM(baseElement))`. It will pri
154156
function rerender(ui: React.ReactNode): Promise<void>
155157
```
156158

159+
Also records a `react.rerender` trace mark in the [Trace View](/guide/browser/trace-view).
160+
157161
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.
158162

159163
```jsx
@@ -171,6 +175,8 @@ await rerender(<NumberDisplay number={2} />)
171175
function unmount(): Promise<void>
172176
```
173177

178+
Also records a `react.unmount` trace mark in the [Trace View](/guide/browser/trace-view).
179+
174180
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).
175181

176182
```jsx

docs/api/browser/svelte.md

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { expect, test } from 'vitest'
1212
import Component from './Component.svelte'
1313

1414
test('counter button increments the count', async () => {
15-
const screen = render(Component, {
15+
const screen = await render(Component, {
1616
initialCount: 1,
1717
})
1818

@@ -39,15 +39,26 @@ export function render<C extends Component>(
3939
Component: ComponentImport<C>,
4040
options?: ComponentOptions<C>,
4141
renderOptions?: SetupOptions
42-
): RenderResult<C>
42+
): RenderResult<C> & PromiseLike<RenderResult<C>>
4343
```
4444

45+
The `render` function records a `svelte.render` trace mark, visible in the [Trace View](/guide/browser/trace-view).
46+
47+
::: warning
48+
Synchronous usage of `render` is deprecated and will be removed in the next major version. Please always `await` the result:
49+
50+
```ts
51+
const screen = render(Component) // [!code --]
52+
const screen = await render(Component) // [!code ++]
53+
```
54+
:::
55+
4556
### Options
4657

4758
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:
4859

4960
```ts
50-
const screen = render(Component, {
61+
const screen = await render(Component, {
5162
props: { // [!code --]
5263
initialCount: 1, // [!code --]
5364
}, // [!code --]
@@ -68,7 +79,7 @@ For example, if you are unit testing a `tbody` element, it cannot be a child of
6879
```ts
6980
const table = document.createElement('table')
7081
71-
const screen = render(TableBody, {
82+
const screen = await render(TableBody, {
7283
props,
7384
// ⚠️ appending the element to `body` manually before rendering
7485
target: document.body.appendChild(table),
@@ -86,7 +97,7 @@ If the `target` is specified, then this defaults to that, otherwise this default
8697
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).
8798

8899
```ts
89-
const screen = render(TableBody, props)
100+
const screen = await render(TableBody, props)
90101
91102
await screen.getByRole('link', { name: 'Expand' }).click()
92103
```
@@ -104,7 +115,7 @@ If you find yourself using `container` to query for rendered elements then you s
104115
The mounted Svelte component instance. You can use this to access component methods and properties if needed.
105116

106117
```ts
107-
const { component } = render(Counter, {
118+
const { component } = await render(Counter, {
108119
initialCount: 0,
109120
})
110121
@@ -118,7 +129,7 @@ The [locator](/api/browser/locators) of your `container`. It is useful to use qu
118129
```ts
119130
import { render } from 'vitest-browser-svelte'
120131
121-
const { locator } = render(NumberDisplay, {
132+
const { locator } = await render(NumberDisplay, {
122133
number: 2,
123134
})
124135
@@ -139,15 +150,15 @@ This method is a shortcut for `console.log(prettyDOM(baseElement))`. It will pri
139150
#### rerender
140151

141152
```ts
142-
function rerender(props: Partial<ComponentProps<T>>): void
153+
function rerender(props: Partial<ComponentProps<T>>): Promise<void>
143154
```
144155

145-
Updates the component's props and waits for Svelte to apply the changes. Use this to test how your component responds to prop changes.
156+
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).
146157

147158
```ts
148159
import { render } from 'vitest-browser-svelte'
149160
150-
const { rerender } = render(NumberDisplay, {
161+
const { rerender } = await render(NumberDisplay, {
151162
number: 1,
152163
})
153164
@@ -158,16 +169,20 @@ await rerender({ number: 2 })
158169
#### unmount
159170

160171
```ts
161-
function unmount(): void
172+
function unmount(): Promise<void>
162173
```
163174

164-
Unmount and destroy the Svelte component. 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).
175+
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).
176+
177+
::: warning
178+
Synchronous usage of `unmount` is deprecated and will be removed in the next major version. Please always `await` the result.
179+
:::
165180

166181
```ts
167182
import { render } from 'vitest-browser-svelte'
168183
169-
const { container, unmount } = render(Component)
170-
unmount()
184+
const { container, unmount } = await render(Component)
185+
await unmount()
171186
// your component has been unmounted and now: container.innerHTML === ''
172187
```
173188

@@ -193,7 +208,7 @@ locators.extend({
193208
},
194209
})
195210
196-
const screen = render(Component)
211+
const screen = await render(Component)
197212
await expect.element(
198213
screen.getByArticleTitle('Hello World')
199214
).toBeVisible()
@@ -211,7 +226,7 @@ import { expect, test } from 'vitest'
211226
import SubjectTest from './basic-snippet.test.svelte'
212227
213228
test('basic snippet', async () => {
214-
const screen = render(SubjectTest)
229+
const screen = await render(SubjectTest)
215230
216231
const heading = screen.getByRole('heading')
217232
const child = heading.getByTestId('child')
@@ -250,7 +265,7 @@ import { expect, test } from 'vitest'
250265
import Subject from './complex-snippet.svelte'
251266
252267
test('renders greeting in message snippet', async () => {
253-
const screen = render(Subject, {
268+
const screen = await render(Subject, {
254269
name: 'Alice',
255270
message: createRawSnippet(greeting => ({
256271
render: () => `<span data-testid="message">${greeting()}</span>`,

docs/api/browser/vue.md

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { expect, test } from 'vitest'
1212
import Component from './Component.vue'
1313

1414
test('counter button increments the count', async () => {
15-
const screen = render(Component, {
15+
const screen = await render(Component, {
1616
props: {
1717
initialCount: 1,
1818
}
@@ -40,9 +40,20 @@ The package exposes two entry points: `vitest-browser-vue` and `vitest-browser-v
4040
export function render(
4141
component: Component,
4242
options?: ComponentRenderOptions,
43-
): RenderResult
43+
): RenderResult & PromiseLike<RenderResult>
4444
```
4545

46+
The `render` function records a `vue.render` trace mark, visible in the [Trace View](/guide/browser/trace-view).
47+
48+
::: warning
49+
Synchronous usage of `render` is deprecated and will be removed in the next major version. Please always `await` the result:
50+
51+
```ts
52+
const screen = render(Component) // [!code --]
53+
const screen = await render(Component) // [!code ++]
54+
```
55+
:::
56+
4657
### Options
4758

4859
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,7 +67,7 @@ For example, if you are unit testing a `tbody` element, it cannot be a child of
5667
```js
5768
const table = document.createElement('table')
5869
59-
const { container } = render(TableBody, {
70+
const { container } = await render(TableBody, {
6071
props,
6172
// ⚠️ appending the element to `body` manually before rendering
6273
container: document.body.appendChild(table),
@@ -72,7 +83,7 @@ If the `container` is specified, then this defaults to that, otherwise this defa
7283
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).
7384

7485
```ts
75-
const screen = render(TableBody, { props })
86+
const screen = await render(TableBody, { props })
7687
7788
await screen.getByRole('link', { name: 'Expand' }).click()
7889
```
@@ -102,7 +113,7 @@ The [locator](/api/browser/locators) of your `container`. It is useful to use qu
102113
```js
103114
import { render } from 'vitest-browser-vue'
104115
105-
const { locator } = render(NumberDisplay, {
116+
const { locator } = await render(NumberDisplay, {
106117
props: { number: 2 }
107118
})
108119
@@ -125,27 +136,37 @@ This method is a shortcut for `console.log(prettyDOM(baseElement))`. It will pri
125136
#### rerender
126137

127138
```ts
128-
function rerender(props: Partial<Props>): void
139+
function rerender(props: Partial<Props>): void & PromiseLike<void>
129140
```
130141

142+
Also records a `vue.rerender` trace mark in the [Trace View](/guide/browser/trace-view).
143+
131144
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.
132145

146+
::: warning
147+
Synchronous usage of `rerender` is deprecated and will be removed in the next major version. Please always `await` the result.
148+
:::
149+
133150
```js
134151
import { render } from 'vitest-browser-vue'
135152
136-
const { rerender } = render(NumberDisplay, { props: { number: 1 } })
153+
const { rerender } = await render(NumberDisplay, { props: { number: 1 } })
137154
138155
// re-render the same component with different props
139-
rerender({ number: 2 })
156+
await rerender({ number: 2 })
140157
```
141158

142159
#### unmount
143160

144161
```ts
145-
function unmount(): void
162+
function unmount(): void & PromiseLike<void>
146163
```
147164

148-
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).
165+
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).
166+
167+
::: warning
168+
Synchronous usage of `unmount` is deprecated and will be removed in the next major version. Please always `await` the result.
169+
:::
149170

150171
#### emitted
151172

@@ -182,7 +203,7 @@ locators.extend({
182203
},
183204
})
184205
185-
const screen = render(Component)
206+
const screen = await render(Component)
186207
await expect.element(
187208
screen.getByArticleTitle('Hello World')
188209
).toBeVisible()

0 commit comments

Comments
 (0)