|
| 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 | +// ^ act imported from 'react-dom/test-utils' will be reported too |
| 31 | +import userEvent from '@testing-library/user-event'; |
| 32 | + |
| 33 | +// ... |
| 34 | + |
| 35 | +act(() => { |
| 36 | + render(<Example />); |
| 37 | +}); |
| 38 | + |
| 39 | +await act(async () => waitFor(() => {})); |
| 40 | + |
| 41 | +act(() => screen.getByRole('button')); |
| 42 | + |
| 43 | +act(() => { |
| 44 | + fireEvent.click(element); |
| 45 | +}); |
| 46 | + |
| 47 | +act(() => { |
| 48 | + userEvent.click(element); |
| 49 | +}); |
| 50 | +``` |
| 51 | + |
| 52 | +```js |
| 53 | +// ❌ wrapping empty callbacks in `act` is incorrect |
| 54 | +import { act } from '@testing-library/react'; |
| 55 | +// ^ act imported from 'react-dom/test-utils' will be reported too |
| 56 | +import userEvent from '@testing-library/user-event'; |
| 57 | + |
| 58 | +// ... |
| 59 | + |
| 60 | +act(() => {}); |
| 61 | + |
| 62 | +await act(async () => {}); |
| 63 | +``` |
| 64 | + |
| 65 | +Examples of **correct** code for this rule: |
| 66 | + |
| 67 | +```js |
| 68 | +// ✅ wrapping things not related to Testing Library in `act` is correct |
| 69 | +import { act } from '@testing-library/react'; |
| 70 | +import { stuffThatDoesNotUseRTL } from 'somwhere-else'; |
| 71 | + |
| 72 | +// ... |
| 73 | + |
| 74 | +act(() => { |
| 75 | + stuffThatDoesNotUseRTL(); |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +```js |
| 80 | +// ✅ wrapping both things related and not related to Testing Library in `act` is correct |
| 81 | +import { act, screen } from '@testing-library/react'; |
| 82 | +import { stuffThatDoesNotUseRTL } from 'somwhere-else'; |
| 83 | + |
| 84 | +await act(async () => { |
| 85 | + await screen.findByRole('button'); |
| 86 | + stuffThatDoesNotUseRTL(); |
| 87 | +}); |
| 88 | +``` |
| 89 | + |
| 90 | +## Further Reading |
| 91 | + |
| 92 | +- [Inspiration for this rule](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#wrapping-things-in-act-unnecessarily) |
| 93 | +- [Fix the "not wrapped in act(...)" warning](https://kentcdodds.com/blog/fix-the-not-wrapped-in-act-warning) |
| 94 | +- [About React Testing Library `act`](https://testing-library.com/docs/react-testing-library/api/#act) |
0 commit comments