Skip to content

Commit

Permalink
fix(prefer-in-document): false positive on .toHaveLength(1) matcher w…
Browse files Browse the repository at this point in the history
…ith *AllBy* query (#311)

Co-authored-by: Doma <leishenghao@126.com>
Co-authored-by: Doma <shenghao.lei@mail.hypers.com>
Co-authored-by: Gareth Jones <jones258@gmail.com>
  • Loading branch information
4 people committed Aug 5, 2023
1 parent c7ccdea commit a7ee93c
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 161 deletions.
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,22 @@ module.exports = {

💼 Configurations enabled in.\
✅ Set in the `recommended` configuration.\
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).

| Name                        | Description | 💼 | 🔧 |
| :----------------------------------------------------------------------- | :-------------------------------------------------------------------- | :- | :- |
| [prefer-checked](docs/rules/prefer-checked.md) | prefer toBeChecked over checking attributes || 🔧 |
| [prefer-empty](docs/rules/prefer-empty.md) | Prefer toBeEmpty over checking innerHTML || 🔧 |
| [prefer-enabled-disabled](docs/rules/prefer-enabled-disabled.md) | prefer toBeDisabled or toBeEnabled over checking attributes || 🔧 |
| [prefer-focus](docs/rules/prefer-focus.md) | prefer toHaveFocus over checking document.activeElement || 🔧 |
| [prefer-in-document](docs/rules/prefer-in-document.md) | Prefer .toBeInTheDocument() for asserting the existence of a DOM node || 🔧 |
| [prefer-required](docs/rules/prefer-required.md) | prefer toBeRequired over checking properties || 🔧 |
| [prefer-to-have-attribute](docs/rules/prefer-to-have-attribute.md) | prefer toHaveAttribute over checking getAttribute/hasAttribute || 🔧 |
| [prefer-to-have-class](docs/rules/prefer-to-have-class.md) | prefer toHaveClass over checking element className || 🔧 |
| [prefer-to-have-style](docs/rules/prefer-to-have-style.md) | prefer toHaveStyle over checking element style || 🔧 |
| [prefer-to-have-text-content](docs/rules/prefer-to-have-text-content.md) | Prefer toHaveTextContent over checking element.textContent || 🔧 |
| [prefer-to-have-value](docs/rules/prefer-to-have-value.md) | prefer toHaveValue over checking element.value || 🔧 |
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).

| Name                        | Description | 💼 | 🔧 | 💡 |
| :----------------------------------------------------------------------- | :-------------------------------------------------------------------- | :- | :- | :- |
| [prefer-checked](docs/rules/prefer-checked.md) | prefer toBeChecked over checking attributes || 🔧 | |
| [prefer-empty](docs/rules/prefer-empty.md) | Prefer toBeEmpty over checking innerHTML || 🔧 | |
| [prefer-enabled-disabled](docs/rules/prefer-enabled-disabled.md) | prefer toBeDisabled or toBeEnabled over checking attributes || 🔧 | |
| [prefer-focus](docs/rules/prefer-focus.md) | prefer toHaveFocus over checking document.activeElement || 🔧 | |
| [prefer-in-document](docs/rules/prefer-in-document.md) | Prefer .toBeInTheDocument() for asserting the existence of a DOM node || 🔧 | 💡 |
| [prefer-required](docs/rules/prefer-required.md) | prefer toBeRequired over checking properties || 🔧 | |
| [prefer-to-have-attribute](docs/rules/prefer-to-have-attribute.md) | prefer toHaveAttribute over checking getAttribute/hasAttribute || 🔧 | |
| [prefer-to-have-class](docs/rules/prefer-to-have-class.md) | prefer toHaveClass over checking element className || 🔧 | |
| [prefer-to-have-style](docs/rules/prefer-to-have-style.md) | prefer toHaveStyle over checking element style || 🔧 | |
| [prefer-to-have-text-content](docs/rules/prefer-to-have-text-content.md) | Prefer toHaveTextContent over checking element.textContent || 🔧 | |
| [prefer-to-have-value](docs/rules/prefer-to-have-value.md) | prefer toHaveValue over checking element.value || 🔧 | |

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

Expand Down
5 changes: 3 additions & 2 deletions docs/rules/prefer-in-document.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
🔧💡 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).

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

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

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

Expand Down Expand Up @@ -46,7 +47,7 @@ expect(screen.getByText("foo").length).toBe(1);
expect(screen.queryByText("foo")).toBeInTheDocument();
expect(await screen.findByText("foo")).toBeInTheDocument();
expect(queryByText("foo")).toBeInTheDocument();
expect(wrapper.queryAllByTestId("foo")).toBeInTheDocument();
expect(wrapper.queryAllByTestId("foo")).toHaveLength(1);
expect(screen.getAllByLabel("foo-bar")).toHaveLength(2);
expect(notAQuery("foo-bar")).toHaveLength(1);

Expand Down

0 comments on commit a7ee93c

Please sign in to comment.