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

Commit436d8d8

Browse files
committed
improved docs
1 parent18ca74d commit436d8d8

File tree

1 file changed

+60
-8
lines changed

1 file changed

+60
-8
lines changed

‎README.md

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ rewire does **not** load the file and eval the contents to emulate node's requir
1414
**Debugging is fully supported.**
1515

1616
-----------------------------------------------------------------
17+
<br />
1718

1819
Installation
1920
------------
2021

2122
`npm install rewire`
2223

2324
-----------------------------------------------------------------
25+
<br />
2426

2527
Examples
2628
--------
@@ -102,40 +104,90 @@ rewire.reset();
102104
```
103105

104106
-----------------------------------------------------------------
107+
<br />
105108

106109
##API
107110

108111
**rewire(***filename, mocks, injections, leaks, cache***)**
109112

110-
Returns the rewired module.
111-
112113
-*{!String} filename*: <br/>
113114
Path to the module that shall be rewired. Use it exactly like require().
114115

115116
-*{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+
An object with mocks.
117118

118119
-*{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+
If you pass an object, all keys of the object will be`var`s within the module. You can also eval a string.
120121

121122
-*{Array&lt;String&gt;} leaks (optional)*: <br/>
122-
An array with variable names that should be exported. These variables are accessible via`myModule.__`
123-
123+
An array with variable names that should be exported. These variables are accessible via`myModule.__`.
124124

125125
-*{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.
126+
Indicates whether the rewired module should be cached by node so subsequent calls of`require()` will
127+
return the rewired module. Further calls of`rewire()` will always overwrite the cache.
128+
129+
Returns the rewired module.
127130

128131
**rewire.reset()**
129132

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.
133+
Removes all rewired modules from`require.cache`. Every`require()` will now return the original module again.
134+
135+
-----------------------------------------------------------------
136+
<br />
137+
138+
##Please note
139+
###mocks
140+
Keys should be the exactly the same like they're required in the target module.
141+
So if you write`require("../../myModules/myModuleA.js")` you need to pass
142+
`{"../../myModules/myModuleA.js": myModuleAMock}`.
143+
144+
###injections
145+
All scripts are injected at the end of the module. So if there is any code in your module
146+
that is executed during`require()`, your injected variables will be undefined at this point.
147+
148+
Imagine`rewire("./myModule.js", null, {console: null});`:
149+
150+
```javascript
151+
console.log("Hello");// ouch, that won't work. console is undefined at this point because of hoisting
152+
153+
// End of module ///////////////
154+
// rewire will inject here
155+
varconsole=null;
156+
```
157+
158+
###leaks
159+
Leaks are executed at the end of the module. If a`var` is undefined at this point you
160+
won't be able to access the leak (because`undefined`-values are[copied by value](http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language)).
161+
A good approach to this is:
162+
163+
```javascript
164+
var myLeaks= {};
165+
166+
module.exports=function (someValue) {
167+
myLeaks.someValue= someValue;
168+
};
169+
170+
// End of module ///////////////
171+
// rewire will inject here
172+
module.exports.__= {myLeaks: myLeaks};
173+
```
174+
175+
Because```myLeaks``` is defined at the end of the module, you're able to access the leak object and all leaks that
176+
are attached to it later during runtime. Because myLeaks is not exposed under regular circumstances your
177+
module interface stays clean.
178+
179+
###reset
180+
You should call this before every unit test to ensure a clean test environment.
131181

132182
-----------------------------------------------------------------
183+
<br />
133184

134185
##Credits
135186

136187
This module is inspired by the great[injectr](https://github.com/nathanmacinnes/injectr"injectr")-module.
137188

138189
-----------------------------------------------------------------
190+
<br />
139191

140192
##License
141193

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp