-
-
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
Make sure function mocks match original arity #4170
Conversation
Thanks for sending a PR, including tests. I think to get this merged it needs to be correct and support a variable length. After 10, we could code-gen and use "new Function", what do you think? |
Codecov Report
@@ Coverage Diff @@
## master #4170 +/- ##
==========================================
- Coverage 60.55% 60.42% -0.14%
==========================================
Files 196 196
Lines 6776 6809 +33
Branches 6 6
==========================================
+ Hits 4103 4114 +11
- Misses 2670 2692 +22
Partials 3 3
Continue to review full report at Codecov.
|
@cpojer I did attempt this several times. I'm not sure it's possible.. also this is very similar to how sinon solves this issue: https://github.com/sinonjs/sinon/blob/master/lib/sinon/spy.js#L61 |
We could try-catch for this and fall-back to the solution that doesn't have proper arity. It means it'll work properly in most environments, and fall back to a working solution at least in an environment without eval. |
@cpojer perhaps the default case should be the same as the 0 case. This way if it's out of range, as you say, it would return a 0 length function like it does right now. |
@cpojer let me know if my current solution works for you or if I need to make other changes. I'm not sure a dynamic case will work... but if you could point me in the right direction I'm willing to try. |
Works for me, thank you! |
* Make sure function mocks match original arity * Mocks maintain function arity with 9 or less arguments * Update index.js
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
As reported in #3997, Jest mocks currently do not have the same arity as their underlying functions. This PR attempts to fix that.
Test plan
Run unit tests locally.
Test in a local project using tests provided in issue #3997
Write unit test in jest-mock to ensure arity is set properly