Skip to content

Commit a7de170

Browse files
committed
docs(no-unnecessary-act): add rule details
1 parent 489be04 commit a7de170

File tree

2 files changed

+118
-2
lines changed

2 files changed

+118
-2
lines changed

docs/rules/no-unnecessary-act.md

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,117 @@
1-
// TODO
1+
# Disallow wrapping Testing Library utils or empty callbacks in `act` (`testing-library/no-unnecessary-act`)
2+
3+
> ⚠️ The `act` method is only available on the following Testing Library packages:
4+
>
5+
> - `@testing-library/react` (supported by this plugin)
6+
> - `@testing-library/preact` (not supported yet by this plugin)
7+
> - `@testing-library/svelte` (not supported yet by this plugin)
8+
9+
## Rule Details
10+
11+
This rule aims to avoid the usage of `act` to wrap Testing Library utils just to silence "not wrapped in act(...)" warnings.
12+
13+
All Testing Library utils are already wrapped in `act`. Most of the time, if you're seeing an `act` warning, it's not just something to be silenced, but it's actually telling you that something unexpected is happening in your test.
14+
15+
Additionally, wrapping empty callbacks in `act` is also an incorrect way of silencing "not wrapped in act(...)" warnings.
16+
17+
Code violations reported by this rule will pinpoint those unnecessary `act`, helping to understand when `act` actually is necessary.
18+
19+
Example of **incorrect** code for this rule:
20+
21+
```js
22+
// ❌ wrapping things related to Testing Library in `act` is incorrect
23+
import {
24+
act,
25+
render,
26+
screen,
27+
waitFor,
28+
fireEvent,
29+
} from '@testing-library/react';
30+
import userEvent from '@testing-library/user-event';
31+
32+
// ...
33+
34+
act(() => {
35+
render(<Example />);
36+
});
37+
38+
await act(async () => waitFor(() => {}));
39+
40+
act(() => screen.getByRole('button'));
41+
42+
act(() => {
43+
fireEvent.click(element);
44+
});
45+
46+
act(() => {
47+
userEvent.click(element);
48+
});
49+
```
50+
51+
```js
52+
// ❌ wrapping empty callbacks in `act` is incorrect
53+
import { act } from '@testing-library/react';
54+
import userEvent from '@testing-library/user-event';
55+
56+
// ...
57+
58+
act(() => {});
59+
60+
await act(async () => {});
61+
```
62+
63+
```js
64+
// ❌ wrapping things related to Testing Library in React DOM Test Utils `act` is also incorrect
65+
import { act } from 'react-dom/test-utils';
66+
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
67+
import userEvent from '@testing-library/user-event';
68+
69+
// ...
70+
71+
act(() => {
72+
render(<Example />);
73+
});
74+
75+
await act(async () => waitFor(() => {}));
76+
77+
act(() => screen.getByRole('button'));
78+
79+
act(() => {
80+
fireEvent.click(element);
81+
});
82+
83+
act(() => {
84+
userEvent.click(element);
85+
});
86+
```
87+
88+
Examples of **correct** code for this rule:
89+
90+
```js
91+
// ✅ wrapping things not related to Testing Library in `act` is correct
92+
import { act } from '@testing-library/react';
93+
import { stuffThatDoesNotUseRTL } from 'somwhere-else';
94+
95+
// ...
96+
97+
act(() => {
98+
stuffThatDoesNotUseRTL();
99+
});
100+
```
101+
102+
```js
103+
// ✅ wrapping both things related and not related to Testing Library in `act` is correct
104+
import { act, screen } from '@testing-library/react';
105+
import { stuffThatDoesNotUseRTL } from 'somwhere-else';
106+
107+
await act(async () => {
108+
await screen.findByRole('button');
109+
stuffThatDoesNotUseRTL();
110+
});
111+
```
112+
113+
## Further Reading
114+
115+
- [Inspiration for this rule](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#wrapping-things-in-act-unnecessarily)
116+
- [Fix the "not wrapped in act(...)" warning](https://kentcdodds.com/blog/fix-the-not-wrapped-in-act-warning)
117+
- [About React Testing Library `act`](https://testing-library.com/docs/react-testing-library/api/#act)

lib/rules/no-unnecessary-act.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default createTestingLibraryRule<[], MessageIds>({
1717
type: 'problem',
1818
docs: {
1919
description:
20-
'Disallow the use of `act` when wrapping Testing Library utils or empty functions',
20+
'Disallow wrapping Testing Library utils or empty callbacks in `act`',
2121
category: 'Possible Errors',
2222
recommendedConfig: {
2323
dom: false,

0 commit comments

Comments
 (0)