|
| 1 | +# Disallow the use of `expect(getBy*)` (prefer-expect-query-by) |
| 2 | + |
| 3 | +The (DOM) Testing Library support three 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 e.g. `expect(queryByText("Foo")).not.toBeInTheDocument()`. |
| 5 | + |
| 6 | +> The same applies for the `getAll*` and `queryAll*` queries. |
| 7 | +
|
| 8 | +## Rule details |
| 9 | + |
| 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. |
| 11 | + |
| 12 | +This rule is enabled by default. |
| 13 | + |
| 14 | +Examples of **incorrect** code for this rule: |
| 15 | + |
| 16 | +```js |
| 17 | +test('some test', () => { |
| 18 | + const { getByText, getAllByText } = render(<App />); |
| 19 | + expect(getByText('Foo')).toBeInTheDocument(); |
| 20 | + expect(getAllByText('Foo')[0]).toBeInTheDocument(); |
| 21 | + 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(); |
| 33 | +}); |
| 34 | +``` |
| 35 | + |
| 36 | +Examples of **correct** code for this rule: |
| 37 | + |
| 38 | +```js |
| 39 | +test('some test', () => { |
| 40 | + const { queryByText, queryAllByText } = render(<App />); |
| 41 | + expect(queryByText('Foo')).toBeInTheDocument(); |
| 42 | + expect(queryAllByText('Foo')[0]).toBeInTheDocument(); |
| 43 | + 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(); |
| 55 | +}); |
| 56 | +``` |
| 57 | + |
| 58 | +## Further Reading |
| 59 | + |
| 60 | +- [Appearance and Disappearance](https://testing-library.com/docs/guide-disappearance#asserting-elements-are-not-present) |
| 61 | +- [Testing Library queries cheatsheet](https://testing-library.com/docs/dom-testing-library/cheatsheet#queries) |
0 commit comments