Skip to content

Commit 093b44b

Browse files
committed
refactor: improve docs and tests, include also findBy
1 parent 353cc77 commit 093b44b

File tree

4 files changed

+98
-11
lines changed

4 files changed

+98
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ To enable this configuration use the `extends` property in your
135135
| [no-await-sync-query](docs/rules/no-await-sync-query.md) | Disallow unnecessary `await` for sync queries | ![recommended-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | |
136136
| [no-debug](docs/rules/no-debug.md) | Disallow the use of `debug` | ![angular-badge][] ![react-badge][] ![vue-badge][] | |
137137
| [no-dom-import](docs/rules/no-dom-import.md) | Disallow importing from DOM Testing Library | ![angular-badge][] ![react-badge][] ![vue-badge][] | ![fixable-badge][] |
138-
| [prefer-expect-query-by](docs/rules/prefer-expect-query-by.md) | Disallow the use of `expect(getBy*)` | ![angular-badge][] ![react-badge][] ![vue-badge][] | ![fixable-badge][] |
138+
| [prefer-expect-query-by](docs/rules/prefer-expect-query-by.md) | Disallow the use of `expect(getBy*)` | ![recommended-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | ![fixable-badge][] |
139139

140140
[build-badge]: https://img.shields.io/travis/Belco90/eslint-plugin-testing-library?style=flat-square
141141
[build-url]: https://travis-ci.org/belco90/eslint-plugin-testing-library

docs/rules/prefer-expect-query-by.md

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,57 @@
11
# Disallow the use of `expect(getBy*)` (prefer-expect-query-by)
22

3-
The (DOM) Testing Library support two types of queries: `getBy*` and `queryBy*`. Using `getBy*` throws an error in case the element is not found. This is useful when using method like `waitForElement`, which are `async` functions that will wait for the element to be found until a certain timeout, after that the test will fail.
4-
However, when trying to assert if an element is not in the document, we can't use `getBy*` as the test will fail immediately. Instead it is recommended to use `queryBy*`, which does not throw and therefore we can assert that `expect(queryByText("Foo")).not.toBeInTheDocument()`.
3+
The (DOM) Testing Library support three types of queries: `getBy*`, `findBy*` and `queryBy*`. Using `getBy*` or `findBy*` throws an error in case the element is not found. This is useful when using method like `waitForElement`, which are `async` functions that will wait for the element to be found until a certain timeout, after that the test will fail.
4+
However, when trying to assert if an element is not in the document, we can't use `getBy*` or `findBy*` as the test will fail immediately. Instead it is recommended to use `queryBy*`, which does not throw and therefore we can assert that e.g. `expect(queryByText("Foo")).not.toBeInTheDocument()`.
5+
6+
> The same applies for the `getAll*`, `findAll*` and `queryAll*` queries.
57
68
## Rule details
79

8-
This rule gives a notification whenever `expect(getBy*)` is used.
10+
This rule gives a notification whenever `expect` is used with one of the query functions that throw an error if the element is not found.
911

1012
This rule is enabled by default.
1113

12-
### Default configuration
13-
14-
The following patterns is considered erroneous:
14+
Examples of **incorrect** code for this rule:
1515

1616
```js
1717
test('some test', () => {
18+
const { getByText, getAllByText } = render(<App />);
19+
expect(getByText('Foo')).toBeInTheDocument();
20+
expect(getAllByText('Foo')[0]).toBeInTheDocument();
1821
expect(getByText('Foo')).not.toBeInTheDocument();
22+
expect(getAllByText('Foo')[0]).not.toBeInTheDocument();
23+
});
24+
```
25+
26+
```js
27+
test('some test', () => {
28+
const rendered = render(<App />);
29+
expect(rendered.getByText('Foo')).toBeInTheDocument();
30+
expect(rendered.getAllByText('Foo')[0]).toBeInTheDocument();
31+
expect(rendered.getByText('Foo')).not.toBeInTheDocument();
32+
expect(rendered.getAllByText('Foo')[0]).not.toBeInTheDocument();
1933
});
2034
```
2135

22-
The following pattern is considered non erroneous:
36+
Examples of **correct** code for this rule:
2337

2438
```js
25-
test('some test', async () => {
39+
test('some test', () => {
40+
const { queryByText, queryAllByText } = render(<App />);
41+
expect(queryByText('Foo')).toBeInTheDocument();
42+
expect(queryAllByText('Foo')[0]).toBeInTheDocument();
2643
expect(queryByText('Foo')).not.toBeInTheDocument();
44+
expect(queryAllByText('Foo')[0]).not.toBeInTheDocument();
45+
});
46+
```
47+
48+
```js
49+
test('some test', () => {
50+
const rendered = render(<App />);
51+
expect(rendered.queryByText('Foo')).toBeInTheDocument();
52+
expect(rendered.queryAllByText('Foo')[0]).toBeInTheDocument();
53+
expect(rendered.queryByText('Foo')).not.toBeInTheDocument();
54+
expect(rendered.queryAllByText('Foo')[0]).not.toBeInTheDocument();
2755
});
2856
```
2957

lib/rules/prefer-expect-query-by.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ function isMemberExpression(node) {
1616
}
1717

1818
function isUsingWrongQueries(node) {
19-
return node.name.startsWith('getBy') || node.name.startsWith('getAllBy');
19+
return (
20+
node.name.startsWith('getBy') ||
21+
node.name.startsWith('getAllBy') ||
22+
node.name.startsWith('findBy') ||
23+
node.name.startsWith('findAllBy')
24+
);
2025
}
2126

2227
function isNotNullOrUndefined(input) {
@@ -89,7 +94,10 @@ module.exports = {
8994
fix(fixer) {
9095
return fixer.replaceText(
9196
nodesGetBy[0],
92-
nodesGetBy[0].name.replace(/^(get(All)?(.*))$/, 'query$2$3')
97+
nodesGetBy[0].name.replace(
98+
/^((get|find)(All)?(.*))$/,
99+
'query$3$4'
100+
)
93101
);
94102
},
95103
});

tests/lib/rules/prefer-expect-query-by.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,74 @@ ruleTester.run('prefer-expect-query-by', rule, {
1515
{
1616
code: "expect(rendered.queryAllByText('Hello')).not.toBeInTheDocument()",
1717
},
18+
{ code: "expect(queryByText('Hello')).toBeInTheDocument()" },
19+
{ code: "expect(rendered.queryByText('Hello')).toBeInTheDocument()" },
20+
{ code: "expect(queryAllByText('Hello')).toBeInTheDocument()" },
21+
{
22+
code: "expect(rendered.queryAllByText('Hello')).toBeInTheDocument()",
23+
},
1824
],
1925
invalid: [
2026
{
2127
code: "expect(getByText('Hello')).not.toBeInTheDocument()",
2228
errors: [{ messageId: 'expectQueryBy' }],
29+
output: "expect(queryByText('Hello')).not.toBeInTheDocument()",
2330
},
2431
{
2532
code: "expect(rendered.getByText('Hello')).not.toBeInTheDocument()",
2633
errors: [{ messageId: 'expectQueryBy' }],
34+
output: "expect(rendered.queryByText('Hello')).not.toBeInTheDocument()",
2735
},
2836
{
2937
code: "expect(getAllByText('Hello')).not.toBeInTheDocument()",
3038
errors: [{ messageId: 'expectQueryBy' }],
39+
output: "expect(queryAllByText('Hello')).not.toBeInTheDocument()",
3140
},
3241
{
3342
code: "expect(rendered.getAllByText('Hello')).not.toBeInTheDocument()",
3443
errors: [{ messageId: 'expectQueryBy' }],
44+
output:
45+
"expect(rendered.queryAllByText('Hello')).not.toBeInTheDocument()",
46+
},
47+
{
48+
code: "expect(getByText('Hello')).toBeInTheDocument()",
49+
errors: [{ messageId: 'expectQueryBy' }],
50+
output: "expect(queryByText('Hello')).toBeInTheDocument()",
51+
},
52+
{
53+
code: "expect(rendered.getByText('Hello')).toBeInTheDocument()",
54+
errors: [{ messageId: 'expectQueryBy' }],
55+
output: "expect(rendered.queryByText('Hello')).toBeInTheDocument()",
56+
},
57+
{
58+
code: "expect(getAllByText('Hello')).toBeInTheDocument()",
59+
errors: [{ messageId: 'expectQueryBy' }],
60+
output: "expect(queryAllByText('Hello')).toBeInTheDocument()",
61+
},
62+
{
63+
code: "expect(rendered.getAllByText('Hello')).toBeInTheDocument()",
64+
errors: [{ messageId: 'expectQueryBy' }],
65+
output: "expect(rendered.queryAllByText('Hello')).toBeInTheDocument()",
66+
},
67+
{
68+
code: "expect(findByText('Hello')).toBeInTheDocument()",
69+
errors: [{ messageId: 'expectQueryBy' }],
70+
output: "expect(queryByText('Hello')).toBeInTheDocument()",
71+
},
72+
{
73+
code: "expect(rendered.findByText('Hello')).toBeInTheDocument()",
74+
errors: [{ messageId: 'expectQueryBy' }],
75+
output: "expect(rendered.queryByText('Hello')).toBeInTheDocument()",
76+
},
77+
{
78+
code: "expect(findAllByText('Hello')).toBeInTheDocument()",
79+
errors: [{ messageId: 'expectQueryBy' }],
80+
output: "expect(queryAllByText('Hello')).toBeInTheDocument()",
81+
},
82+
{
83+
code: "expect(rendered.findAllByText('Hello')).toBeInTheDocument()",
84+
errors: [{ messageId: 'expectQueryBy' }],
85+
output: "expect(rendered.queryAllByText('Hello')).toBeInTheDocument()",
3586
},
3687
],
3788
});

0 commit comments

Comments
 (0)