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

Commit3cc25ae

Browse files
author
Johannes
committed
- All tests running
- Added changelog
1 parentc5d8fab commit3cc25ae

20 files changed

+513
-489
lines changed

‎CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
##Changelog
2+
3+
###v1.0.0
4+
- Removed caching functionality. Now rewire doesn't modify`require.cache` at all.
5+
- Added support for[webpack](https://github.com/webpack/webpack)-bundler
6+
- Moved browserify-middleware from`rewire.browserify` to`rewire.bundlers.browserify`
7+
- Reached stable state :)

‎README.md

Lines changed: 129 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,129 @@
1-
rewire
2-
=====
3-
**Dependency injection for node.js applications**.
4-
5-
rewire adds a special setter and getter to modules so you can modify their behaviour for better unit testing. You may
6-
7-
- introduce mocks for other modules
8-
- leak private variables
9-
- override variables within the module.
10-
11-
rewire does**not** load the file and eval the contents to emulate node's require mechanism. In fact it uses node's own require to load the module. Thus your module behaves exactly the same in your test environment as under regular circumstances (except your modifications).
12-
13-
**Debugging is fully supported.**
14-
15-
Furthermore rewire comes also with support for[browserify](https://github.com/substack/node-browserify). You just
16-
have to add rewire as a middleware (see below).
17-
18-
[![Build Status](https://secure.travis-ci.org/jhnns/rewire.png?branch=master)](http://travis-ci.org/jhnns/rewire)
19-
20-
<br />
21-
22-
Installation
23-
------------
24-
25-
`npm install rewire`
26-
27-
**For older node versions:**<br />
28-
rewire is tested with node 0.6.x - 0.8.x. I recommend to run the unit tests via`mocha` in the rewire-folder before
29-
using rewire with other node versions.
30-
31-
**Use with[browserify](https://github.com/substack/node-browserify):**<br />
32-
33-
```javascript
34-
var b=require("browserify")({debug:true});
35-
36-
b.use(require("rewire").browserify);
37-
```
38-
39-
After that rewire works exactly as in node.
40-
41-
<br />
42-
43-
Examples
44-
--------
45-
46-
```javascript
47-
var rewire=require("rewire");
48-
49-
50-
// rewire acts exactly like require.
51-
var myModule=rewire("../lib/myModule.js");
52-
53-
54-
// Your module will now export a special setter and getter for private variables.
55-
myModule.__set__("myPrivateVar",123);
56-
myModule.__get__("myPrivateVar");// = 123
57-
58-
59-
// This allows you to mock almost everything within the module e.g. the fs-module.
60-
// Just pass the variable name as first parameter and your mock as second.
61-
myModule.__set__("fs", {
62-
readFile:function (path,encoding,cb) {
63-
cb(null,"Success!");
64-
}
65-
});
66-
myModule.readSomethingFromFileSystem(function (err,data) {
67-
console.log(data);// = Success!
68-
});
69-
70-
71-
// All later requires will now return the module with the mock.
72-
myModule===require("./myModule.js");// = true
73-
74-
75-
// You can set different variables with one call.
76-
myModule.__set__({
77-
fs: fsMock,
78-
http: httpMock,
79-
someOtherVar:"hello"
80-
});
81-
82-
83-
// You may also override globals. These changes are only within the module, so
84-
// you don't have to be concerned that other modules are influenced by your mock.
85-
myModule.__set__({
86-
console: {
87-
log:function () {/* be quiet*/ }
88-
},
89-
process: {
90-
argv: ["testArg1","testArg2"]
91-
}
92-
});
93-
94-
95-
// But be careful, if you do something like this you'll change your global
96-
// console instance.
97-
myModule.__set__("console.log",function () {/* be quiet*/ });
98-
99-
100-
// By getting private variables you can test for instance if your
101-
// module is in a specific state
102-
assert.ok(myModule.__get__("currentState")==="idle");
103-
104-
105-
// You can also disable caching when loading the rewired module. All
106-
// subsequent calls of require() will than return the original module again.
107-
rewire("./myModule.js")===require("./myModule.js");// = true
108-
rewire("./myModule.js",false)===require("./myModule.js");// = false
109-
110-
111-
// Every call of rewire returns a new instance and overwrites the old
112-
// one in the module cache.
113-
rewire("./myModule.js")===rewire("./myModule.js");// = false
114-
115-
116-
// If you want to remove all your rewired modules from
117-
// cache just call rewire.reset().
118-
// Do this after every single unit test to ensure a clean testing environment.
119-
rewire.reset();
120-
```
121-
122-
<br />
123-
124-
##API
125-
126-
**rewire(***filename, cache***): {RewiredModule}**
127-
128-
-*{!String} filename*: <br/>
129-
Path to the module that shall be rewired. Use it exactly like require().
130-
131-
-*{Boolean=true} cache (optional)*: <br />
132-
Indicates whether the rewired module should be cached by node so subsequent calls of`require()` will
133-
return the rewired module. Further calls of`rewire()` will always overwrite the cache.
134-
135-
**rewire.reset()**
136-
137-
Removes all rewired modules from`require.cache`. Every`require()` will now return the original module again.
138-
139-
**RewiredModule.&#95;&#95;set&#95;&#95;(***name, value***)**
140-
141-
-*{!String} name*: <br/>
142-
Name of the variable to set. The variable should be a global or defined with`var` in the top-level
143-
scope of the module.
144-
145-
-*{&lowast;} value*: <br/>
146-
The value to set
147-
148-
**RewiredModule.&#95;&#95;set&#95;&#95;(***env***)**
149-
150-
-*{!Object} env*: <br/>
151-
Takes all keys as variable names and sets the values respectively.
152-
153-
**RewiredModule.&#95;&#95;get&#95;&#95;(***name***): {&lowast;}**
154-
155-
Returns the private variable.
156-
157-
<br />
158-
159-
##Credits
160-
161-
This module is inspired by the great[injectr](https://github.com/nathanmacinnes/injectr"injectr")-module.
1+
rewire
2+
=====
3+
**Dependency injection for node.js applications**.
4+
5+
rewire adds a special setter and getter to modules so you can modify their behaviour for better unit testing. You may
6+
7+
- inject mocks for other modules
8+
- leak private variables
9+
- override variables within the module.
10+
11+
rewire does**not** load the file and eval the contents to emulate node's require mechanism. In fact it uses node's own require to load the module. Thus your module behaves exactly the same in your test environment as under regular circumstances (except your modifications).
12+
13+
Furthermore rewire comes also with support for various client-side bundlers (see[below](#client-side-bundlers)).
14+
15+
[![Build Status](https://secure.travis-ci.org/jhnns/rewire.png?branch=master)](http://travis-ci.org/jhnns/rewire)
16+
17+
<br />
18+
19+
Installation
20+
------------
21+
22+
`npm install rewire`
23+
24+
<br />
25+
26+
Examples
27+
--------
28+
29+
```javascript
30+
var rewire=require("rewire");
31+
32+
33+
// rewire acts exactly like require.
34+
var myModule=rewire("../lib/myModule.js");
35+
36+
37+
// Just with one difference:
38+
// Your module will now export a special setter and getter for private variables.
39+
myModule.__set__("myPrivateVar",123);
40+
myModule.__get__("myPrivateVar");// = 123
41+
42+
43+
// This allows you to mock almost everything within the module e.g. the fs-module.
44+
// Just pass the variable name as first parameter and your mock as second.
45+
myModule.__set__("fs", {
46+
readFile:function (path,encoding,cb) {
47+
cb(null,"Success!");
48+
}
49+
});
50+
myModule.readSomethingFromFileSystem(function (err,data) {
51+
console.log(data);// = Success!
52+
});
53+
54+
55+
// You can set different variables with one call.
56+
myModule.__set__({
57+
fs: fsMock,
58+
http: httpMock,
59+
someOtherVar:"hello"
60+
});
61+
62+
63+
// You may also override globals. These changes are only within the module, so
64+
// you don't have to be concerned that other modules are influenced by your mock.
65+
myModule.__set__({
66+
console: {
67+
log:function () {/* be quiet*/ }
68+
},
69+
process: {
70+
argv: ["testArg1","testArg2"]
71+
}
72+
});
73+
74+
75+
// But be careful, if you do something like this you'll change your global
76+
// console instance.
77+
myModule.__set__("console.log",function () {/* be quiet*/ });
78+
79+
// There is another difference to require:
80+
// Every call of rewire() returns a new instance.
81+
rewire("./myModule.js")===rewire("./myModule.js");// = false
82+
```
83+
84+
<br />
85+
86+
##API
87+
88+
###rewire(filename): rewiredModule
89+
90+
-*filename*: <br/>
91+
Path to the module that shall be rewired. Use it exactly like require().
92+
93+
###rewiredModule.&#95;&#95;set&#95;&#95;(name, value)
94+
95+
-*name*: <br/>
96+
Name of the variable to set. The variable should be global or defined with`var` in the top-leve scope of the module.
97+
-*value*: <br/>
98+
The value to set.
99+
100+
###rewiredModule.&#95;&#95;set&#95;&#95;(env)
101+
-*env*: <br/>
102+
Takes all keys as variable names and sets the values respectively.
103+
104+
###rewiredModule.&#95;&#95;get&#95;&#95;(name): value
105+
106+
Returns the private variable.
107+
108+
<br />
109+
110+
##Client-Side Bundlers
111+
Since rewire relies heavily on node's require mechanism it can't be used on the client-side without adding special middleware to the bundling process. Currently supported bundlers are:
112+
113+
-[browserify](https://github.com/substack/node-browserify)
114+
-[webpack](https://github.com/webpack/webpack)
115+
116+
###browserify
117+
118+
```javascript
119+
var b=browserify();
120+
b.use(require("rewire").bundlers.browserify);
121+
```
122+
123+
###webpack
124+
125+
```javascript
126+
var options= {};
127+
128+
require("rewire").bundlers.webpack(options);
129+
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp