Skip to content
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

Rewiring a nested module #61

Closed
cazgp opened this issue May 29, 2015 · 2 comments
Closed

Rewiring a nested module #61

cazgp opened this issue May 29, 2015 · 2 comments

Comments

@cazgp
Copy link

cazgp commented May 29, 2015

I would like to rewire a module that sits quite far down the require hierarchy. Simply rewiring it at the top level is not enough. I then tried something like this:

# This in the top level on its own doesn't work
validate = rewire './validate'
validate.__set__ 'func', -> true

moduleD = rewire './moduleD'
moduleD.__set__ 'validate', validate

moduleC = rewire './moduleC'
moduleC.__set__ 'moduleD', moduleD

moduleB = rewire './moduleB'
moduleB.__set__ 'moduleC', moduleC

moduleA = rewire './moduleA'
moduleA.__set__ 'moduleB', moduleB

.. but the original validate function is still always called. A console.log at the top of validate shows it's being loaded twice - shouldn't it only be loaded once? Validate is required at least 5 different times in the app. I even rewired all of those modules with the rewired validate, but nothing has worked.

Is this a bug, not a feature, or user error?

@cazgp
Copy link
Author

cazgp commented May 29, 2015

OK so trawling through old issues seems to have helped.

require.cache[require.resolve './validate'] = exports: validate

Any chance you could add this to the docs for future users?

@jhnns
Copy link
Owner

jhnns commented Jul 12, 2015

Sorry for the late response.

Is this a bug, not a feature, or user error?

This is a feature, because this way you have the full control. Do you want the rewired version to be in the global require cache or do you just want the rewired version to be isolated – it's up to you. Imho it would not be a good style if rewire did that for you. Then there would be issues like "Add option for global require cache"...

Any chance you could add this to the docs for future users?

I am happy about pull-requests. 😀

@jhnns jhnns closed this as completed Jul 12, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants