|
| 1 | +# Disallow the use of `getBy*` queries when checking elements are not present (no-get-by-for-checking-element-not-present) |
| 2 | + |
| 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 | + |
| 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 asserting if an element is not present or waiting for disappearance, using `getBy*` will make the test fail immediately. Instead it is recommended to use `queryBy*`, which does not throw and therefore we can: |
| 9 | + |
| 10 | +- assert element does not exist: `expect(queryByText("Foo")).not.toBeInTheDocument()` |
| 11 | +- wait for disappearance: `await waitForElementToBeRemoved(() => queryByText('the mummy'))` |
| 12 | + |
| 13 | +## Rule details |
| 14 | + |
| 15 | +This rule fires whenever: |
| 16 | + |
| 17 | +- `expect` is used to assert element does not exist with `.not.toBeInTheDocument()` or `.toBeNull()` matchers |
| 18 | +- `waitForElementToBeRemoved` async util is used to wait for element to be removed from DOM |
| 19 | + |
| 20 | +Examples of **incorrect** code for this rule: |
| 21 | + |
| 22 | +```js |
| 23 | +test('some test', () => { |
| 24 | + const { getByText } = render(<App />); |
| 25 | + expect(getByText('Foo')).not.toBeInTheDocument(); |
| 26 | + expect(getByText('Foo')).toBeFalsy(); |
| 27 | + expect(getByText('Foo')).toBeNull(); |
| 28 | +}); |
| 29 | +``` |
| 30 | + |
| 31 | +```js |
| 32 | +test('some test', async () => { |
| 33 | + const utils = render(<App />); |
| 34 | + await waitForElementToBeRemoved(() => utils.getByText('Foo')); |
| 35 | +}); |
| 36 | +``` |
| 37 | + |
| 38 | +Examples of **correct** code for this rule: |
| 39 | + |
| 40 | +```js |
| 41 | +test('some test', () => { |
| 42 | + const { getByText } = render(<App />); |
| 43 | + expect(getByText('Foo')).toBeInTheDocument(); |
| 44 | + expect(queryByText('Foo')).not.toBeInTheDocument(); |
| 45 | + expect(queryByText('Foo')).toBeFalsy(); |
| 46 | +}); |
| 47 | +``` |
| 48 | + |
| 49 | +```js |
| 50 | +test('some test', async () => { |
| 51 | + const utils = render(<App />); |
| 52 | + await waitForElementToBeRemoved(() => utils.queryByText('Foo')); |
| 53 | +}); |
| 54 | +``` |
| 55 | + |
| 56 | +## Further Reading |
| 57 | + |
| 58 | +- [Asserting elements are not present](https://testing-library.com/docs/guide-disappearance#asserting-elements-are-not-present) |
| 59 | +- [Waiting for disappearance](https://testing-library.com/docs/guide-disappearance#waiting-for-disappearance) |
| 60 | +- [jest-dom note about using `getBy` within assertions](https://testing-library.com/docs/ecosystem-jest-dom) |
| 61 | +- [Testing Library queries cheatsheet](https://testing-library.com/docs/dom-testing-library/cheatsheet#queries) |
0 commit comments