Skip to content

Enforce await on async utilities #49

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

Closed
cdagli opened this issue Dec 10, 2019 · 7 comments · Fixed by #69
Closed

Enforce await on async utilities #49

cdagli opened this issue Dec 10, 2019 · 7 comments · Fixed by #69
Labels
new rule New rule to be included in the plugin released

Comments

@cdagli
Copy link

cdagli commented Dec 10, 2019

Currently https://github.com/Belco90/eslint-plugin-testing-library/blob/master/docs/rules/await-async-query.md rule only supports findBy* and findByAll*. What do you think about enabling it for other async utilities, too?

https://testing-library.com/docs/dom-testing-library/api-async

const ASYNC_QUERIES_REGEXP = /^find(All)?By(LabelText|PlaceholderText|Text|AltText|Title|DisplayValue|Role|TestId)$/;
@thomaslombart
Copy link
Collaborator

thomaslombart commented Dec 10, 2019

Thanks for suggesting this idea @cdagli!

This rule currently exists because findBy and findByAll doesn't (in my opinion) expresses the fact that there are actually asynchronous queries.
However, it's pretty clear that the other async utilities such as wait or waitForElement are asynchronous. I'm not sure if we would need to enable them 🤔

Have you encountered a use case where someone forgot to use await or .then on one of these async utilities? 🙂

@thomaslombart thomaslombart added enhancement New feature or request question Further information is requested and removed enhancement New feature or request labels Dec 10, 2019
@cdagli
Copy link
Author

cdagli commented Dec 10, 2019

Yes, I know couple of people that keeps forgetting to add await these methods 😄 (Myself included)

Weird stuff starts to happen if you forget to await. And for example if you have a test case that ends up with waitForElementToBeRemoved and if you don't await, your test might pass magically.

@Belco90
Copy link
Member

Belco90 commented Dec 10, 2019

I just realized eslint is not warning about that (I thought it did because those async utils are fixed, not as findBy* queries which are dynamically generated so eslint was checking returned type properly): I thought it did but it was my IDE! So this is an interesting idea, maybe something like await-async-utils?

@thomaslombart
Copy link
Collaborator

thomaslombart commented Dec 10, 2019

await-async-utils sounds good to me!

In the end, forgetting to await an async function is a common mistake, not just when using Testing Library's tools. I tried to search if there was a way to prevent this globally but haven't found anything.
It would be cool to have a tool that performs this check when calling async functions.

@Belco90 Belco90 added the new rule New rule to be included in the plugin label Dec 10, 2019
@benmonro
Copy link
Member

👍 I would love this rule!

@Belco90
Copy link
Member

Belco90 commented Jan 17, 2020

I'm back to handle this issue. Just to clarify, the expected utils checked here would be:

  • wait
  • waitForElement
  • waitForDomChange
  • waitForElementToBeRemoved

Right?

@Belco90 Belco90 removed question Further information is requested enhancement New feature or request labels Jan 17, 2020
@Belco90
Copy link
Member

Belco90 commented Jan 28, 2020

🎉 This issue has been resolved in version 2.0.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