|
1 |
| -# Disallow the use of `expect(getBy*)` (prefer-expect-query-by) |
| 1 | +# Disallow the use of `expect(getBy*)` when elements may be asbent (no-get-by-for-absent-elements) |
2 | 2 |
|
3 | 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:
|
4 | 4 |
|
5 | 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 | 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 | 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()`. |
| 8 | +However, when trying to assert if an element is not present or disappearance, using `getBy*` will make the test 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 | 9 |
|
10 | 10 | > The same applies for the `getAll*` and `queryAll*` queries too.
|
11 | 11 |
|
12 | 12 | ## Rule details
|
13 | 13 |
|
14 |
| -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. |
| 14 | +This rule gives a notification whenever `expect` is used with one of the query functions that throws an error if the element is not found. |
15 | 15 |
|
16 | 16 | Examples of **incorrect** code for this rule:
|
17 | 17 |
|
18 | 18 | ```js
|
19 | 19 | test('some test', () => {
|
20 | 20 | const { getByText, getAllByText } = render(<App />);
|
21 |
| - expect(getByText('Foo')).toBeInTheDocument(); |
22 |
| - expect(getAllByText('Foo')[0]).toBeInTheDocument(); |
23 | 21 | expect(getByText('Foo')).not.toBeInTheDocument();
|
24 |
| - expect(getAllByText('Foo')[0]).not.toBeInTheDocument(); |
| 22 | + expect(getAllByText('Foo')[0]).toBeNull(); |
| 23 | + expect(getByText('Foo')).toBeFalsy(); |
| 24 | + expect(getAllByText('Foo')[0]).not.toBeTruthy(); |
| 25 | + expect(getByText('Foo')).toBeUndefined(); |
25 | 26 | });
|
26 | 27 | ```
|
27 | 28 |
|
28 | 29 | ```js
|
29 | 30 | test('some test', async () => {
|
30 | 31 | const utils = render(<App />);
|
31 |
| - await wait(() => { |
| 32 | + await waitForElementToBeRemoved(() => { |
32 | 33 | expect(utils.getByText('Foo')).toBeInTheDocument();
|
33 | 34 | });
|
34 |
| - await wait(() => { |
| 35 | + await waitForElementToBeRemoved(() => { |
35 | 36 | expect(utils.getAllByText('Foo')).toBeInTheDocument();
|
36 | 37 | });
|
37 |
| - await wait(() => { |
38 |
| - expect(utils.getByText('Foo')).not.toBeInTheDocument(); |
39 |
| - }); |
40 |
| - await wait(() => { |
41 |
| - expect(utils.getAllByText('Foo')).not.toBeInTheDocument(); |
42 |
| - }); |
43 | 38 | });
|
44 | 39 | ```
|
45 | 40 |
|
46 | 41 | Examples of **correct** code for this rule:
|
47 | 42 |
|
48 | 43 | ```js
|
49 | 44 | test('some test', () => {
|
50 |
| - const { queryByText, queryAllByText } = render(<App />); |
51 |
| - expect(queryByText('Foo')).toBeInTheDocument(); |
52 |
| - expect(queryAllByText('Foo')[0]).toBeInTheDocument(); |
53 |
| - expect(queryByText('Foo')).not.toBeInTheDocument(); |
54 |
| - expect(queryAllByText('Foo')[0]).not.toBeInTheDocument(); |
| 45 | + const { getByText, getAllByText } = render(<App />); |
| 46 | + expect(getByText('Foo')).toBeInTheDocument(); |
| 47 | + expect(getAllByText('Foo')).toBeTruthy(); |
55 | 48 | });
|
56 | 49 | ```
|
57 | 50 |
|
58 | 51 | ```js
|
59 | 52 | test('some test', async () => {
|
60 | 53 | const utils = render(<App />);
|
61 |
| - await wait(() => { |
| 54 | + await waitForElementToBeRemoved(() => { |
62 | 55 | expect(utils.queryByText('Foo')).toBeInTheDocument();
|
63 | 56 | });
|
64 |
| - await wait(() => { |
| 57 | + await waitForElementToBeRemoved(() => { |
65 | 58 | expect(utils.queryAllByText('Foo')).toBeInTheDocument();
|
66 | 59 | });
|
67 |
| - await wait(() => { |
68 |
| - expect(utils.queryByText('Foo')).not.toBeInTheDocument(); |
69 |
| - }); |
70 |
| - await wait(() => { |
71 |
| - expect(utils.queryAllByText('Foo')).not.toBeInTheDocument(); |
72 |
| - }); |
73 | 60 | });
|
74 | 61 | ```
|
75 | 62 |
|
|
0 commit comments