Comments (23)
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
from rewire.
Sorry, can you give me an example? I don't understand what the problem is 😃
from rewire.
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
from rewire.
rewire didn't allow injecting variables that weren't defined with var.
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.
from rewire.
My change is still working locally, however, I believe Johannes fixed it
with a commit as well. Make sure you're on the right branch and you should
be good to go.
On Fri, Jan 2, 2015 at 2:10 AM, cdnadmin [email protected] wrote:
@phishy https://github.com/phishy did this ever work for you? I'm
having a similar issue with an AWS Lambda function. "context" is defined in
the aws env, so, my rewired tests are not working due to "context is not
defined" error.—
Reply to this email directly or view it on GitHub
#35 (comment).
from rewire.
@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
from rewire.
Sorry, but wrapping eval()
with try/catch is not a valid option. The method call should fail if the variable is not defined.
If the variable has been added by Sails.js to the global scope before you call __set__
, everything should work fine. When there is no global variable yet, rewire will throw a ReferenceError
which is the desired behavior.
Could you describe your actual problem? Is there an error? Does mocking not work?
from rewire.
When I am mocking the model, none of the variables that would be set by
Sails are there, hence undefined. Rewire fails to set them as well even if
I call set.
On Feb 9, 2015 6:18 PM, "Johannes Ewald" [email protected] wrote:
Sorry, but wrapping eval() with try/catch is not a valid option. The
method call should fail if the variable is not defined.If the variable has been added by Sails.js to the global scope before
you call set, everything should work fine. When there is no global
variable yet, rewire will throw a ReferenceError which is the desired
behavior.Could you describe your actual problem? Is there an error? Does mocking
not work?—
Reply to this email directly or view it on GitHub
#35 (comment).
from rewire.
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.
from rewire.
No problem. It's really just about testing files and mocking any globals
that would be injected by requiring another file, or being required by
another file.
Here is a demo.
https://ide.c9.io/phishy/sails
When running the Mocha test, you can see that it fails to overwrite the
Story variable, because it is not available outside of context when not
loading up the framework. In this case I am only trying to mock out all of
the external dependencies.
On Tue, Feb 10, 2015 at 9:24 AM, Johannes Ewald [email protected]
wrote:
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.—
Reply to this email directly or view it on GitHub
#35 (comment).
from rewire.
You need to initialize Sails at least to test your modules. Rewire can't mock anything when it is not defined.
from rewire.
I'm already successfully mocking everything and its working great. I guess
I'll just use my fork. Thanks for your help. It seems within reason to me,
that rewire should be able to inject undefined variables.
On Feb 10, 2015 7:45 PM, "Johannes Ewald" [email protected] wrote:
You need to initialize Sails at least to test your modules. Rewire can't
mock anything when it is not defined.—
Reply to this email directly or view it on GitHub
#35 (comment).
from rewire.
Its incredibly expensive to load Sails, hence the mocking, plus it's
cleaner, IMO.
On Feb 10, 2015 7:45 PM, "Johannes Ewald" [email protected] wrote:
You need to initialize Sails at least to test your modules. Rewire can't
mock anything when it is not defined.—
Reply to this email directly or view it on GitHub
#35 (comment).
from rewire.
How does your fork solve your problem? You're just wrapping the eval()
with try/catch...
You just need to define the variables to mock on the global
object and everything works fine. From the rewire point of view it doesn't make sense to mock global variables with rewire.
from rewire.
It solves my problem by allowing me to inject global variables into the
script. I know, it's a stupid argument, but some of the major frameworks
operate this way, and there's no reason to load the framework, and test it
when I"m writing userland code.
It solves the problem, because the patch actually allows writing a global
variable to succeed instead of throwing an exception.
On Wed, Feb 11, 2015 at 8:47 AM, Johannes Ewald [email protected]
wrote:
How does your fork solve your problem? You're just wrapping the eval()
with try/catch...You just need to define the variables to mock on the global object and
everything works fine. From the rewire point of view it doesn't make sense
to mock global variables with rewire.—
Reply to this email directly or view it on GitHub
#35 (comment).
from rewire.
Sorry, that is just not true. The eval()
throws an exception and the error is ignored, no variable is set on the global scope. Your provided test-case is checking for the wrong value, moduleFake.getValue()
returns myValue
, not undefinedValue
.
from rewire.
Let me correct myself. It doesn't allow me to add variables onto global.*,
it allows me to set any variable within the head of the script (ones not
explicitly defined with var). I will try to boil up another test case that
makes more sense, as it seems it is hard to explain how this change is
currently working for me.
On Wed, Feb 11, 2015 at 10:51 AM, Johannes Ewald [email protected]
wrote:
Sorry, that is just not true. The eval() throws an exception and the
error is ignored, no variable is set on the global scope. Your provided
test-case is checking for the wrong value, moduleFake.getValue() returns
myValue, not undefinedValue.—
Reply to this email directly or view it on GitHub
#35 (comment).
from rewire.
I will try to boil up another test case that makes more sense
That would be great 😀
from rewire.
Here is a test case for wanting to be able to set undefined variables. https://gist.github.com/phishy/cb06c276a332b01ec149
from rewire.
I'm so sorry. I just didn't get what you were trying to say 😁
Should be solved with 2.3.0
from rewire.
Thanks.
On Mon, Feb 16, 2015 at 7:16 PM, Johannes Ewald [email protected]
wrote:
I'm so sorry. I just didn't get what you were trying to say [image:
😁]Should be solved with 2.3.0
—
Reply to this email directly or view it on GitHub
#35 (comment).
from rewire.
I'm pretty sure I have a use case for this too, thanks!
from rewire.
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
from rewire.
Related Issues (20)
- global variable is not available in the module HOT 8
- Injected mocks add functionality rather than replace it HOT 1
- rewiring any function executes every line that isn't in any function
- Rewired function is not instanceof Function in Jest context HOT 1
- Rewire doesn't work with typescript HOT 3
- Rewire does not seem to work with flow HOT 1
- mocking fs when using rewire
- Rewire dosn't mock function
- A behaviour that could have HOT 1
- Update the vulnerable packages HOT 6
- type error when rewire'ing const HOT 4
- Vulnerable dependancy eslint 7.32.0 HOT 1
- Vulnerability in dependency eslint HOT 2
- Feature request: ESM support
- Special handling for process.env? HOT 2
- Rewire on node module with shebang doesn't work.
- Eslint need to be updated to at least version 8.15.0 HOT 1
- Rewire is great, but it doesn't work for SonarQube HOT 1
- unpexpected : due to type definition in TS HOT 1
- nconf values are undefined when a module is rewire
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rewire.