Comments (7)
rewire adds the setters and getters to the exports
-object at the bottom of the module. Just imagine that rewire injects code at the bottom of your module that looks like:
exports.__set__ = function () { ... };
exports.__get__ = function () { ... };
So if you do module.exports = helper
, the setters and getters will be attached to the helper
-function itself, not to the object returned by calling helper(app)
.
from rewire.
Does that mean the only way to be able to use rewire on a module is to
export an object and not export a function ? Do you know of any way to use
rewire with the module pattern ?
On Nov 7, 2013 6:47 PM, "Johannes Ewald" [email protected] wrote:
rewire adds the setters and getters to the exports-object at the bottom
of the module. Just imagine that rewire injects code at the bottom of your
module that looks like:exports.set = function () { ... }:exports.get = function () { ... };
So if you do module.exports = helper, the setters and getters will be
attached to the helper-object, not to the object returned by calling
helper(app).—
Reply to this email directly or view it on GitHubhttps://github.com//issues/22#issuecomment-27961747
.
from rewire.
rewire still works when exporting a function. If you place your variables you want to mock in the top-level scope of the module, it should be no problem. Given this module:
var http = require("http"),
fs = require("fs");
function helper(app) {
return {
get_watch_list: function () { ... }
update_watch_list: function (r_ip) { ... }
};
}
module.exports = helper;
you can still inject mocks for the http
- and the fs
-module. Just write
var helper = rewire ("./helper");
helper.__set__("http", { ... });
helper.__set__("fs", { ... });
from rewire.
@gprasant You can but you can only interact with variables at the "top" level. You can't override http.get but you can override the http object to have a custom get function.
I put together a sample on c9 where I override http.get here that might help:
https://c9.io/dposin/jasmine-node-rewire
Also, I have a post that walks through rewire and jasmine-node:
http://randomjavascript.blogspot.com/2013/10/jasmine-node-and-rewire-part-1.html
Hope this helps.
from rewire.
Thanks @Lastalas .
var helper = rewire ("./helper");
helper.__set__("http", { ... });
helper.__set__("fs", { ... });
# use test_helper for calling functions & user helper for __set__ and __get__ .
test_helper = helper(app_stub);
This is the style Im going ahead with. Looks good? Better Ideas?
from rewire.
Looks good! 👍
Thanks @Lastalas for your detailed blog post. Wish I had more time to write such posts...
Just found two things:
- the link to rewire isn't working
- you don't need the
__dirname
-thing inrewire(__dirname + '/server.js')
. rewire works just like require and will resolve any relative paths to the current module
from rewire.
@jhnns I fixed the link. thanks for spotting that.
The __dirname was because of the way c9 handles the actual file structure. I'll look and see if I can remove it.
from rewire.
Related Issues (20)
- 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
- Node test coverage fails with rewire 7.0.0
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.