Allow destructuring from screen with prefer-screen-queries
See original GitHub issueIt 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:
- Created 3 years ago
- Comments:14 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Having said that, I’m happy to update the rule description to cover why it won’t allow you to destructure queries from
screen
.from what I understand in kent c dodds post about using screen
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 towithin
(as it changes the bounding of the queries). There, nothing is said about allowing destructuring or not, so I assume it should be allowed…