You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: api/browser/react.md
+47-47Lines changed: 47 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
---
2
2
outline: deep
3
3
---
4
-
<!-- TODO: translation -->
4
+
5
5
# vitest-browser-react
6
6
7
-
The community [`vitest-browser-react`](https://www.npmjs.com/package/vitest-browser-react)package renders [React](https://react.dev/)components in [Browser Mode](/guide/browser/).
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:
`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.
28
+
`vitest-browser-react`返回的 API 能与内置的 [定位器](/api/browser/locators)、[用户事件](/api/browser/interactivity)及 [断言](/api/browser/assertions) 更好的协作。例如:即使组件在断言间被重新渲染,Vitest 仍会自动重试元素查找,直至断言成功。
29
29
:::
30
30
31
-
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.
Bydefault, Vitestwillcreatea`div`, appenditto`document.body`, andrenderyourcomponentthere. Ifyouprovideyourown`HTMLElement`container, itwillnotbeappendedautomatically — you'll need to call `document.body.appendChild(container)` before `render`.
@@ -95,9 +95,9 @@ export function customRender(ui, options) {
95
95
}
96
96
```
97
97
98
-
### RenderResult
98
+
### 渲染结果 {#render-result}
99
99
100
-
Inadditiontodocumentedreturnvalue, the`render`function also returns all available [locators](/api/browser/locators) relative to the [`baseElement`](#baseelement), including [custom ones](/api/browser/locators#custom-locators).
The containing `div` DOM node of your rendered React Element (renderedusing`ReactDOM.render`). This is a regular DOM node, so you technically could call `container.querySelector` etc. to inspect the children.
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!
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`.
118
+
React元素将被渲染到 `container` 这个 DOM 容器中。如果在调用 render 时没有通过`baseElement`选项指定容器,则默认使用 `document.body`。
119
119
120
-
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.
120
+
适用于被测组件需要在容器 `div` 之外渲染内容,例如,对直接渲染到 body 的传送门组件进行快照测试。
121
121
122
122
:::tip
123
-
The queries returned by the `render` looks into `baseElement`, so you can use queries to test your portal component without the `baseElement`.
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:
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.
function rerender(ui: React.ReactNode): Promise<void>
155
155
```
156
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.
This will cause the rendered component to be unmounted. This is useful for testing what happens when your component is removed from the page (liketestingthatyoudon't leave event handlers hanging around causing memory leaks).
@@ -206,7 +206,7 @@ export function renderHook<Props, Result>(
206
206
): Promise<RenderHookResult<Result, Props>>
207
207
```
208
208
209
-
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.
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:
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.
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:
0 commit comments