-
Notifications
You must be signed in to change notification settings - Fork 128
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
possible to set variable not defined explicitly with var? #35
Comments
I made a PR here, but weirdly, the try/catch block doesn't work in mocha so the test doesn't pass, but works find in my code. I don't get it. https://github.com/phishy/rewire/commit/9a459d1e0242cf40368f0b9c51176b58c63658bb |
Sorry, can you give me an example? I don't understand what the problem is 😃 |
In MVC frameworks like Sails.js, global variables exist inside modules that are not defined in that module. I would like to be able to rewire those, however, rewire didn't allow injecting variables that weren't defined with var. This PR allows you to introduce new variables into a module that weren't defined with var in that module. Something like this: https://gist.github.com/phishy/e135bdddeaf176bd5a71 |
That works, I've added a test-case for it. The problem with implicit global vars is though, that they will be changed globally by rewire. Thus writing Product.__set__({ Vendor: vendorStub }); is effectively the same as Vendor = vendorStub; That's because rewire prepends a list of var statements that import global vars to the local scope. To create this we need to know all globals before the module is executed, but implicit global vars are created as the module is executed. However, I don't think that is a great problem. When the module to test uses implicit global vars you don't need rewire to change them. rewire has been designed to be used with a module system. |
My change is still working locally, however, I believe Johannes fixed it On Fri, Jan 2, 2015 at 2:10 AM, cdnadmin notifications@github.com wrote:
|
@jhnns I'm sorry. I'm just now realizing that you added a test case but never added a fix like the one I had on my pull request. I'm just now realizing as I recloned my project and lost my changes to rewire. Let me try to explain further. Using the Sails.js framework, all Models and Services are automatically added to global scope. Therefore when I require a file in order to test it, there are variables being used in that file that were never defined within it (with or without var). This is why I created the pull request that allows overwriting variables that are attached to global. I understand that I'm requesting a feature in rewrite to overcome deficiencies in poorly designed frameworks, however, I don't know enough about rewire to understand if adding this feature would have side effects. Otherwise, it would allow mocking and testing object in Sails.js and frameworks like it that hang some objects onto global. https://github.com/phishy/rewire/commit/9a459d1e0242cf40368f0b9c51176b58c63658bb |
Sorry, but wrapping If the variable has been added by Sails.js to the global scope before you call Could you describe your actual problem? Is there an error? Does mocking not work? |
When I am mocking the model, none of the variables that would be set by
|
Could you set up a tiny example project where the problem exists? It's hard to tell the issue when I don't know enough about Sails. |
No problem. It's really just about testing files and mocking any globals Here is a demo. When running the Mocha test, you can see that it fails to overwrite the On Tue, Feb 10, 2015 at 9:24 AM, Johannes Ewald notifications@github.com
|
You need to initialize Sails at least to test your modules. Rewire can't mock anything when it is not defined. |
I'm already successfully mocking everything and its working great. I guess
|
Its incredibly expensive to load Sails, hence the mocking, plus it's
|
How does your fork solve your problem? You're just wrapping the You just need to define the variables to mock on the |
It solves my problem by allowing me to inject global variables into the It solves the problem, because the patch actually allows writing a global On Wed, Feb 11, 2015 at 8:47 AM, Johannes Ewald notifications@github.com
|
Sorry, that is just not true. The |
Let me correct myself. It doesn't allow me to add variables onto global.*, On Wed, Feb 11, 2015 at 10:51 AM, Johannes Ewald notifications@github.com
|
That would be great 😀 |
Here is a test case for wanting to be able to set undefined variables. https://gist.github.com/phishy/cb06c276a332b01ec149 |
I'm so sorry. I just didn't get what you were trying to say 😁 Should be solved with 2.3.0 |
Thanks. On Mon, Feb 16, 2015 at 7:16 PM, Johannes Ewald notifications@github.com
|
I'm pretty sure I have a use case for this too, thanks! |
Same here. I m using rewire though not sure if there is any other latest plugin that is better for mocking and checking internal variables during unit tests |
In some js frameworks, variables are injected into a file that appear to be "global" and have no var definition. I would love to be able to inject variables into a file that are not explicitly defined with var. I can surely look into adding this, was just curious if there is a philosophy difference here.
Example: https://gist.github.com/phishy/c9ac6b2aad7d2f87a5de
The text was updated successfully, but these errors were encountered: