You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Storage test
Mocked init...
Mocked health...
storage.health: mocked health
✔ Health test
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:
First - it is more than possible and callThrough should handled it.
Second - you probably want to move init function to a separate file and mock it, so the "original" file still holding .health will have access to client, but the client will be mocked.
Thirdly - you don't have to use dependency injection for this case, consider adding ability to "inject" client from the outside, so one will be able to initialize it from the outside.
In any case, can you do console.log(storage), wondering what is inside this object.
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: