Skip to content

Commit 9c512a4

Browse files
committed
docs: add query doc and supported rules table entry
1 parent 9b82c1a commit 9c512a4

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ To enable this configuration use the `extends` property in your
228228
| [prefer-find-by](docs/rules/prefer-find-by.md) | Suggest using `find(All)By*` query instead of `waitFor` + `get(All)By*` to wait for elements | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-vue][] | 🔧 |
229229
| [prefer-presence-queries](docs/rules/prefer-presence-queries.md) | Ensure appropriate `get*`/`query*` queries are used with their respective matchers | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-vue][] | |
230230
| [prefer-query-by-disappearance](docs/rules/prefer-query-by-disappearance.md) | Suggest using `queryBy*` queries when waiting for disappearance | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-vue][] | |
231+
| [prefer-query-matchers](docs/rules/prefer-query-matchers.md) | Ensure the configured `get*`/`query*` query is used with the corresponding matchers | | |
231232
| [prefer-screen-queries](docs/rules/prefer-screen-queries.md) | Suggest using `screen` while querying | ![badge-angular][] ![badge-dom][] ![badge-marko][] ![badge-react][] ![badge-vue][] | |
232233
| [prefer-user-event](docs/rules/prefer-user-event.md) | Suggest using `userEvent` over `fireEvent` for simulating user interactions | | |
233234
| [prefer-wait-for](docs/rules/prefer-wait-for.md) | Use `waitFor` instead of deprecated wait methods | | 🔧 |

docs/rules/prefer-query-matchers.md

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Ensure the configured `get*`/`query*` query is used with the corresponding matchers (`testing-library/prefer-query-matchers`)
2+
3+
<!-- end auto-generated rule header -->
4+
5+
The (DOM) Testing Library allows to query DOM elements using different types of queries such as `get*` and `query*`. Using `get*` throws an error in case the element is not found, while `query*` returns null instead of throwing (or empty array for `queryAllBy*` ones).
6+
7+
It may be helpful to ensure that either `get*` or `query*` are always used for a given matcher. For example, `.toBeVisible()` and the negation `.not.toBeVisible()` both assume that an element exists in the DOM and will error if not. Using `get*` with `.toBeVisible()` ensures that if the element is not found the error thrown will offer better info than with `query*`.
8+
9+
## Rule details
10+
11+
This rule must be configured with a list of `validEntries`: for a given matcher, is `get*` or `query*` required.
12+
13+
Assuming the following configuration:
14+
15+
```json
16+
{
17+
"testing-library/prefer-query-matchers": [
18+
2,
19+
{
20+
"validEntries": [{ "matcher": "toBeVisible", "query": "get" }]
21+
}
22+
]
23+
}
24+
```
25+
26+
Examples of **incorrect** code for this rule with the above configuration:
27+
28+
```js
29+
test('some test', () => {
30+
render(<App />);
31+
32+
// use configured matcher with the disallowed `query*`
33+
expect(screen.queryByText('button')).toBeVisible();
34+
expect(screen.queryByText('button')).not.toBeVisible();
35+
expect(screen.queryAllByText('button')[0]).toBeVisible();
36+
expect(screen.queryAllByText('button')[0]).not.toBeVisible();
37+
});
38+
```
39+
40+
Examples of **correct** code for this rule:
41+
42+
```js
43+
test('some test', async () => {
44+
render(<App />);
45+
// use configured matcher with the allowed `get*`
46+
expect(screen.getByText('button')).toBeVisible();
47+
expect(screen.getByText('button')).not.toBeVisible();
48+
expect(screen.getAllByText('button')[0]).toBeVisible();
49+
expect(screen.getAllByText('button')[0]).not.toBeVisible();
50+
51+
// use an unconfigured matcher with either `get* or `query*
52+
expect(screen.getByText('button')).toBeEnabled();
53+
expect(screen.getAllByText('checkbox')[0]).not.toBeChecked();
54+
expect(screen.queryByText('button')).toHaveFocus();
55+
expect(screen.queryAllByText('button')[0]).not.toMatchMyCustomMatcher();
56+
57+
// `findBy*` queries are out of the scope for this rule
58+
const button = await screen.findByText('submit');
59+
expect(button).toBeVisible();
60+
});
61+
```
62+
63+
## Options
64+
65+
| Option | Required | Default | Details |
66+
| -------------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
67+
| `validEntries` | No | `[]` | A list of objects with a `matcher` property (the name of any matcher, such as "toBeVisible") and a `query` property (either "get" or "query"). Indicates whether `get*` or `query*` are allowed with this matcher. |
68+
69+
## Example
70+
71+
```json
72+
{
73+
"testing-library/prefer-query-matchers": [
74+
2,
75+
{
76+
"validEntries": [{ "matcher": "toBeVisible", "query": "get" }]
77+
}
78+
]
79+
}
80+
```
81+
82+
## Further Reading
83+
84+
- [Testing Library queries cheatsheet](https://testing-library.com/docs/dom-testing-library/cheatsheet#queries)
85+
- [jest-dom note about using `getBy` within assertions](https://testing-library.com/docs/ecosystem-jest-dom)

0 commit comments

Comments
 (0)