-
Notifications
You must be signed in to change notification settings - Fork 148
False positive for testing-library/prefer-screen-queries #366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hey @julienw. I believe this is pretty much the same idea behind allowing destructuring You can see in that issue we decided not to implement that functionality, since the purpose of using I agree the error doesn't fit really well for this particular case tho, but I'm not sure if it's worth it to report it differently for this scenario. Does it make sense to you? |
I'm a big fan of In this specific case, we wanted to use const popup = screen.getByTestId("modal-dialog");
const element = getByText(popup, "label");
// Do something with element This is an advanced use case, and probably we should really use within instead in this case, but I believe this is out of scope for The alternative is to just add an ignore comment for |
Hi @julienw, thanks for opening the issue!. I agree that this sounds like a use case for within. However, there's something that I might be missing. You mentioned that you wanted to use Shouldn't something like this work too? const element = screen.getByText(popup, "label"); If I understand correctly, the queries don't have any difference in the react package from the In that sense, your code would comply with the rule even if you don't use within (which I would recommend using, as this is its main purpose). |
Exactly. Just like
This doesn't work, BTW I noticed that renaming the query also avoids the rule: import { getByText as globalGetByText } from '@testing-library/react';
globalGetByText(element, "text"); // Doesn't get noticed |
Yes! But TBH I'd be happy as well if you said "OK this works as expected" and I'd just have to use |
I'd say this works as expected. The error message and the rule docs can be improved to clarify this and the destructuring |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I was thinking that another workaround is doing something like import TestingLibrary, { render } from '@testing-library/react';
TestingLibrary.getByText(element, selector); By avoiding the destructuring. |
=>
testing-library/prefer-screen-queries: Avoid destructuring queries from render result, use screen.getByTestId instead
In this case, we use the function imported from
@testing-library/dom
reexported from@testing-library/react
that allows to search for an element in the tree of any other element (likewithin
). This exact example is of course useless (we can usescreen
) but this is to show the problem with a simple testcase. I believe this is a valid use case and shouldn't be flagged bytesting-library/prefer-screen-queries
.What do you think?
The text was updated successfully, but these errors were encountered: