-
Notifications
You must be signed in to change notification settings - Fork 150
Feature request: convert no-get-by-for-checking-element-not-present to be prefer-appropriate-query-for-expect #90
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
I don't see any reference for this in the doc:
To be honest, even if the doc doesn't mention that, it could work fine but this is just an optional rule. It's not forcing anyone to follow this advice and it's more a best practices thing. I don't think this is an issue then. |
@Belco90 i like the rule actually when used in expects but this new approach is actually a very good practice and the rules should not discourage it. Actually in your second example it is essentially the same. If it throws the test will fail which is fine and if it doesn't throw it will wait for the removal. So effectively it's the exact same code path. I still maintain that expect(queryByText("Foo")).toBeInTheDocument() should report but waitForElementToBeRemoved(getByText("Foo")) should not. Can we open this discussion up to the broader community before quickly closing the issue? |
Sure. But those 2 examples are not technically the same and it's explained why. As mentioned, even if the |
How is it a best practice to not use getBy with waitForElementToBeRemoved? Is there a scenario you had in mind when the rule was written? I'm of the opinion it actually is better to use getBy when something is supposed to exist because it gives you better erroring. In this case an element needs to exist before it can be removed so what am I missing? |
Also to be clear, the rule is for prefer get by for checking non existence. It's not a rule for not using get by as you commented. |
Also I was thinking the rule could be expanded to include using queryBy for checking existence. Ie Should report, do you agree? |
The rule was originated by:
I'm afraid not. By the rule name (no-get-by...) and rule details the idea of this rule is not to use
👆 Why? The rule is aiming just to test when checking element not present. In this example you are checking that an element is present. Additionally, there is no issue on asserting element existence with Please take a look to all these docs and motivations for the rule and let me know. |
The issue on checking presence with queryBy is it does not give you good errors when it fails to find the element in the way that getBy or findBy does. Would you be open to it being in a septate rule? With regards to the use of getBy in waitForElementToBeRemoved I'd be curious to hear from @kentcdodds on if there's any reasons to use queryBy over getBy. It seems to work just fine to me... |
I'm fine to get another issue for a new rule to prefer asserting with Again, even if |
Yeah I'll file a new request for that. 👍 Using get by in a waitForElementToBeRemoved isn't the same as using findBy instead of waitFor + queryBy Still not seeing how it's a best practice but maybe Kent will give some insight |
Actually, these are the same thing: // this:
const submitButton = screen.getByText(/submit/i)
await waitForElementToBeRemoved(submitButton)
// is the same as this:
await waitForElementToBeRemoved(screen.getByText(/submit/i)) Both of those should work equally well. The disappearance recipe works just as well with I believe this should report: // reports:
expect(queryByText("Foo")).toBeInTheDocument()
// does not report:
expect(queryByText("Foo")).not.toBeInTheDocument()
expect(getByText("Foo")).toBeInTheDocument() |
Both should work equally well is not the same as they are the same thing tho. In first example you are 100% sure nothing will throw within If I don't get why a rule called |
I think you're confused @Belco90. Look carefully: // this:
const submitButton = screen.getByText(/submit/i)
await waitForElementToBeRemoved(submitButton)
// is the same as this:
await waitForElementToBeRemoved(screen.getByText(/submit/i)) Just like: const foo = 'hi'
console.log(foo)
// is the same as this:
console.log('hi') |
@kentcdodds I don't think is comparable as we are talking about |
Personally I would vote for changing that rule name to prefer-appropriate-query-for-expect or something to that effect, rather than having 2 separate rules as ultimately they are just inverses of the same thing. |
I'm sorry I'm not able to communicate this effectively to you @Belco90, but you're still misunderstanding. I think you're reading it like this: // this:
const submitButton = screen.getByText(/submit/i)
await waitForElementToBeRemoved(submitButton)
// is NOT the same as this (I think this is what you're thinking we're doing, but it's not):
await waitForElementToBeRemoved(() => screen.getByText(/submit/i))
// but it IS the same as this:
await waitForElementToBeRemoved(screen.getByText(/submit/i)) |
If we end up just leaving only the expect check, I agree on that (tho it would mean a breaking change). @kentcdodds yeah I noticed it's not wrapped withing a function anymore. I feel I can't communicate properly what I want, but if the method works properly using |
@Belco90 I've personally seen it work just fine previous to v7. we have tests that do this: waitForElementToBeRemoved(() => getByText("foo")) |
Then I'll close #91 in favour of of repurposing this one to convert the rule into a different one. |
cool thanks @Belco90 ! |
I'll update the description to include that as well. |
@Belco90 updated the description, let me know if that sounds good to you? |
So just to sum up:
Is that right? |
I think we should improve the name, as this doesn't apply to all expects in general but only those checking presence/absence of elements. |
Yeah agreed just couldn't think of a better name. Everything you summed up looks good to me |
Can't come up with something better than |
This shouldn't be really complex to implement to be honest, but I'd like to wrap this together these other changes for v3:
|
I'd say less is more just go with |
feat(await-async-utils): reflect waitFor changes (#89) feat: new rule no-wait-for-empty-callback (#94) feat: new rule prefer-wait-for (#88) feat: new rule prefer-screen-queries (#99) BREAKING CHANGE: drop support for node v8. Min version allowed is node v10.12 (#96) BREAKING CHANGE: rule `no-get-by-for-checking-element-not-present` removed in favor of new rule `prefer-presence-queries` (#98) Closes #85 Closes #86 Closes #90 Closes #92 Closes #95 Co-authored-by: timdeschryver <[email protected]>
🎉 This issue has been resolved in version 3.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
In addition as discussed below in the comments, the use of
get*
queries inside of waitForElementToBeRemoved will no longer report an error either in a callback or directly as a parameter.The text was updated successfully, but these errors were encountered: