jhnns / rewire

Easy monkey-patching for node.js unit tests
MIT License
3.08k stars 128 forks source link

Rewiring transitive dependencies? #124

Closed CMCDragonkai closed 6 years ago

CMCDragonkai commented 7 years ago

So the example shows the ability to rewire the 'fs require. However if the module you're rewiring also requires/imports another module which also requires fs, do all transitive uses of fs get rewired to the fs mock or is it only 1 level deep?

JvJefke commented 7 years ago

No this will not be mocked. The only thing rewire does (at the moment), is giving you the opportunity to reassign global variables (var, let, const) of the module itself. It is also important to note that the original fs will be loaded initially. You can replace the result of the require with a mock object using rewire __set__ or __with__. So if the required module (eg. fs) does something on initiating, this will still be executed. If this is an issue, you'll need something like proxyquire to prevent this.

jhnns commented 6 years ago

Yes. It's only one level deep. And there are no plans to change that because I think it could be very confusing otherwise.