-
Notifications
You must be signed in to change notification settings - Fork 141
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
fix(prefer-explicit-assert): only enforce assertion when presence/absence matcher #238
fix(prefer-explicit-assert): only enforce assertion when presence/absence matcher #238
Conversation
@@ -144,14 +152,44 @@ ruleTester.run(RULE_NAME, rule, { | |||
code: `expect(getByText('foo')).toBeDefined()`, | |||
options: [ | |||
{ | |||
assertion: 'toBeInDocument', | |||
assertion: 'toBeInTheDocument', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one 💯
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking really good! Thanks also for taking the time to refactor and extract those utils, really appreciated.
@skovy Should rule's doc be updated to reflect this change? |
I think the current docs are accurate, but I can update with more details about the specific logic and thinking around this rule. |
@Belco90 updated the docs, let me know if that was roughly what you had in mind or something else? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, that's more than enough. Thanks!
🎉 This PR is included in version 3.9.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Problem
This is a follow-up improvement to issue #218.
The original implementation was flawed. With the way this was implemented, any
getBy*
query will trigger a linting error if it's not usingtoBeInTheDocument
(assuming the config defined theassertion
to use astoBeInTheDocument
). However, there are still valid cases to use a different assertion. For example, if you want to check if a button is disabled (toBeDisabled
) or maybe check if an anchor has the appropriatehref
attribute (toHaveAttribute
).With the way the rule was implemented, both of these examples would trigger as violations (since they're not using
toBeInTheDocument
) but they're both still valid uses ofgetBy*
and thus are false positives.Solution
Now, this rule only triggers if the matchers used are
toBeInTheDocument
,toBeTruthy
,toBeDefined
,not.toBeNull
, ornot.toBeFalsy
and doesn't match the configuredassertion
. These matches already existed as a constant for another rule (prefer-presence-queries
), so I moved them toutils
so they could be reused. This rule also had somewhat related logic for handling negated matchers. This logic was adapted for this rule.