question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

prefer-in-document: `.toHaveLength(1)` vs `.toBeInTheDocument()`

See original GitHub issue
  • eslint-plugin-jest-dom version: 3.8.1
  • node version: 16.1.0
  • npm version: 7.11.1

Relevant code or config

expect(screen.queryAllByRole('option')).toHaveLength(2);
// some action here
expect(screen.queryAllByRole('option')).toHaveLength(1);

What you did: I want to ensure there is only one option.

What happened:

The rule wants this to be

expect(screen.queryByRole('option')).toBeInTheDocument();

Reproduction repository:

Problem description: It’s not the same thing! I cannot assert that there is only one option now.

Suggested solution: prefer-in-document should allow .toHaveLength(1). Or there should be an option at least.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:7
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
mdotwillscommented, Aug 9, 2021

Based on the discussion in this thread, I’ve put together this truth table of selector type vs assertion style:

Selector type .toBeInTheDocument() .toHaveLength(1) .toHaveLength(0)
(*By*) ✅ correct usage, no change Did you mean to use (*AllBy*)? 🔧 replace with .not.toBeInTheDocument()
(*AllBy*) 🔧 replace with .toHaveLength(1) ✅ correct usage, no change ✅ correct usage, no change

The resolution of the (*By*).toHaveLength(1) suggestion of did you mean to use (*AllBy*) could resolve as

  • Yes, then replace (*By*) with (*AllBy*), leaving expect((*AllBy*)).toHaveLength(1),
  • No, then replace .toHaveLength(1) with .toBeInTheDocument() , leaving expect((*By*)).toBeInTheDocument()

both of which are stable states.

4reactions
mdotwillscommented, Jun 16, 2021

I like your suggestion @nstepien 👍 I’ll have a crack at implementing that

Read more comments on GitHub >

github_iconTop Results From Across the Web

eslint-plugin-jest-dom/prefer-in-document.md at main - GitHub
toBeInTheDocument() . The rule prefers that matcher over various existance checks such as .toHaveLength(1) , .not.toBeNull() and similar.
Read more >
reactjs - Property 'toBeInTheDocument' does not exist on type ...
Was facing a problem with toBeVisible() not being found (TS2339: Property 'toBeVisible' does not exist on type 'JestMatchersShape<Matchers<void, ...
Read more >
Appearance and Disappearance - Testing Library
toBeInTheDocument() matcher, which can be used to assert that an element is in the body of the document, or not. This can be...
Read more >
Guide to testing in React - my software development blog
getBy vs findBy vs queryBy; The act() function ... setCount] = useState(0) const handleIncrement = () => setCount(oldVal => oldVal + 1); ...
Read more >
Common mistakes with React Testing Library - Kent C. Dodds
You're likely missing confidence or will have problematic tests ... toBeDisabled() // error message: // Received element is not disabled: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found