Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb4b2794

Browse files
committed
changed repo-name to rewire
1 parentd4eea33 commitb4b2794

File tree

9 files changed

+229
-128
lines changed

9 files changed

+229
-128
lines changed

‎README.md

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,61 @@
1-
trick
2-
=====
3-
4-
Dependency injection for node.js applications
1+
rewire
2+
=====
3+
**Dependency injection for node.js applications**.
4+
5+
rewire allows you to modify the behaviour of modules for better unit testing. You may
6+
7+
- provide mocks for other modules
8+
- leak private variables
9+
- override variables within the module
10+
- inject scripts
11+
12+
rewire does**not** load the file and eval it to emulate node's require mechanism. In fact it uses node's require to load the module. Thus your module behaves exactly the same in your test environment as under regular circumstances (except your modifications).
13+
14+
Installation
15+
------------
16+
17+
```npm install rewire```
18+
19+
Examples
20+
--------
21+
22+
```javascript
23+
var rewire=require("rewire"),
24+
rewiredModule;
25+
26+
// Default
27+
////////////////////////////////
28+
// rewire acts exactly like require when omitting all other params
29+
rewiredModule=rewire("./myModuleA.js");
30+
31+
// Mocks
32+
////////////////////////////////
33+
var mockedModuleB= {},
34+
mocks= {
35+
"path/to/moduleB.js": mockedModuleB
36+
};
37+
38+
// the rewired module will now use your mock instead of moduleB.js.
39+
rewiredModule=rewire("./myModuleA.js", mocks);
40+
41+
42+
// Injections
43+
////////////////////////////////
44+
var injections= {
45+
console: {
46+
log:function () {/* be quiet*/ }
47+
},
48+
process: { argv: ["someArgs"] },
49+
__filename:"some/other/dir"
50+
};
51+
52+
// overrides all given variables within the module
53+
rewiredModule=rewire("./myModuleA.js",null, injections);
54+
// you can also pass a script to inject
55+
rewiredModule=rewire("./myModuleA.js",null,"console.log('hellooo');");
56+
57+
58+
// Leaks
59+
////////////////////////////////
60+
61+
```

‎lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";// run code in ES5 strict mode
22

3-
vartrick=require("./trick.js");
3+
varrewire=require("./rewire.js");
44

55
module.exports=function(request,mocks,injections,leaks,cache){
66
deleterequire.cache[__filename];// deleting self from module cache so the parent module is always up to date
@@ -9,5 +9,5 @@ module.exports = function (request, mocks, injections, leaks, cache) {
99
cache=true;
1010
}
1111

12-
returntrick(module.parent,request,mocks,injections,leaks,cache);
12+
returnrewire(module.parent,request,mocks,injections,leaks,cache);
1313
};

‎lib/trick.jsrenamed to‎lib/rewire.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ var Module = require("module"),
77
getInjectionSrc=require("./getInjectionSrc.js");
88

99
functionrestoreOriginalWrappers(){
10-
Module.wrapper[0]=nodeWrapper0;
1110
Module.wrapper[1]=nodeWrapper1;
1211
}
1312

14-
functiontrick(parentModule,filename,mocks,injections,leaks,cache){
13+
functionrewire(parentModule,filename,mocks,injections,leaks,cache){
1514
vartestModule,
16-
nodeRequire;
15+
nodeRequire,
16+
wrapperExtensions="";
1717

1818
functionrequireTrick(path){
1919
restoreOriginalWrappers();// we need to restore the wrappers now so we don't influence other modules
@@ -37,15 +37,16 @@ function trick(parentModule, filename, mocks, injections, leaks, cache) {
3737

3838
// Prepare module for injection
3939
if(typeofinjections==="object"){
40-
Module.wrapper[0]=nodeWrapper0+getInjectionSrc(injections);
40+
wrapperExtensions+=getInjectionSrc(injections);
4141
}elseif(typeofinjections==="string"){
42-
Module.wrapper[0]=nodeWrapper0+injections;
42+
wrapperExtensions+=injections;
4343
}
4444

4545
// Prepare module for leaking private vars
4646
if(Array.isArray(leaks)){
47-
Module.wrapper[1]=getLeakingSrc(leaks)+nodeWrapper1;
47+
wrapperExtensions+=getLeakingSrc(leaks);
4848
}
49+
Module.wrapper[1]=wrapperExtensions+nodeWrapper1;
4950

5051
// Mocking module.require-function
5152
testModule.require=requireTrick;
@@ -61,4 +62,4 @@ function trick(parentModule, filename, mocks, injections, leaks, cache) {
6162
returntestModule.exports;
6263
}
6364

64-
module.exports=trick;
65+
module.exports=rewire;

‎package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name" :"trick",
2+
"name" :"rewire",
33
"version" :"0.1.0",
44
"description" :"Dependency injection for node.js applications",
55
"keywords" : [
@@ -17,15 +17,15 @@
1717
"main" :"lib/index.js",
1818
"bugs" : {
1919
"email" :"mail@johannesewald.de",
20-
"url" :"https://github.com/jhnns/trick/issues"
20+
"url" :"https://github.com/jhnns/rewire/issues"
2121
},
2222
"licenses" : {
2323
"type" :"MIT",
2424
"url" :"http: //www.opensource.org/licenses/mit-license.php"
2525
},
2626
"repositories" : {
2727
"type" :"git",
28-
"url" :"https://github.com/jhnns/trick"
28+
"url" :"https://github.com/jhnns/rewire"
2929
},
3030
"engines" : {
3131
"node" :"0.6.x"

‎test/debug.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";// run code in ES5 strict mode
2+
3+
varrewire=require("../lib/index.js");
4+
5+
// add breakpoints in testModules/debuggerModule.js and debug this file with your IDE to
6+
// check if debugging works with rewire.
7+
vardebuggerModule=rewire("./testModules/debuggerModule.js",null,{
8+
someVar:"Look if you can see me in your IDE when holding at the breakpoints"
9+
});
10+
11+
debuggerModule();

‎test/rewire.test.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"use strict";// run code in ES5 strict mode
2+
3+
varpath=require("path"),
4+
expect=require("expect.js"),
5+
rewire=require("../lib/index.js");
6+
7+
vartestModules=[
8+
path.resolve(__dirname,"./testModules/index.js"),
9+
path.resolve(__dirname,"./testModules/A/moduleA.js"),
10+
path.resolve(__dirname,"./testModules/B/moduleB.js"),
11+
path.resolve(__dirname,"./testModules/C/moduleC.js")
12+
];
13+
14+
15+
functioncleanRequireCache(){
16+
vari;
17+
18+
for(i=0;i<testModules.length;i++){
19+
deleterequire.cache[testModules[i]];
20+
}
21+
}
22+
23+
describe("#rewire",function(){
24+
beforeEach(cleanRequireCache);// ensuring a clean test environment
25+
it("should work like require() when omitting all other params",function(){
26+
expect(rewire("./testModules/A/moduleA.js")).to.be(require("./testModules/A/moduleA.js"));
27+
});
28+
it("should require all mocks",function(){
29+
varrewired,
30+
fsMock={},
31+
mocks={},
32+
moduleBMock={},
33+
moduleCMock={},
34+
toSrcMock={},
35+
indexMock={};
36+
37+
mocks["fs"]=fsMock;
38+
mocks[path.resolve(__dirname,"./testModules/B/moduleB.js")]=moduleBMock;
39+
mocks["../C/moduleC.js"]=moduleCMock;
40+
mocks["toSrc"]=toSrcMock;
41+
mocks["../"]=indexMock;
42+
43+
rewired=rewire("./testModules/A/moduleA.js",mocks);
44+
expect(rewired.fs).to.be(fsMock);
45+
expect(rewired.b).to.be(moduleBMock);
46+
expect(rewired.c).to.be(moduleCMock);
47+
expect(rewired.toSrc).to.be(toSrcMock);
48+
expect(rewired.index).to.be(indexMock);
49+
});
50+
it("should inject object modifications",function(){
51+
varrewired,
52+
injections={
53+
process:{
54+
argv:["arg1","arg2","arg3"]
55+
},
56+
console:123
57+
};
58+
59+
rewired=rewire("./testModules/A/moduleA.js",null,injections);
60+
rewired.exportAll();
61+
expect(rewired.process).not.to.be(process);
62+
expect(process.argv).not.to.eql(injections.process.argv);
63+
expect(rewired.process).to.eql(injections.process);
64+
expect(rewired.console).to.be(123);
65+
});
66+
it("should inject custom scripts",function(){
67+
varrewired,
68+
script="var console = 456;";
69+
70+
rewired=rewire("./testModules/A/moduleA.js",null,script);
71+
rewired.exportAll();
72+
expect(rewired.console).to.be(456);
73+
});
74+
it("should leak private variables",function(){
75+
varrewired,
76+
leaks=["myPrivateVar"];
77+
78+
rewired=rewire("./testModules/A/moduleA.js",null,null,leaks);
79+
expect(rewired.__.myPrivateVar).to.be("Hello I'm very private");
80+
});
81+
it("should leak private functions",function(){
82+
varrewired,
83+
leaks=["myPrivateFunction"];
84+
85+
rewired=rewire("./testModules/A/moduleA.js",null,null,leaks);
86+
expect(rewired.__.myPrivateFunction()).to.be("Hello I'm very private");
87+
});
88+
it("should leak nothing on demand",function(){
89+
varrewired;
90+
91+
rewired=rewire("./testModules/A/moduleA.js");
92+
expect(rewired.__).to.be(undefined);
93+
});
94+
it("should cache the rewired module",function(){
95+
varrewired;
96+
97+
rewired=rewire("./testModules/B/moduleB.js");
98+
rewired.requireIndex();
99+
expect(rewired.index.b).to.be(rewired);
100+
cleanRequireCache();
101+
rewired=rewire("./testModules/B/moduleB.js",null,null,null,true);
102+
rewired.requireIndex();
103+
expect(rewired.index.b).to.be(rewired);
104+
});
105+
it("should not cache the rewired module on demand",function(){
106+
varrewired;
107+
108+
rewired=rewire("./testModules/B/moduleB.js",null,null,null,false);
109+
rewired.requireIndex();
110+
expect(rewired.index.b).not.to.be(rewired);
111+
});
112+
});

‎test/testModules/A/moduleA.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,22 @@ var path = require("path"),
1111

1212
varmyPrivateVar="Hello I'm very private";
1313

14-
// expose all required modules to test for mocks
15-
exports.fs=fs;
16-
exports.b=b;
17-
exports.c=c;
18-
exports.toSrc=toSrc;
19-
exports.index=index;
20-
exports.process=process;
21-
exports.console=console;
14+
functionmyPrivateFunction(){
15+
return"Hello I'm very private";
16+
}
17+
18+
functionexportAll(){
19+
// expose all required modules to test for mocks
20+
exports.fs=fs;
21+
exports.b=b;
22+
exports.c=c;
23+
exports.toSrc=toSrc;
24+
exports.index=index;
25+
exports.process=process;
26+
exports.console=console;
27+
}
28+
29+
exportAll();
30+
exports.exportAll=exportAll;
31+
32+

‎test/testModules/debuggerModule.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";// run code in ES5 strict mode
2+
3+
// Add a breakpoint on line 6 and on line 11 and debug "debug.test.js" to test if the IDE stops at these points.
4+
// Watch also the variable someVar that is injected by rewire. It will be undefined at this point because
5+
// all injections are executed at the end of the module.
6+
// It's already visible because of hoisting: http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-javascript-hoisting-explained/)
7+
varsomeNumber=0;
8+
9+
module.exports=function(){
10+
// In this line someVar will be defined.
11+
someNumber++;
12+
someVar;
13+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp