Skip to content

Commit 93e2a8b

Browse files
authored
Remove prefer-expect-query-by rule from shareable configs (#60)
* fix(prefer-expect-query-by): remove rule from recommended preset * docs(prefer-expect-query-by): improve use cases and motivation * docs(prefer-expect-query-by): get examples without negation back
1 parent 3956299 commit 93e2a8b

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

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

+38-18
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# Disallow the use of `expect(getBy*)` (prefer-expect-query-by)
22

3-
The (DOM) Testing Library allows to query DOM elements using different types of queries such as `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 e.g. `expect(queryByText("Foo")).not.toBeInTheDocument()`.
3+
The (DOM) Testing Library allows to query DOM elements using different types of queries such as `getBy*` and `queryBy*`. Using `getBy*` throws an error in case the element is not found. This is useful when:
54

6-
> The same applies for the `getAll*` and `queryAll*` queries.
5+
- 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.
6+
- using `getBy` queries as an assert itself, so if the element is not found the error thrown will work as the check itself within the test.
7+
8+
However, when trying to assert if an element is not present or disappearance, 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 e.g. `expect(queryByText("Foo")).not.toBeInTheDocument()`.
9+
10+
> The same applies for the `getAll*` and `queryAll*` queries too.
711
812
## Rule details
913

1014
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.
1115

12-
This rule is enabled by default.
13-
1416
Examples of **incorrect** code for this rule:
1517

1618
```js
@@ -24,12 +26,20 @@ test('some test', () => {
2426
```
2527

2628
```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();
29+
test('some test', async () => {
30+
const utils = render(<App />);
31+
await wait(() => {
32+
expect(utils.getByText('Foo')).toBeInTheDocument();
33+
});
34+
await wait(() => {
35+
expect(utils.getAllByText('Foo')).toBeInTheDocument();
36+
});
37+
await wait(() => {
38+
expect(utils.getByText('Foo')).not.toBeInTheDocument();
39+
});
40+
await wait(() => {
41+
expect(utils.getAllByText('Foo')).not.toBeInTheDocument();
42+
});
3343
});
3444
```
3545

@@ -46,16 +56,26 @@ test('some test', () => {
4656
```
4757

4858
```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();
59+
test('some test', async () => {
60+
const utils = render(<App />);
61+
await wait(() => {
62+
expect(utils.queryByText('Foo')).toBeInTheDocument();
63+
});
64+
await wait(() => {
65+
expect(utils.queryAllByText('Foo')).toBeInTheDocument();
66+
});
67+
await wait(() => {
68+
expect(utils.queryByText('Foo')).not.toBeInTheDocument();
69+
});
70+
await wait(() => {
71+
expect(utils.queryAllByText('Foo')).not.toBeInTheDocument();
72+
});
5573
});
5674
```
5775

5876
## Further Reading
5977

60-
- [Appearance and Disappearance](https://testing-library.com/docs/guide-disappearance#asserting-elements-are-not-present)
78+
- [Asserting elements are not present](https://testing-library.com/docs/guide-disappearance#asserting-elements-are-not-present)
79+
- [Waiting for disappearance](https://testing-library.com/docs/guide-disappearance#waiting-for-disappearance)
80+
- [jest-dom note about using `getBy` within assertions](https://testing-library.com/docs/ecosystem-jest-dom)
6181
- [Testing Library queries cheatsheet](https://testing-library.com/docs/dom-testing-library/cheatsheet#queries)

lib/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const rules = {
1414
const recommendedRules = {
1515
'testing-library/await-async-query': 'error',
1616
'testing-library/no-await-sync-query': 'error',
17-
'testing-library/prefer-expect-query-by': 'error',
1817
};
1918

2019
module.exports = {

tests/__snapshots__/index.test.js.snap

-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Object {
1313
"error",
1414
"angular",
1515
],
16-
"testing-library/prefer-expect-query-by": "error",
1716
},
1817
}
1918
`;
@@ -31,7 +30,6 @@ Object {
3130
"error",
3231
"react",
3332
],
34-
"testing-library/prefer-expect-query-by": "error",
3533
},
3634
}
3735
`;
@@ -44,7 +42,6 @@ Object {
4442
"rules": Object {
4543
"testing-library/await-async-query": "error",
4644
"testing-library/no-await-sync-query": "error",
47-
"testing-library/prefer-expect-query-by": "error",
4845
},
4946
}
5047
`;
@@ -63,7 +60,6 @@ Object {
6360
"error",
6461
"vue",
6562
],
66-
"testing-library/prefer-expect-query-by": "error",
6763
},
6864
}
6965
`;

0 commit comments

Comments
 (0)