-
Notifications
You must be signed in to change notification settings - Fork 470
fix(TS): Forbid async function as callback argument for waitFor #572
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(TS): Forbid async function as callback argument for waitFor #572
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 839be13:
|
Codecov Report
@@ Coverage Diff @@
## master #572 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 23 23
Lines 462 462
Branches 114 114
=========================================
Hits 462 462 Continue to review full report at Codecov.
|
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.
I would love to add a test, but the pre-commit hook that builds typescript files does not allow it. With dtslint, you can add an $ExpectError comment to test error cases. But if I add such a test to the type-tests.ts file, the build in the pre-commit hook fails. And I did not find a solution to disable the build for that file
I have an idea for that.
In the meantime you could use https://www.typescriptlang.org/play/ to illustrate how you'd test that.
Just checked and it definitely works. Either your test or implementation is faulty. Just push the test so that we can take a look at it together. |
I cannot commit it. :D Basically what I want to do is to add this to the test file: await waitForElementToBeRemoved(getByText(element, 'apple'))
await waitForElementToBeRemoved(getAllByText(element, 'apple'))
+
+ // $ExpectError
+ await waitFor(async () => {})
} But if I want to commit it, the commit fails:
Which is expected. We want to have an error there. I think the best way would be to not build the ts files here. The test of the types is done by dtslint. Here's a playground link: https://www.typescriptlang.org/play/#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXwHcosMAxHGAHgBUA+ACjCgggCMowBrALnnoEp4AXlrxq8EAA8MIVMADO8AAowcAWyzyQlKKgCeogPzxUIAG4gY8XtX69degFCOiJcjHpR5e9H34iAbwBffiA |
|
Thank you. I pushed the commit. |
I'm not sure of the best way to make tsc ignore the Should we just make |
I think that would be the best. How would you tackle this, @kentcdodds ? Changing the kcd-scripts? |
I think we should change kcd-scripts to not typecheck check Would you be interested in doing that? |
Hey @eps1lon and @kentcdodds, I updated the version of kcd-scripts. Now the type-tests.ts file can be committed even though it has a ts-error in it. Please re-review. :) |
Thank you so much 👏 @all-contributors please add @Lukas-Kullmann for code and tests |
I've put up a pull request to add @Lukas-Kullmann! 🎉 |
🎉 This PR is included in version 7.5.8 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
What:
Change typescript definitions to forbid async functions in
waitFor
.Why:
Promises returned by
waitFor
do not work as one might expect (i.e. a rejected promise does not mean the same thing as a throw in the function). So these examples work differently:So in order to prevent a user from using an async function, a typescript error would be good. This is what this PR adds.
How:
I've added a conditional type to the return value of waitFor's callback that makes it impossible to return a promise from the callback.
Checklist:
docs site
DefinitelyTyped
I did not check the first three points because
$ExpectError
comment to test error cases. But if I add such a test to the type-tests.ts file, the build in the pre-commit hook fails. And I did not find a solution to disable the build for that file.Also, I did not find an information how you correctly prefix the commit message. So I hope I did it correctly. :)