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

Commit141cfec

Browse files
committed
introduced require.reset()
1 parentc6aacb4 commit141cfec

File tree

6 files changed

+68
-20
lines changed

6 files changed

+68
-20
lines changed

‎README.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ rewire does **not** load the file and eval the contents to emulate node's requir
1818
Installation
1919
------------
2020

21-
```npm install rewire```
21+
`npm install rewire`
2222

2323
-----------------------------------------------------------------
2424

@@ -45,8 +45,9 @@ var mockedModuleB = {},
4545
"path/to/moduleB.js": mockedModuleB
4646
};
4747

48-
// The rewired module will now use your mocks instead of fs and moduleB.js.
49-
// Just make sure that the path is exactly as in myModuleA.js required.
48+
// The rewired module will now use your mocks instead of fs
49+
// and moduleB.js. Just make sure that the path is exactly as
50+
// in myModuleA.js required.
5051
rewiredModule=rewire("./myModuleA.js", mocks);
5152

5253

@@ -70,7 +71,8 @@ var injections = {
7071
rewiredModule=rewire("./myModuleA.js",null, injections);
7172

7273
// You can also pass a script to inject
73-
rewiredModule=rewire("./myModuleA.js",null,"console.log('hellooo');");// prints "hellooo"
74+
rewiredModule=
75+
rewire("./myModuleA.js",null,"console.log('hellooo');");// prints "hellooo"
7476

7577

7678

@@ -88,22 +90,44 @@ rewiredModule.__.myPrivateVar2; // returns former private myPrivateVar2
8890
// Cache
8991
////////////////////////////////
9092
// By disabling the module cache the rewired module will not be cached.
91-
// Any later require()-calls within other modules will now return the original
92-
// module again instead of the rewired. Caching is enabled by default.
93-
rewire("./myModuleA.js",null,null,null,false)!==require("./myModuleA.js");// = true
93+
// Any require()-calls will now return the original module again instead
94+
// of the rewired. Caching is enabled by default.
95+
rewire("./myModuleA.js",null,null,null,false)!==
96+
require("./myModuleA.js");// = true
97+
98+
// This removes all rewired modules from require.cache.
99+
rewire.reset();
100+
// IMPORTANT: You should call this before every unit test to ensure
101+
// a clean test environment.
94102
```
95103

96104
-----------------------------------------------------------------
97105

98106
##API
99107

100-
**rewire(***filename, mocks, injections, leaks, cache***)**
108+
**rewire(***filename, mocks, injections, leaks, cache***)**
101109

102-
-*{!String} filename*: Path to the module that shall be rewired. Use it exactly like require().
103-
-*{Object} mocks (optional)*: An object with mocks. Keys should be the exactly the same like they're required in the target module. So if you write```require("../../myModules/myModuleA.js")``` you need to pass```{"../../myModules/myModuleA.js": myModuleAMock}```.
104-
-*{Object|String} injections (optional)*: If you pass an object, all keys of the object will be```var```s within the module. You can also eval a string.**Please note**: All scripts are injected at the end of the module. So if there is any code in your module that is executed during```require()```, your injected variables will be undefined at this point. For example: passing```{console: null}``` will cause all calls of```console.log()``` to throw an exception if they're executed during```require()```.
105-
-*{Array<String>} leaks (optional)*: An array with variable names that should be exported. These variables are accessible via```myModule.__```
106-
-*{Boolean=true} cache (optional)*: Indicates whether the rewired module should be cached by node so subsequent calls of```require()``` will return the rewired module. Further calls of```rewire()``` will always overwrite the cache.
110+
Returns the rewired module.
111+
112+
-*{!String} filename*: <br/>
113+
Path to the module that shall be rewired. Use it exactly like require().
114+
115+
-*{Object} mocks (optional)*: <br/>
116+
An object with mocks. Keys should be the exactly the same like they're required in the target module. So if you write`require("../../myModules/myModuleA.js")` you need to pass`{"../../myModules/myModuleA.js": myModuleAMock}`.
117+
118+
-*{Object|String} injections (optional)*: <br />
119+
If you pass an object, all keys of the object will be`var`s within the module. You can also eval a string.**Please note**: All scripts are injected at the end of the module. So if there is any code in your module that is executed during`require()`, your injected variables will be undefined at this point. For example: passing`{console: null}` will cause all calls of`console.log()` to throw an exception if they're executed during`require()`.
120+
121+
-*{Array&lt;String&gt;} leaks (optional)*: <br/>
122+
An array with variable names that should be exported. These variables are accessible via`myModule.__`
123+
124+
125+
-*{Boolean=true} cache (optional)*: <br />
126+
Indicates whether the rewired module should be cached by node so subsequent calls of`require()` will return the rewired module. Further calls of`rewire()` will always overwrite the cache.
127+
128+
**rewire.reset()**
129+
130+
Removes all rewired modules from`require.cache`. Every`require()` will now return the original module again. <br />**Please note:** You should call this before every unit test to ensure a clean test environment.
107131

108132
-----------------------------------------------------------------
109133

‎lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ function rewire(request, mocks, injections, leaks, cache) {
2323
returnrewireModule(module.parent,request,mocks,injections,leaks,cache);
2424
}
2525

26+
rewire.reset=rewireModule.reset;
27+
2628
module.exports=rewire;

‎lib/rewire.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ var Module = require("module"),
44
nodeWrapper0=Module.wrapper[0],// caching original wrapper
55
nodeWrapper1=Module.wrapper[1],
66
getLeakingSrc=require("./getLeakingSrc.js"),
7-
getInjectionSrc=require("./getInjectionSrc.js");
7+
getInjectionSrc=require("./getInjectionSrc.js"),
8+
rewiredModules=[];
89

910
functionrestoreOriginalWrappers(){
1011
Module.wrapper[1]=nodeWrapper1;
@@ -13,7 +14,7 @@ function restoreOriginalWrappers() {
1314
/**
1415
* Does actual rewiring the module. For further documentation @see index.js
1516
*/
16-
module.exports=functiondoRewire(parentModule,filename,mocks,injections,leaks,cache){
17+
functionrewire(parentModule,filename,mocks,injections,leaks,cache){
1718
vartestModule,
1819
nodeRequire,
1920
wrapperExtensions="";
@@ -58,9 +59,22 @@ module.exports = function doRewire(parentModule, filename, mocks, injections, le
5859

5960
if(cache){
6061
require.cache[filename]=testModule;
62+
rewiredModules.push(filename);// save in private cache for .reset()
6163
}
6264

6365
restoreOriginalWrappers();// this is only necessary if nothing has been required within the module
6466

6567
returntestModule.exports;
66-
};
68+
}
69+
70+
rewire.reset=function(){
71+
vari;
72+
73+
for(i=0;i<rewiredModules.length;i++){
74+
deleterequire.cache[rewiredModules[i]];
75+
}
76+
77+
rewiredModules=[];
78+
};
79+
80+
module.exports=rewire;

‎test/getInjectionSrc.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
varexpect=require("expect.js"),
44
getInjectionSrc=require("../lib/getInjectionSrc.js");
55

6-
describe("#getMonkeyPatchSrc",function(){
6+
describe("getMonkeyPatchSrc",function(){
77
it("should return ''",function(){
88
varexpectedSrc="",
99
subject={};

‎test/getLeakingSrc.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
varexpect=require("expect.js"),
44
getLeakingWrapper=require("../lib/getLeakingSrc.js");
55

6-
describe("#getLeakingWrapper",function(){
6+
describe("getLeakingWrapper",function(){
77
it("should return 'exports.__ = {};'",function(){
88
expect(getLeakingWrapper([])).to.be("exports.__ = {};");
99
});

‎test/rewire.test.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ var testModules = [
1111
path.resolve(__dirname,"./testModules/C/moduleC.js")
1212
];
1313

14-
1514
functioncleanRequireCache(){
1615
vari;
1716

@@ -20,7 +19,7 @@ function cleanRequireCache() {
2019
}
2120
}
2221

23-
describe("#rewire",function(){
22+
describe("rewire",function(){
2423
beforeEach(cleanRequireCache);// ensuring a clean test environment
2524
it("should work like require() when omitting all other params",function(){
2625
expect(rewire("./testModules/A/moduleA.js")).to.be(require("./testModules/A/moduleA.js"));
@@ -120,4 +119,13 @@ describe("#rewire", function () {
120119
moduleB=require("./testModules/A/moduleA.js");// if restoring the original node require didn't worked, the mock would be applied now
121120
expect(moduleB.c).not.to.be(moduleCMock);
122121
});
122+
describe("#reset",function(){
123+
it("should remove all rewired modules from cache",function(){
124+
varrewired=rewire("./testModules/B/moduleB.js");
125+
126+
expect(require("./testModules/B/moduleB.js")).to.be(rewired);
127+
rewire.reset();
128+
expect(require("./testModules/B/moduleB.js")).not.to.be(rewired);
129+
});
130+
});
123131
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp