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: website/docs/14.x/docs/advanced/understanding-act.mdx
+9-5Lines changed: 9 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,9 +61,9 @@ test('render without act', () => {
61
61
When testing without `act` call wrapping rendering call, we see that the assertion runs just after the rendering but before `useEffect`hooks effects are applied. Which is not what we expected in our tests.
62
62
63
63
```jsx
64
-
test('render with act', () => {
64
+
test('render with act', async() => {
65
65
let renderer: ReactTestRenderer;
66
-
act(() => {
66
+
awaitact(async() => {
67
67
renderer =TestRenderer.create(<TestComponent />);
68
68
});
69
69
@@ -73,6 +73,8 @@ test('render with act', () => {
73
73
});
74
74
```
75
75
76
+
**Note**: In v14, `act` is now async by default and always returns a Promise. Even if your callback is synchronous, you should use `await act(async () => ...)`.
77
+
76
78
When wrapping rendering call with `act` we see that the changes caused by `useEffect` hook have been applied as we would expect.
77
79
78
80
### When to use act
@@ -95,7 +97,7 @@ Note that `act` calls can be safely nested and internally form a stack of calls.
95
97
96
98
As of React version of 18.1.0, the `act` implementation is defined in the [ReactAct.js source file](https://github.com/facebook/react/blob/main/packages/react/src/ReactAct.js) inside React repository. This implementation has been fairly stable since React 17.0.
97
99
98
-
RNTL exports `act` for convenience of the users as defined in the [act.ts source file](https://github.com/callstack/react-native-testing-library/blob/main/src/act.ts). That file refers to [ReactTestRenderer.js source](https://github.com/facebook/react/blob/ce13860281f833de8a3296b7a3dad9caced102e9/packages/react-test-renderer/src/ReactTestRenderer.js#L52) file from React Test Renderer package, which finally leads to Reactact implementation in ReactAct.js (already mentioned above).
100
+
RNTL exports `act` for convenience of the users as defined in the [act.ts source file](https://github.com/callstack/react-native-testing-library/blob/main/src/act.ts). In v14, `act` is now async by default and always returns a Promise, making it compatible with React 19, React Suspense, and `React.use()`. The underlying implementation still uses React's `act` function, but wraps it to ensure consistent async behavior.
99
101
100
102
## Asynchronous `act`
101
103
@@ -149,17 +151,19 @@ Note that this is not yet the infamous async act warning. It only asks us to wra
149
151
First solution is to use Jest's fake timers inside out tests:
**Note**: In v14, `act` is now async by default, so you should await it even when using fake timers.
166
+
163
167
That way we can wrap `jest.runAllTimers()` call which triggers the `setTimeout` updates inside an `act` call, hence resolving the act warning. Note that this whole code is synchronous thanks to usage of Jest fake timers.
0 commit comments