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
All the frameworks on this page support client-side rendering ([CSR](https://developer.mozilla.org/en-US/docs/Glossary/CSR)), single-page apps ([SPA](https://developer.mozilla.org/en-US/docs/Glossary/SPA)), and static-site generation ([SSG](https://developer.mozilla.org/en-US/docs/Glossary/SSG)). These apps can be deployed to a [CDN](https://developer.mozilla.org/en-US/docs/Glossary/CDN)or static hosting service without a server. Additionally, these frameworks allow you to add server-side rendering on a per-route basis, when it makes sense for your use case.
This allows you to start with a client-only app, and if your needs change later, you can opt-in to using server features on individual routes without rewriting your app. See your framework's documentation for configuring the rendering strategy.
Copy file name to clipboardExpand all lines: src/content/reference/react/act.md
+31-31Lines changed: 31 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,32 +4,32 @@ title: act
4
4
5
5
<Intro>
6
6
7
-
`act`is a test helper to apply pending React updates before making assertions.
7
+
`act`是个测试辅助工具,用于在断言前应用待处理的 React 更新。
8
8
9
9
```js
10
10
awaitact(async actFn)
11
11
```
12
12
13
13
</Intro>
14
14
15
-
To prepare a component for assertions, wrap the code rendering it and performing updates inside an `await act()`call. This makes your test run closer to how React works in the browser.
You might find using `act()`directly a bit too verbose. To avoid some of the boilerplate, you could use a library like [React Testing Library](https://testing-library.com/docs/react-testing-library/intro), whose helpers are wrapped with `act()`.
When writing UI tests, tasks like rendering, user events, or data fetching can be considered as “units” of interaction with a user interface. React provides a helper called `act()` that makes sure all updates related to these “units” have been processed and applied to the DOM before you make any assertions.
@@ -42,25 +42,25 @@ it ('renders with button disabled', async () => {
42
42
43
43
<Note>
44
44
45
-
We recommend using `act`with`await`and an `async`function. Although the sync version works in many cases, it doesn't work in all cases and due to the way React schedules updates internally, it's difficult to predict when you can use the sync version.
We will deprecate and remove the sync version in the future.
47
+
我们将在未来弃用并移除同步版本。
48
48
49
49
</Note>
50
50
51
-
#### Parameters {/*parameters*/}
51
+
#### 参数 {/*parameters*/}
52
52
53
-
*`async actFn`: An async function wrapping renders or interactions for components being tested. Any updates triggered within the `actFn`, are added to an internal act queue, which are then flushed together to process and apply any changes to the DOM. Since it is async, React will also run any code that crosses an async boundary, and flush any updates scheduled.
@@ -109,13 +109,13 @@ it('can render and update a counter', async () => {
109
109
});
110
110
```
111
111
112
-
Here, we create a container, append it to the document, and render the `Counter` component inside `act()`. This ensures that the component is rendered and its effects are applied before making assertions.
@@ -142,36 +142,36 @@ it.only('can render and update a counter', async () => {
142
142
});
143
143
```
144
144
145
-
Here, we render the component with `act`, and then dispatch the event inside another `act()`. This ensures that all updates from the event are applied before making assertions.
Don’t forget that dispatching DOM events only works when the DOM container is added to the document. You can use a library like [React Testing Library](https://testing-library.com/docs/react-testing-library/intro)to reduce the boilerplate code.
149
+
只有将 DOM 容器添加到文档后,触发 DOM 事件才会生效。可以使用 [React Testing Library](https://testing-library.com/docs/react-testing-library/intro)等库来减少样板代码。
150
150
151
151
</Pitfall>
152
152
153
-
## Troubleshooting {/*troubleshooting*/}
153
+
## 疑难解答 {/*troubleshooting*/}
154
154
155
-
### I'm getting an error: "The current testing environment is not configured to support act"(...)" {/*error-the-current-testing-environment-is-not-configured-to-support-act*/}
155
+
### 出现错误 "The current testing environment is not configured to support act"(...)" {/*error-the-current-testing-environment-is-not-configured-to-support-act*/}
156
156
157
-
Using`act`requires setting `global.IS_REACT_ACT_ENVIRONMENT=true`in your test environment. This is to ensure that `act`is only used in the correct environment.
If you don't set the global, you will see an error like this:
159
+
如果未设置该全局变量,将看到如下错误:
160
160
161
161
<ConsoleBlocklevel="error">
162
162
163
163
Warning: The current testing environment is not configured to support act(...)
164
164
165
165
</ConsoleBlock>
166
166
167
-
To fix, add this to your global setup file for React tests:
167
+
解决方法:在 React 测试的全局设置文件中添加:
168
168
169
169
```js
170
170
global.IS_REACT_ACT_ENVIRONMENT=true
171
171
```
172
172
173
173
<Note>
174
174
175
-
In testing frameworks like [React Testing Library](https://testing-library.com/docs/react-testing-library/intro), `IS_REACT_ACT_ENVIRONMENT`is already set for you.
0 commit comments