-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
jest-validate
does not work well with async functions
#7894
Comments
You're right, we don't have a separation of sync/async functions in |
@thymikee this is the opposite: there is a separation of sync/async functions in As described above this creates issues with Babel transpiling, or when converting from/to an If you're happy with the following solution, I can submit a PR: if See current code. |
I'm cool with that. Just add a test :D |
Done at #7896 |
## Summary `jest-validate` distinguishes between sync and async functions, but it should not. Fixes #7894. ## Test plan ```js const { validate } = require('jest-validate') assert(validate({ name: async () => {} }, { exampleConfig: { name: () => {} } }).isValid) assert(validate({ name: () => {} }, { exampleConfig: { name: async () => {} } }).isValid) assert(validate({ name: async () => {} }, { exampleConfig: { name: async () => {} } }).isValid) assert(validate({ name: () => {} }, { exampleConfig: { name: () => {} } }).isValid) ```
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🐛 Bug Report
When validating input using
jest-validate
'sopts.exampleConfig
, the validation distinguishes between sync and async functions which creates three issues.First:
opts.exampleConfig
uses anasync
function, functions returning a promise will make the validation fail, butasync
functions will pass. However one might expect that those two are functionally equivalent.opts.exampleConfig
uses a non-async
function, functions returning a promise will make the validation pass, butasync
functions will fail.multipleValidOptions()
can be used to work around this problem, but I expect very few users will do so.Second: when using Babel
async
function are transpiled to non-async
function, changing the behavior ofjest-validate
. Also this meansjest-validate
always validate against async functions when the code is using Babel.Third: the error message is not descriptive enough as it says
function
for both sync and async functions.Note that the current type checking is using
Object.prototype.toString
, which returns either'[object Function]'
or'[object AsyncFunction]'
.To Reproduce
Expected behavior
If a sync function is passed to
exampleConfig
, async functions should pass.If an async function is passed to
exampleConfig
, sync functions should pass.Link to repl or repo (highly encouraged)
https://repl.it/repls/WelcomeFrizzyService
Run
npx envinfo --preset jest
The text was updated successfully, but these errors were encountered: