|
| 1 | +# Disallow the use of `expect(getBy*)` (prefer-expect-query-by) |
| 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 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. |
| 11 | +
|
| 12 | +## Rule details |
| 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. |
| 15 | + |
| 16 | +Examples of **incorrect** code for this rule: |
| 17 | + |
| 18 | +```js |
| 19 | +test('some test', () => { |
| 20 | + const { getByText, getAllByText } = render(<App />); |
| 21 | + expect(getByText('Foo')).toBeInTheDocument(); |
| 22 | + expect(getAllByText('Foo')[0]).toBeInTheDocument(); |
| 23 | + expect(getByText('Foo')).not.toBeInTheDocument(); |
| 24 | + expect(getAllByText('Foo')[0]).not.toBeInTheDocument(); |
| 25 | +}); |
| 26 | +``` |
| 27 | + |
| 28 | +```js |
| 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 | + }); |
| 43 | +}); |
| 44 | +``` |
| 45 | + |
| 46 | +Examples of **correct** code for this rule: |
| 47 | + |
| 48 | +```js |
| 49 | +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(); |
| 55 | +}); |
| 56 | +``` |
| 57 | + |
| 58 | +```js |
| 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 | + }); |
| 73 | +}); |
| 74 | +``` |
| 75 | + |
| 76 | +## Further Reading |
| 77 | + |
| 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) |
| 81 | +- [Testing Library queries cheatsheet](https://testing-library.com/docs/dom-testing-library/cheatsheet#queries) |
0 commit comments