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.

Shouldn't prefer-screen-queries takes into account querySelector?

See original GitHub issue

prefer-screen-queries is not taken into account that the user can do this:

const utils = render(<Component />);
utils.container.querySelector('div');

This kind of selector can lead the user to a bad pattern of tests according to the testing-library.

I don’t understand eslint under the hood, but I propose to change the prefer-screen-queries to take the snippet above into account as a bad pattern.

What do you guys think about that?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
lourencicommented, Jun 5, 2020

Yes, there is a bug. I made some mistake earlier trying to reproduce this, I can reproduce now.

I’ll try to solve this.

2reactions
gndeliacommented, Jun 5, 2020

Hey, @lourenci , I’m reopening your issue.

despite the discussion about whether we should enforce or not using the querySelector and all that, this code in your first comment

const utils = render(<Component />);
utils.container.querySelector('div');

is an invalid scenario.

This rule (prefer-screen-queries) should detect that screen is not used and enforce it to fail, as the correct scenario should be

render(<Component />);
screen.container.querySelector('div');

Then, another rule should probably enforce whether to use or not container.querySelector - but this probably is a bug of prefer-screen-queries

Good find btw 😃

If you like, feel free to take a look at this bug and open a PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Document.querySelector() - Web APIs | MDN
An Element object representing the first element in the document that matches the specified set of CSS selectors, or null is returned if...
Read more >
javascript - Why can't I use querySelector on a parentElement ...
No. from docs: The querySelector() method of the Element interface returns the first element that is a descendant of the element on which...
Read more >
I'm trying to use document.querySelector by it doesn't seem to ...
Hi, the better option here is to use toe getElementById() method. The reason for this is that specific id's should be used only...
Read more >
Using querySelector and querySelectorAll With Modern ...
querySelector () accepts a string which it uses as a CSS selector for selecting elements in the DOM. The first element that matches...
Read more >
How to use CSS Selector - QuerySelector, QuerySelectorAll
In the following example we use CSS Element Selector to find the first ... In the following C# example, QuerySelector() method takes as...
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