jhnns / rewire

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

Possible to set variable during rewiring? #180

Closed studioph closed 2 years ago

studioph commented 4 years ago

Is it possible to set the value of a variable at the same time as rewiring? I have this code:

if (typeof(mymodule) === 'undefined'){
    var mymodule = require('mymodule');
}

and would like to be able to override the mymodule variable when calling rewire so that the variable is defined with a mock, and doesn't try to load it from node_modules.

If this isn't possible with rewire, if anyone has any resources on how I could achieve this I would be most appreciative.

Thanks!

georgezlei commented 3 years ago

second this issue. rewire function can have an optional parameter to pass in variables. For example,

const target = rewire('./target', { toOverride: 'value' })
jhnns commented 2 years ago

I'm sorry, this is out of scope of the current design. The module needs to be evaluated in order to change the internals. Fixing or changing this would require a major rewrite.