Skip to content

Commit bdecec7

Browse files
committed
docs: add no-get-by-for-absent-elements
1 parent d49fdb9 commit bdecec7

File tree

3 files changed

+27
-38
lines changed

3 files changed

+27
-38
lines changed

README.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,17 @@ To enable this configuration use the `extends` property in your
129129

130130
## Supported Rules
131131

132-
| Rule | Description | Configurations | Fixable |
133-
| -------------------------------------------------------------- | ------------------------------------------------------------------- | ------------------------------------------------------------------------- | ------------------ |
134-
| [await-async-query](docs/rules/await-async-query.md) | Enforce async queries to have proper `await` | ![recommended-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | |
135-
| [await-fire-event](docs/rules/await-fire-event.md) | Enforce async fire event methods to be awaited | ![vue-badge][] | |
136-
| [no-await-sync-query](docs/rules/no-await-sync-query.md) | Disallow unnecessary `await` for sync queries | ![recommended-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | |
137-
| [no-debug](docs/rules/no-debug.md) | Disallow the use of `debug` | ![angular-badge][] ![react-badge][] ![vue-badge][] | |
138-
| [no-dom-import](docs/rules/no-dom-import.md) | Disallow importing from DOM Testing Library | ![angular-badge][] ![react-badge][] ![vue-badge][] | ![fixable-badge][] |
139-
| [prefer-expect-query-by](docs/rules/prefer-expect-query-by.md) | Disallow the use of `expect(getBy*)` | ![recommended-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | |
140-
| [prefer-explicit-assert](docs/rules/prefer-explicit-assert.md) | Suggest using explicit assertions rather than just `getBy*` queries | | |
141-
| [consistent-data-testid](docs/rules/consistent-data-testid.md) | Ensure `data-testid` values match a provided regex. | | |
132+
| Rule | Description | Configurations | Fixable |
133+
| ------------------------------------------------------------------------- | ------------------------------------------------------------------- | ------------------------------------------------------------------------- | ------------------ |
134+
| [await-async-query](docs/rules/await-async-query.md) | Enforce async queries to have proper `await` | ![recommended-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | |
135+
| [await-fire-event](docs/rules/await-fire-event.md) | Enforce async fire event methods to be awaited | ![vue-badge][] | |
136+
| [no-await-sync-query](docs/rules/no-await-sync-query.md) | Disallow unnecessary `await` for sync queries | ![recommended-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | |
137+
| [no-debug](docs/rules/no-debug.md) | Disallow the use of `debug` | ![angular-badge][] ![react-badge][] ![vue-badge][] | |
138+
| [no-dom-import](docs/rules/no-dom-import.md) | Disallow importing from DOM Testing Library | ![angular-badge][] ![react-badge][] ![vue-badge][] | ![fixable-badge][] |
139+
| [prefer-expect-query-by](docs/rules/prefer-expect-query-by.md) | Disallow the use of `expect(getBy*)` | ![recommended-badge][] ![angular-badge][] ![react-badge][] ![vue-badge][] | |
140+
| [prefer-explicit-assert](docs/rules/prefer-explicit-assert.md) | Suggest using explicit assertions rather than just `getBy*` queries | | |
141+
| [consistent-data-testid](docs/rules/consistent-data-testid.md) | Ensure `data-testid` values match a provided regex. | | |
142+
| [no-get-by-for-absent-elements](docs/rules/no-get-by-for-absent-elements) | Disallow the use of `expect(getBy*)` when elements may be asbent | | |
142143

143144
[build-badge]: https://img.shields.io/travis/Belco90/eslint-plugin-testing-library?style=flat-square
144145
[build-url]: https://travis-ci.org/belco90/eslint-plugin-testing-library
@@ -185,6 +186,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
185186

186187
<!-- markdownlint-enable -->
187188
<!-- prettier-ignore-end -->
189+
188190
<!-- ALL-CONTRIBUTORS-LIST:END -->
189191

190192
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

docs/rules/no-get-by-for-absent-elements.md

+14-27
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,62 @@
1-
# Disallow the use of `expect(getBy*)` (prefer-expect-query-by)
1+
# Disallow the use of `expect(getBy*)` when elements may be asbent (no-get-by-for-absent-elements)
22

33
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:
44

55
- 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.
66
- 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.
77

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()`.
8+
However, when trying to assert if an element is not present or 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 that e.g. `expect(queryByText("Foo")).not.toBeInTheDocument()`.
99

1010
> The same applies for the `getAll*` and `queryAll*` queries too.
1111
1212
## Rule details
1313

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.
14+
This rule gives a notification whenever `expect` is used with one of the query functions that throws an error if the element is not found.
1515

1616
Examples of **incorrect** code for this rule:
1717

1818
```js
1919
test('some test', () => {
2020
const { getByText, getAllByText } = render(<App />);
21-
expect(getByText('Foo')).toBeInTheDocument();
22-
expect(getAllByText('Foo')[0]).toBeInTheDocument();
2321
expect(getByText('Foo')).not.toBeInTheDocument();
24-
expect(getAllByText('Foo')[0]).not.toBeInTheDocument();
22+
expect(getAllByText('Foo')[0]).toBeNull();
23+
expect(getByText('Foo')).toBeFalsy();
24+
expect(getAllByText('Foo')[0]).not.toBeTruthy();
25+
expect(getByText('Foo')).toBeUndefined();
2526
});
2627
```
2728

2829
```js
2930
test('some test', async () => {
3031
const utils = render(<App />);
31-
await wait(() => {
32+
await waitForElementToBeRemoved(() => {
3233
expect(utils.getByText('Foo')).toBeInTheDocument();
3334
});
34-
await wait(() => {
35+
await waitForElementToBeRemoved(() => {
3536
expect(utils.getAllByText('Foo')).toBeInTheDocument();
3637
});
37-
await wait(() => {
38-
expect(utils.getByText('Foo')).not.toBeInTheDocument();
39-
});
40-
await wait(() => {
41-
expect(utils.getAllByText('Foo')).not.toBeInTheDocument();
42-
});
4338
});
4439
```
4540

4641
Examples of **correct** code for this rule:
4742

4843
```js
4944
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();
45+
const { getByText, getAllByText } = render(<App />);
46+
expect(getByText('Foo')).toBeInTheDocument();
47+
expect(getAllByText('Foo')).toBeTruthy();
5548
});
5649
```
5750

5851
```js
5952
test('some test', async () => {
6053
const utils = render(<App />);
61-
await wait(() => {
54+
await waitForElementToBeRemoved(() => {
6255
expect(utils.queryByText('Foo')).toBeInTheDocument();
6356
});
64-
await wait(() => {
57+
await waitForElementToBeRemoved(() => {
6558
expect(utils.queryAllByText('Foo')).toBeInTheDocument();
6659
});
67-
await wait(() => {
68-
expect(utils.queryByText('Foo')).not.toBeInTheDocument();
69-
});
70-
await wait(() => {
71-
expect(utils.queryAllByText('Foo')).not.toBeInTheDocument();
72-
});
7360
});
7461
```
7562

lib/rules/no-get-by-for-absent-elements.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
docs: {
1010
category: 'Best Practices',
1111
description:
12-
'Disallow using getBy* queries in expect calls when elements may not be present',
12+
'Disallow the use of getBy* queries in expect calls when elements may be absent',
1313
recommended: 'error',
1414
url: getDocsUrl('no-get-by-for-absent-elements'),
1515
},

0 commit comments

Comments
 (0)