-
-
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
the second argument of jest.mock must be a function #4588
Comments
It accepts functions, it just doesn't accept references. If you'd like, you could send a PR to |
@cpojer is there a good reason why |
Yes, because your mock may be required before references are fully initialized. It's easier to see Consider a module Banana that depends on Kiwi, which is mocked:
When Banana requires Kiwi, we know Kiwi is mocked. We run the function and then it throws saying
This is why you cannot reference anything from the outer scope. Hope that makes sense. |
Thanks! See #4593 |
Hi, is there a way to automatically call mock on every test suite? I ran into this (now updated) error message when trying to create a mock module for i18n and call it as I know this function will be required in multiple modules so I am trying to avoid writing this fix in every test.
|
@lmenus add your mock to a file called |
@SimenB Ah, missed that, thank you! This certainly reduces it to a mere |
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. |
What is the current behavior?
jest.mock(moduleName, func)
throws the error,the second argument of jest.mock must be a function
if func is defined as:...but works if the func is defined inline as an arrow function:
jest.mock(moduleName, () => 'Hello')
In both cases
moduleName
is a string like'fs'
What is the expected behavior?
Either of:
the second argument of jest.mock must be an arrow function
jest.mock
accept afunction
that's not an arrow functionThis is in a bare-bones Node app that doesn't use Babel and has index.js that prints a message to console.
The text was updated successfully, but these errors were encountered: