Skip to content

fix(prefer-screen-queries): false positives when using within method #119

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

Conversation

gndelia
Copy link
Collaborator

@gndelia gndelia commented Apr 27, 2020

I am pretty new to writing/fixing ESlint rules, so I might be doing something wrong 😄

This should fix #116 so this code

import { render, within, screen } from 'react-testing-library';

// ...
render(<SomeComponent />);
const section = screen.getByTestId('some-id');

// this is fine but the rule complains about it.
const submit = within(section).getByText(/submit/i);

should now be valid

I think this scenario is still pending

const { getByText } = within(foo)
getByText(/submit/)

perhaps I could do another PR for that one. I am still new to AST so there's probably a better way to do this one.

Thank you very much!

Copy link
Member

@Belco90 Belco90 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Gonzalo, thanks for your PR!

The implementation and tests seem fine. You are right about the other scenario: it should cover destructuring queries from within too. I think it should be addressed here, so take your time to investigate that. There are similar behaviors around the plugin code, you can probably spot how to do something similar in other rules! If you struggle with this, let me know so I can guide you to proper way of checking that.

Additionally, there is something weird in your branch. Commits are from "unknown", and it's causing issues as travis-ci stopping pipelines because it's not secure or something. Could you take a look at that?

@gndelia
Copy link
Collaborator Author

gndelia commented Apr 27, 2020

sure, I will take a look. I will fix that problem about my user (probably a git config error 🤔 ) and I will try to address the destructuring scenario during the week. Thanks for the feedback!

@gndelia gndelia force-pushed the bug/false-positives-prefer-screen-queries-when-using-within branch 2 times, most recently from 58fa568 to ba73d0f Compare April 27, 2020 12:21
@gndelia
Copy link
Collaborator Author

gndelia commented Apr 27, 2020

Fixed the author issue 😁

edit: Now that my email is properly set I'm getting the CI errors - I Will fix it. I will increase the coverage

@gndelia gndelia force-pushed the bug/false-positives-prefer-screen-queries-when-using-within branch from ba73d0f to d2274f6 Compare May 2, 2020 20:56
@gndelia
Copy link
Collaborator Author

gndelia commented May 2, 2020

Pushed an update

the following scenario should now work

const { getByText } = within(screen.getByText('foo'));
getByText('foo');

the only pending scenario is

const myWithinVariable = within(foo)
myWithinVariable.getByText('baz')

and after that, the PR should be ready

@Belco90
Copy link
Member

Belco90 commented May 3, 2020

@gndelia Awesome, nice work! We are about to release a migration to TypeScript tho, maybe you prefer to wait until that's released to keep working on it. Just FYI.

@gndelia
Copy link
Collaborator Author

gndelia commented May 3, 2020

thanks!

I see. I will rebase my work once that one is released - I have experience working with TS so it should be straightforward to adapt my changes to it. thanks for the FYI

@Belco90
Copy link
Member

Belco90 commented May 4, 2020

TS version of the package moved. Let us know if you need help rebasing your changes or adapting the code to TS!

@gndelia
Copy link
Collaborator Author

gndelia commented May 4, 2020

thank you! I will try to do it during the week, worst-case scenario in the weekend.

@gndelia gndelia force-pushed the bug/false-positives-prefer-screen-queries-when-using-within branch 2 times, most recently from 5441e71 to d6d2392 Compare May 5, 2020 21:27
@gndelia
Copy link
Collaborator Author

gndelia commented May 5, 2020

I added that last case - PR is ready! My only doubt is that coverage is failing and I am not sure why. It states that this line is not being covered in the tests, but that does not make sense, as the following line is... any ideas?

Thanks!

@Belco90
Copy link
Member

Belco90 commented May 6, 2020

Our current coverage check it's too picky, isn't it? I'd like to migrate from forced 100% coverage threshold form jest to codecov checking the current coverage isn't decreased. Meanwhile, I'm afraid we need to try to cover that line. I'll try to check it later.

