Skip to content

Commit a7ee93c

Browse files
EricKim987SevenOutmanDomaG-Rath
authored
fix(prefer-in-document): false positive on .toHaveLength(1) matcher with *AllBy* query (#311)
Co-authored-by: Doma <[email protected]> Co-authored-by: Doma <[email protected]> Co-authored-by: Gareth Jones <[email protected]>
1 parent c7ccdea commit a7ee93c

File tree

4 files changed

+246
-161
lines changed

4 files changed

+246
-161
lines changed

README.md

+16-15
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,22 @@ module.exports = {
9696

9797
💼 Configurations enabled in.\
9898
✅ Set in the `recommended` configuration.\
99-
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).
100-
101-
| Name                        | Description | 💼 | 🔧 |
102-
| :----------------------------------------------------------------------- | :-------------------------------------------------------------------- | :- | :- |
103-
| [prefer-checked](docs/rules/prefer-checked.md) | prefer toBeChecked over checking attributes || 🔧 |
104-
| [prefer-empty](docs/rules/prefer-empty.md) | Prefer toBeEmpty over checking innerHTML || 🔧 |
105-
| [prefer-enabled-disabled](docs/rules/prefer-enabled-disabled.md) | prefer toBeDisabled or toBeEnabled over checking attributes || 🔧 |
106-
| [prefer-focus](docs/rules/prefer-focus.md) | prefer toHaveFocus over checking document.activeElement || 🔧 |
107-
| [prefer-in-document](docs/rules/prefer-in-document.md) | Prefer .toBeInTheDocument() for asserting the existence of a DOM node || 🔧 |
108-
| [prefer-required](docs/rules/prefer-required.md) | prefer toBeRequired over checking properties || 🔧 |
109-
| [prefer-to-have-attribute](docs/rules/prefer-to-have-attribute.md) | prefer toHaveAttribute over checking getAttribute/hasAttribute || 🔧 |
110-
| [prefer-to-have-class](docs/rules/prefer-to-have-class.md) | prefer toHaveClass over checking element className || 🔧 |
111-
| [prefer-to-have-style](docs/rules/prefer-to-have-style.md) | prefer toHaveStyle over checking element style || 🔧 |
112-
| [prefer-to-have-text-content](docs/rules/prefer-to-have-text-content.md) | Prefer toHaveTextContent over checking element.textContent || 🔧 |
113-
| [prefer-to-have-value](docs/rules/prefer-to-have-value.md) | prefer toHaveValue over checking element.value || 🔧 |
99+
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
100+
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
101+
102+
| Name                        | Description | 💼 | 🔧 | 💡 |
103+
| :----------------------------------------------------------------------- | :-------------------------------------------------------------------- | :- | :- | :- |
104+
| [prefer-checked](docs/rules/prefer-checked.md) | prefer toBeChecked over checking attributes || 🔧 | |
105+
| [prefer-empty](docs/rules/prefer-empty.md) | Prefer toBeEmpty over checking innerHTML || 🔧 | |
106+
| [prefer-enabled-disabled](docs/rules/prefer-enabled-disabled.md) | prefer toBeDisabled or toBeEnabled over checking attributes || 🔧 | |
107+
| [prefer-focus](docs/rules/prefer-focus.md) | prefer toHaveFocus over checking document.activeElement || 🔧 | |
108+
| [prefer-in-document](docs/rules/prefer-in-document.md) | Prefer .toBeInTheDocument() for asserting the existence of a DOM node || 🔧 | 💡 |
109+
| [prefer-required](docs/rules/prefer-required.md) | prefer toBeRequired over checking properties || 🔧 | |
110+
| [prefer-to-have-attribute](docs/rules/prefer-to-have-attribute.md) | prefer toHaveAttribute over checking getAttribute/hasAttribute || 🔧 | |
111+
| [prefer-to-have-class](docs/rules/prefer-to-have-class.md) | prefer toHaveClass over checking element className || 🔧 | |
112+
| [prefer-to-have-style](docs/rules/prefer-to-have-style.md) | prefer toHaveStyle over checking element style || 🔧 | |
113+
| [prefer-to-have-text-content](docs/rules/prefer-to-have-text-content.md) | Prefer toHaveTextContent over checking element.textContent || 🔧 | |
114+
| [prefer-to-have-value](docs/rules/prefer-to-have-value.md) | prefer toHaveValue over checking element.value || 🔧 | |
114115

115116
<!-- end auto-generated rules list -->
116117

docs/rules/prefer-in-document.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
💼 This rule is enabled in the ✅ `recommended` config.
44

5-
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
5+
🔧💡 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix) and manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
66

77
<!-- end auto-generated rule header -->
88

@@ -11,6 +11,7 @@
1111
This rule enforces checking existance of DOM nodes using `.toBeInTheDocument()`.
1212
The rule prefers that matcher over various existance checks such as `.toHaveLength(1)`, `.not.toBeNull()` and
1313
similar.
14+
However it's considered OK to use `.toHaveLength(value)` matcher with `*AllBy*` queries.
1415

1516
Examples of **incorrect** code for this rule:
1617

@@ -46,7 +47,7 @@ expect(screen.getByText("foo").length).toBe(1);
4647
expect(screen.queryByText("foo")).toBeInTheDocument();
4748
expect(await screen.findByText("foo")).toBeInTheDocument();
4849
expect(queryByText("foo")).toBeInTheDocument();
49-
expect(wrapper.queryAllByTestId("foo")).toBeInTheDocument();
50+
expect(wrapper.queryAllByTestId("foo")).toHaveLength(1);
5051
expect(screen.getAllByLabel("foo-bar")).toHaveLength(2);
5152
expect(notAQuery("foo-bar")).toHaveLength(1);
5253

0 commit comments

Comments
 (0)