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.

Allow destructuring from screen with prefer-screen-queries

See original GitHub issue

It would be great if prefer-screen-queries could allow destructuring from screen, for example:

it("renders foo", () => {
  const { getByText } = screen;

  render(<App />);

  // currently flagged by rule
  expect(getByText("foo")).toBeTruthy();
});

or

const { getByText } = screen;

it("renders foo", () => {
  render(<App />);

  // currently flagged by rule
  expect(getByText("foo")).toBeTruthy();
});

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
Belco90commented, May 25, 2020

Having said that, I’m happy to update the rule description to cover why it won’t allow you to destructure queries from screen.

2reactions
gndeliacommented, May 22, 2020

from what I understand in kent c dodds post about using screen

The benefit of using screen is you no longer need to keep the render call destructure up-to-date as you add/remove the queries you need. You only need to type screen. and let your editor’s magic autocomplete take care of the rest.

as I see it, your second example should be triggered by the linter as “incorrect”, according to the rule.

The only allowed scenarios to query should be screen.{{query}} calls, being the only exception any queries related to within (as it changes the bounding of the queries). There, nothing is said about allowing destructuring or not, so I assume it should be allowed…

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-testing-library - Screen vs Render queries - Stack Overflow
@BradyDowling render returns an object that you can destructure, e.g. const { getByText } = render(<MyComponent />) . If you want to use...
Read more >
React Testing Library best practices | Ben Ilegbodu
The changed best practice is to always use screen object and no longer destructure the object returned by render . And the prefer-screen-queries...
Read more >
prefer-destructuring - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
Using React Testing Library - Sentry Developer Documentation
You won't have to keep the render call destructure up-to-date as you add/remove the queries you need. You only need to type screen...
Read more >
Common mistakes with React Testing Library - Kent C. Dodds
Advice: destructure what you need from render or call it view . ... You only need to type screen. and let your editor's...
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