-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Changing require result instead of variables #24
Comments
Yes, I see the advantages. It's more likely to change a variable name than the dependency. But I think your suggestion is impossible to implement because rewire does no static analysis of your code. Thus it doesn't know which variables are referencing which module. The only way to accomplish this is by mapping the dependency-paths on module startup like node-sandboxed-module does: var foo = rewire("./foo.js", {
"./util.js": utilMock
}); But if your prefer this approach I think it would be better to just use node-sandboxed-module 😉. Personally I came to the conclusion that after all unit-tests are usually gray-box-tests. Basically you are aware of the implementation but you're trying to test it like you didn't know. If you're changing the implementation you'll likely need to adjust the unit-test as well. I like your idea, but I don't know how to implement it. |
Okay, thanks for your answer. Too bad I can't have the best of rewire and sandboxed-module. I think I'm gonna stick with rewire either way since I really like the non-eval approach :) Interesting about the gray-box-testing. Never thought about that in that way, but I guess it makes sense. |
Nevermind, it's always good to discuss about improvements 👍 |
First of all: I love the way rewire is built and I really see the advantages by keeping and using an intact require function.
The only drawback i can see by doing this instead of the node-sandboxed-module approach, is that you have to keep track of variable names being used in the module being mocked. With the altering-require-function approach, one only needs to remember which module the mocked module is dependent upon.
For example, let's say we have the following module foo.js:
If I would like to rewire the module for better testing I would do something like this:
With node-sandboxed-module I would do something like this:
The second approach has two advantages in my opinion:
Now, I understand that this might not be the natural way of doing this since rewire doesn't alter the require function, but I wonder if there is any way of mimicking this feature? Maybe rewire could check which variable names are assigned to the required module that the user would like to mock and then simply altering the variables for the user.
Something like this:
What do you think? :)
The text was updated successfully, but these errors were encountered: