Skip to content
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

Unnecessary presence checks on findBy queries #743

Closed
nathanmmiller opened this issue Mar 30, 2023 · 10 comments · Fixed by #815
Closed

Unnecessary presence checks on findBy queries #743

nathanmmiller opened this issue Mar 30, 2023 · 10 comments · Fixed by #815
Assignees
Labels
new rule New rule to be included in the plugin released

Comments

@nathanmmiller
Copy link
Contributor

Name for new rule

no-find-by-presence

Description of the new rule

The rule should avoid things like expect(await screen.findBy*()).toBeInTheDocument(), because that's functionally the same as await screen.findBy*().

Perhaps this is a new rule (I proposed a name) or maybe it should just be part of the existing find-by rule.

Testing Library feature

await findBy

Testing Library framework(s)

All, presumaly.

What category of rule is this?

Suggests an alternate way of doing something

Optional: other category of rule

No response

Code examples

expect(await screen.findByRole("button")).toBeInTheDocument();
expect(await screen.findByText("hello")).toBeDefined();
expect(await screen.findByLabel("zoop!")).toBeTruthy();

Anything else?

I am happy to contribute this rule - but if so, it may take me a month or two to do so, as things are busy. Please let me know if you'd like me to contribute or not.

Do you want to submit a pull request to make the new rule?

Yes

@nathanmmiller nathanmmiller added new rule New rule to be included in the plugin triage Pending to be triaged by a maintainer labels Mar 30, 2023
@Belco90
Copy link
Member

Belco90 commented May 2, 2023

Hi @nathanmmiller! I think it could be a good idea to implement this rule, so give it a try if you are willing to contribute to this plugin!

We need to make sure we explain in its docs that it would be the opposite of prefer-explicit-assert, clarifying only one should be enabled, and the plugin actually recommends the explicit one. This rule could be named prefer-implicit-assert then.

@Belco90 Belco90 removed the triage Pending to be triaged by a maintainer label May 2, 2023
@adevick
Copy link
Contributor

adevick commented Sep 11, 2023

I like this idea and have started my first open-source PR 🎉 prefer-implicit-assert #815

@adevick
Copy link
Contributor

adevick commented Sep 11, 2023

@nathanmmiller ^

@nathanmmiller
Copy link
Contributor Author

nathanmmiller commented Sep 12, 2023 via email

@adevick
Copy link
Contributor

adevick commented Sep 12, 2023

and the plugin actually recommends the explicit one

Why does the plugin need to recommend the explicit one? Can the community/consumers decide? Just asking because sometimes people blindly follow what the "library recommends" as the "gold standard"?

@Belco90
Copy link
Member

Belco90 commented Sep 12, 2023

@adevick We made that decision based on KCD's "Common mistakes with React Testing Library" post.

@adevick
Copy link
Contributor

adevick commented Sep 14, 2023

@adevick We made that decision based on KCD's "Common mistakes with React Testing Library" post.

That makes sense I guess that last one to me was more of his personal opinion For this reason, many people skip the assertion. This really is fine honestly, but I personally, ... but to each their own.

@nathanmmiller
Copy link
Contributor Author

I'd actually be curious on @kentcdodds thoughts on this one now that the above was brought up.

To me, I would always do:

expect(screen.getByRole("button")).toBeInTheDocument()

over

screen.getByRole("button")

...for the same reason KCD says - it's clear that there's an intentional assertion, not just an accidental remnant of some refactor.

But, also to me,

await screen.findByRole("button")

...feels more intentional - the whole await part of it makes me feel like we're purposely asserting something ("this will show up").

That said, I don't have an extreme preference here. If this rule exists, I will turn it on, even if it's not "recommended," for precisely the reasoning above. I can see that others wouldn't because it's not "recommended" but 🤷.

@kentcdodds
Copy link
Member

I don't feel very strongly about this. As noted in the article:

Importance: low

🤷‍♂️ I will personally keep the assertion, but feel free to do what you like.

Belco90 pushed a commit that referenced this issue Oct 12, 2023
Closes #743

* feat(prefer-implicit-assert): adding new rule

* feat(prefer-implicit-assert): increment number of rules

* feat(prefer-implicit-assert): reduce duplication

* feat(prefer-implicit-assert): add recommened rule by library, more test cases, update docs

* feat(prefer-implicit-assert): added findBy link

* feat(prefer-implicit-assert): added line and column checks

* feat(prefer-implicit-assert): use existing utils

* feat(prefer-implicit-assert): remove unnecessary checks
@github-actions
Copy link

🎉 This issue has been resolved in version 6.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new rule New rule to be included in the plugin released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants