|
| 1 | +# Ensure the configured `get*`/`query*` query is used with the corresponding matchers (`testing-library/prefer-query-matchers`) |
| 2 | + |
| 3 | +<!-- end auto-generated rule header --> |
| 4 | + |
| 5 | +The (DOM) Testing Library allows to query DOM elements using different types of queries such as `get*` and `query*`. Using `get*` throws an error in case the element is not found, while `query*` returns null instead of throwing (or empty array for `queryAllBy*` ones). |
| 6 | + |
| 7 | +It may be helpful to ensure that either `get*` or `query*` are always used for a given matcher. For example, `.toBeVisible()` and the negation `.not.toBeVisible()` both assume that an element exists in the DOM and will error if not. Using `get*` with `.toBeVisible()` ensures that if the element is not found the error thrown will offer better info than with `query*`. |
| 8 | + |
| 9 | +## Rule details |
| 10 | + |
| 11 | +This rule must be configured with a list of `validEntries`: for a given matcher, is `get*` or `query*` required. |
| 12 | + |
| 13 | +Assuming the following configuration: |
| 14 | + |
| 15 | +```json |
| 16 | +{ |
| 17 | + "testing-library/prefer-query-matchers": [ |
| 18 | + 2, |
| 19 | + { |
| 20 | + "validEntries": [{ "matcher": "toBeVisible", "query": "get" }] |
| 21 | + } |
| 22 | + ] |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +Examples of **incorrect** code for this rule with the above configuration: |
| 27 | + |
| 28 | +```js |
| 29 | +test('some test', () => { |
| 30 | + render(<App />); |
| 31 | + |
| 32 | + // use configured matcher with the disallowed `query*` |
| 33 | + expect(screen.queryByText('button')).toBeVisible(); |
| 34 | + expect(screen.queryByText('button')).not.toBeVisible(); |
| 35 | + expect(screen.queryAllByText('button')[0]).toBeVisible(); |
| 36 | + expect(screen.queryAllByText('button')[0]).not.toBeVisible(); |
| 37 | +}); |
| 38 | +``` |
| 39 | + |
| 40 | +Examples of **correct** code for this rule: |
| 41 | + |
| 42 | +```js |
| 43 | +test('some test', async () => { |
| 44 | + render(<App />); |
| 45 | + // use configured matcher with the allowed `get*` |
| 46 | + expect(screen.getByText('button')).toBeVisible(); |
| 47 | + expect(screen.getByText('button')).not.toBeVisible(); |
| 48 | + expect(screen.getAllByText('button')[0]).toBeVisible(); |
| 49 | + expect(screen.getAllByText('button')[0]).not.toBeVisible(); |
| 50 | + |
| 51 | + // use an unconfigured matcher with either `get* or `query* |
| 52 | + expect(screen.getByText('button')).toBeEnabled(); |
| 53 | + expect(screen.getAllByText('checkbox')[0]).not.toBeChecked(); |
| 54 | + expect(screen.queryByText('button')).toHaveFocus(); |
| 55 | + expect(screen.queryAllByText('button')[0]).not.toMatchMyCustomMatcher(); |
| 56 | + |
| 57 | + // `findBy*` queries are out of the scope for this rule |
| 58 | + const button = await screen.findByText('submit'); |
| 59 | + expect(button).toBeVisible(); |
| 60 | +}); |
| 61 | +``` |
| 62 | + |
| 63 | +## Options |
| 64 | + |
| 65 | +| Option | Required | Default | Details | |
| 66 | +| -------------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | |
| 67 | +| `validEntries` | No | `[]` | A list of objects with a `matcher` property (the name of any matcher, such as "toBeVisible") and a `query` property (either "get" or "query"). Indicates whether `get*` or `query*` are allowed with this matcher. | |
| 68 | + |
| 69 | +## Example |
| 70 | + |
| 71 | +```json |
| 72 | +{ |
| 73 | + "testing-library/prefer-query-matchers": [ |
| 74 | + 2, |
| 75 | + { |
| 76 | + "validEntries": [{ "matcher": "toBeVisible", "query": "get" }] |
| 77 | + } |
| 78 | + ] |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +## Further Reading |
| 83 | + |
| 84 | +- [Testing Library queries cheatsheet](https://testing-library.com/docs/dom-testing-library/cheatsheet#queries) |
| 85 | +- [jest-dom note about using `getBy` within assertions](https://testing-library.com/docs/ecosystem-jest-dom) |
0 commit comments