-
Notifications
You must be signed in to change notification settings - Fork 150
feat: new no-unnecessary-act
rule
#365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
314405c
refactor: rename function to detect potential Testing Library func
Belco90 6fc1e2d
feat(no-unnecessary-act): first approach for the rule
Belco90 7fd39ef
feat(no-unnecessary-act): first bunch of tests
Belco90 54ef6b3
test: update number of expected rules
Belco90 4b1d628
feat(no-unnecessary-act): detect error from returns
Belco90 573c689
refactor: extract util to determine if a function is empty
Belco90 9e5442c
feat(no-unnecessary-act): detect error for empty callbacks
Belco90 964209c
refactor(no-unnecessary-act): function to find non-testing-library calls
Belco90 6a3a3b3
refactor(no-unnecessary-act): update docs recommended config
Belco90 9ebe5ae
fix: check if node is coming from Testing Library correctly
Belco90 8882203
feat: save react-dom/test-utils import in detection mechanism
Belco90 d44285c
refactor: extract function for finding import specifier
Belco90 e464ece
refactor(no-unnecessary-act): detect act from React DOM test utils
Belco90 50d5d78
fix: remove duplicated isVariableDeclaration declaration
Belco90 489be04
feat(no-unnecessary-act): detect edge and mixed cases
Belco90 a7de170
docs(no-unnecessary-act): add rule details
Belco90 d6b4247
docs(no-unnecessary-act): include rule in README
Belco90 82e7ad0
refactor: rename findImportedTestingLibraryUtilSpecifier in helpers
Belco90 963e70e
docs(no-unnecessary-act): simplify examples
Belco90 50a72c1
refactor: remove unnecessary comment
Belco90 dc64f8b
fix(no-unnecessary-act): cover more trees searching for statements
Belco90 d07669f
test(no-unnecessary-act): extra cases for thenable promises
Belco90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# Disallow wrapping Testing Library utils or empty callbacks in `act` (`testing-library/no-unnecessary-act`) | ||
|
||
> ⚠️ The `act` method is only available on the following Testing Library packages: | ||
> | ||
> - `@testing-library/react` (supported by this plugin) | ||
> - `@testing-library/preact` (not supported yet by this plugin) | ||
> - `@testing-library/svelte` (not supported yet by this plugin) | ||
gndelia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Rule Details | ||
|
||
This rule aims to avoid the usage of `act` to wrap Testing Library utils just to silence "not wrapped in act(...)" warnings. | ||
|
||
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. | ||
|
||
Additionally, wrapping empty callbacks in `act` is also an incorrect way of silencing "not wrapped in act(...)" warnings. | ||
|
||
Code violations reported by this rule will pinpoint those unnecessary `act`, helping to understand when `act` actually is necessary. | ||
|
||
Example of **incorrect** code for this rule: | ||
|
||
```js | ||
// ❌ wrapping things related to Testing Library in `act` is incorrect | ||
import { | ||
act, | ||
render, | ||
screen, | ||
waitFor, | ||
fireEvent, | ||
} from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
// ... | ||
|
||
act(() => { | ||
render(<Example />); | ||
}); | ||
|
||
await act(async () => waitFor(() => {})); | ||
|
||
act(() => screen.getByRole('button')); | ||
|
||
act(() => { | ||
fireEvent.click(element); | ||
}); | ||
|
||
act(() => { | ||
userEvent.click(element); | ||
}); | ||
``` | ||
|
||
```js | ||
// ❌ wrapping empty callbacks in `act` is incorrect | ||
import { act } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
// ... | ||
|
||
act(() => {}); | ||
|
||
await act(async () => {}); | ||
``` | ||
|
||
```js | ||
// ❌ wrapping things related to Testing Library in React DOM Test Utils `act` is also incorrect | ||
Belco90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { act } from 'react-dom/test-utils'; | ||
import { render, screen, waitFor, fireEvent } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
// ... | ||
|
||
act(() => { | ||
render(<Example />); | ||
}); | ||
|
||
await act(async () => waitFor(() => {})); | ||
|
||
act(() => screen.getByRole('button')); | ||
|
||
act(() => { | ||
fireEvent.click(element); | ||
}); | ||
|
||
act(() => { | ||
userEvent.click(element); | ||
}); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
// ✅ wrapping things not related to Testing Library in `act` is correct | ||
import { act } from '@testing-library/react'; | ||
import { stuffThatDoesNotUseRTL } from 'somwhere-else'; | ||
|
||
// ... | ||
|
||
act(() => { | ||
stuffThatDoesNotUseRTL(); | ||
}); | ||
``` | ||
|
||
```js | ||
// ✅ wrapping both things related and not related to Testing Library in `act` is correct | ||
import { act, screen } from '@testing-library/react'; | ||
import { stuffThatDoesNotUseRTL } from 'somwhere-else'; | ||
|
||
await act(async () => { | ||
await screen.findByRole('button'); | ||
stuffThatDoesNotUseRTL(); | ||
}); | ||
``` | ||
|
||
## Further Reading | ||
|
||
- [Inspiration for this rule](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#wrapping-things-in-act-unnecessarily) | ||
- [Fix the "not wrapped in act(...)" warning](https://kentcdodds.com/blog/fix-the-not-wrapped-in-act-warning) | ||
- [About React Testing Library `act`](https://testing-library.com/docs/react-testing-library/api/#act) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.