-
Notifications
You must be signed in to change notification settings - Fork 31
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
Question: Mocking local variable in the scope of original module #146
Comments
In any case, can you do |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inside my CommonJS Express app, I want to use rewiremock to replace the default Elasticsearch client with Elasticsearch mock. This package allows to intercept a desired request pattern and mock the response, like so:
In a simplified example, my service has two methods -
The mocked version uses the Elasticsearch mock instead of the actual client:
With the rewiremock initialised in a separate module
My test file looks as follows:
When the module.exports.health is not implemented in the mock, the test fails
But If I implement the health function it works fine:
What I would like to achieve is to mock nothing but the init function, which is replacing the original ES client with the mocked one.
It seems that unless I implement a method inside my stub, rewiremock is using the client variable from the scope of the original module.
In other words, I can see that the init function is mocked successfully. I have no issue that the original health function is called - that's the desired behavior. What I expect is that the mocked init sets the client in the scope of the original module.
May I ask for some help? Is it even possible?
The text was updated successfully, but these errors were encountered: