-
Notifications
You must be signed in to change notification settings - Fork 155
feat: no-get-by-for-checking-element-not-present #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
0c48fec
36c0eec
42abb3a
333e1c8
0d11ebd
585caa1
c976115
31503ad
8f31a55
f39276b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,58 @@ | ||||||||||
| # Disallow the use of `getBy*` queries when checking elements are not present (no-get-by-for-asserting-element-not-present) | ||||||||||
|
|
||||||||||
| 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: | ||||||||||
|
|
||||||||||
| - 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. | ||||||||||
| - 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. | ||||||||||
|
|
||||||||||
| 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: | ||||||||||
|
|
||||||||||
| - assert element does not exist: `expect(queryByText("Foo")).not.toBeInTheDocument()` | ||||||||||
| - wait for disappearance: `await waitForElementToBeRemoved(() => queryByText('the mummy'))` | ||||||||||
|
|
||||||||||
| ## Rule details | ||||||||||
|
|
||||||||||
| This rule fires whenever: | ||||||||||
|
|
||||||||||
| - `expect` is used to assert element does not exist with `.not.toBeInTheDocument()` or `.toBeNull()` matchers | ||||||||||
| - `waitForElementToBeRemoved` async util is used to wait for element to be removed from DOM | ||||||||||
|
|
||||||||||
| Examples of **incorrect** code for this rule: | ||||||||||
|
|
||||||||||
| ```js | ||||||||||
| test('some test', () => { | ||||||||||
| const { getByText } = render(<App />); | ||||||||||
| expect(getByText('Foo')).not.toBeInTheDocument(); | ||||||||||
| expect(getByText('Foo')).toBeFalsy(); | ||||||||||
thomaslombart marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
| }); | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| ```js | ||||||||||
| test('some test', async () => { | ||||||||||
| const utils = render(<App />); | ||||||||||
| await waitForElementToBeRemoved(() => utils.getByText('Foo')).toBeInTheDocument()); | ||||||||||
thomaslombart marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| }); | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| Examples of **correct** code for this rule: | ||||||||||
|
|
||||||||||
| ```js | ||||||||||
| test('some test', () => { | ||||||||||
| const { getByText } = render(<App />); | ||||||||||
| expect(getByText('Foo')).toBeInTheDocument(); | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| }); | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| ```js | ||||||||||
| test('some test', async () => { | ||||||||||
| const utils = render(<App />); | ||||||||||
| await waitForElementToBeRemoved(() => utils.queryByText('Foo')); | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example is fine as it's not asserting inside. |
||||||||||
| }); | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| ## Further Reading | ||||||||||
|
|
||||||||||
| - [Asserting elements are not present](https://testing-library.com/docs/guide-disappearance#asserting-elements-are-not-present) | ||||||||||
| - [Waiting for disappearance](https://testing-library.com/docs/guide-disappearance#waiting-for-disappearance) | ||||||||||
| - [jest-dom note about using `getBy` within assertions](https://testing-library.com/docs/ecosystem-jest-dom) | ||||||||||
| - [Testing Library queries cheatsheet](https://testing-library.com/docs/dom-testing-library/cheatsheet#queries) | ||||||||||
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,78 @@ | ||||||
| 'use strict'; | ||||||
|
|
||||||
| const { getDocsUrl } = require('../utils'); | ||||||
|
|
||||||
| const falsyMatchers = ['toBeNull', 'toBeFalsy']; | ||||||
|
|
||||||
| module.exports = { | ||||||
| meta: { | ||||||
| docs: { | ||||||
| category: 'Best Practices', | ||||||
| description: | ||||||
| 'Disallow the use of `getBy*` queries when checking elements are not present', | ||||||
| recommended: 'error', | ||||||
| url: getDocsUrl('no-get-by-for-asserting-element-not-present'), | ||||||
| }, | ||||||
| messages: { | ||||||
| expectQueryBy: | ||||||
| 'Use `expect(getBy*)` only when elements are present, otherwise use `expect(queryBy*)`.', | ||||||
|
||||||
| 'Use `expect(getBy*)` only when elements are present, otherwise use `expect(queryBy*)`.', | |
| 'Use `getBy*` only when elements are present, otherwise use `queryBy*` when checking that elements are not present.', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is when cheking that elements are not present necessary? I feel like it's too much, what about:
Use getBy* only when checking elements are present, otherwise use queryBy*
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.