However, the PR itself is looking fine so far! I'll try to do deeper review later too. Thanks.

@Belco90
Copy link
Member

Belco90 commented May 7, 2020

Btw I still see all the pipeline executions from this PR as "abuse detected" on travis: https://travis-ci.org/github/testing-library/eslint-plugin-testing-library/requests

This is the first time I see something like that.

@gndelia gndelia force-pushed the bug/false-positives-prefer-screen-queries-when-using-within branch 2 times, most recently from a96746d to d0c4bed Compare May 7, 2020 12:10
@gndelia
Copy link
Collaborator Author

gndelia commented May 7, 2020

Btw I still see all the pipeline executions from this PR as "abuse detected" on travis: https://travis-ci.org/github/testing-library/eslint-plugin-testing-library/requests

This is the first time I see something like that.

really not sure what's that about 🤷 I sent an email to travis support to see if they can help.

As of the build, here's a screenshot of the coverage

image

as you can see, line 69 does not seem covered, though I don't know why 😞

@gndelia gndelia force-pushed the bug/false-positives-prefer-screen-queries-when-using-within branch from d0c4bed to a17239c Compare May 7, 2020 20:35
@gndelia
Copy link
Collaborator Author

gndelia commented May 7, 2020

after talking with Travis support, the builds fail normally and my account does not appear as "abuse detected" anymore 😄

now I need to figure out why that line appear as not tested in coverage lol

@Belco90
Copy link
Member

Belco90 commented May 8, 2020

@gndelia Cool, I see the CI working fine now! I hope I can check the coverage threshold soon, I'll let you know when I find something.

@Belco90
Copy link
Member

Belco90 commented May 8, 2020

Ok, mystery solved 🔍

If you open the generated coverage html report in the browser, you can see why it's complaining about that particular line:
image

There is no test for checking that if statement when the condition evaluates to false. But I can't think about a particular case to test this from an ESLint rule test to be honest.

What we are gonna do instead is relaxing the threshold for branches, as I don't see the point of having 100% there for now. Could you do that for me in your PR please? You need to set branches to 90 rather than 100 in jest.config.js.

Sorry to bother you with all that coverage threshold stuff 😅 . After that should be ready to go!

@gndelia gndelia force-pushed the bug/false-positives-prefer-screen-queries-when-using-within branch from a17239c to f97f691 Compare May 8, 2020 21:12
@gndelia
Copy link
Collaborator Author

gndelia commented May 8, 2020

I see, thanks for checking !

my first thought was about that else, but I thought the following scenario

const [myVariable] = render()
myVariable.getByText(baz)

was verifying it. Now I see it doesn't.

I added another test, perhaps not so valid as within does not return an array response 😆 but it gets coverage 100% now

const [myVariable] = within()
myVariable.${queryMethod}(baz)

I'd rather keep the coverage for the rules as the maximum, as it's reading all kinds of code, so at least we should cover a wide variety of examples. Having said that, if you still consider the threshold should be lowered, I can just do a separated PR, no problem 😃

the PR now should be ready, the build passed 🎉

@Belco90
Copy link
Member

Belco90 commented May 9, 2020

That's even better! Thank you so much for your effort. Approving and merging 🚀

@Belco90 Belco90 merged commit 28238aa into testing-library:master May 9, 2020
@Belco90
Copy link
Member

Belco90 commented May 9, 2020

🎉 This PR is included in version 3.1.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@Belco90
Copy link
Member

Belco90 commented May 9, 2020

@all-contributors please add @gndelia for code, tests and doc

@allcontributors
Copy link
Contributor

@Belco90

I've put up a pull request to add @gndelia! 🎉

@gndelia gndelia deleted the bug/false-positives-prefer-screen-queries-when-using-within branch May 9, 2020 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

prefer-screen-queries: false positives when query used from within
2 participants