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
# Use `find*` query methods to wait for elements instead of waitFor (prefer-find-by)
2
+
3
+
findBy* queries are a simple combination of getBy* queries and waitFor. The findBy\* queries accept the waitFor options as the last argument. (i.e. screen.findByText('text', queryOptions, waitForOptions))
4
+
5
+
## Rule details
6
+
7
+
This rule aims to use `findBy*` or `findAllBy*` queries to wait for elements, rather than using `waitFor`, or the deprecated methods `waitForElement` and `wait`.
8
+
This rules analyzes those cases where `waitFor` is used with just one query method, in the form of an arrow function with only one statement (that is, without a block of statements). Given the callback could be more complex, this rule does not consider function callbacks or arrow functions with blocks of code
9
+
10
+
Examples of **incorrect** code for this rule
11
+
12
+
```js
13
+
// arrow functions with one statement, using screen and any sync query method
14
+
constsubmitButton=awaitwaitFor(() =>
15
+
screen.getByRole('button', { name:/submit/i })
16
+
);
17
+
constsubmitButton=awaitwaitFor(() =>
18
+
screen.getAllTestId('button', { name:/submit/i })
19
+
);
20
+
21
+
// arrow functions with one statement, calling any sync query method
// using waitFor with an arrow function with a code block
57
+
awaitwaitFor(() => {
58
+
baz();
59
+
returnqueryAllByText('foo');
60
+
});
61
+
62
+
// using a custom arrow function
63
+
awaitwaitFor(() =>myCustomFunction());
64
+
```
65
+
66
+
## When Not To Use It
67
+
68
+
- Not encouraging use of findBy shortcut from testing library best practices
69
+
70
+
## Further Reading
71
+
72
+
- Documentation for [findBy\* queries](https://testing-library.com/docs/dom-testing-library/api-queries#findby)
73
+
74
+
- Common mistakes with RTL, by Kent C. Dodds: [Using waitFor to wait for elements that can be queried with find\*](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#using-waitfor-to-wait-for-elements-that-can-be-queried-with-find)
0 commit comments