Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Ewald <mail@johannesewald.de>2013-02-05 02:01:58 +0400
committerJohannes Ewald <mail@johannesewald.de>2013-02-05 02:01:58 +0400
commit7b21f0f1f0284aa6a935d0767709e294168c1268 (patch)
tree9f563964c049ada7c9163defe5f23f8c8e2da394 /README.md
parent9e5401917a1b59b5fe8bdc568a496e4a4c445331 (diff)
parent9dcb9bce1d7e15d2aa7e7b9cb9b0c07b3c4b7a71 (diff)
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'README.md')
-rw-r--r--README.md34
1 files changed, 27 insertions, 7 deletions
diff --git a/README.md b/README.md
index eb399e1..9def98b 100644
--- a/README.md
+++ b/README.md
@@ -26,20 +26,43 @@ Installation
Examples
--------
+Imagine you want to test this module:
+
```javascript
-var rewire = require("rewire");
+// lib/myModule.js
+
+// With rewire you can change all these variables
+var fs = require("fs"),
+ http = require("http"),
+ someOtherVar = "hi",
+ myPrivateVar = 1;
+
+function readSomethingFromFileSystem(cb) {
+ // But no scoped variables
+ var path = "/somewhere/on/the/disk";
+
+ console.log("Reading from file system ...");
+ fs.readFile(path, "utf8", cb);
+}
+
+exports.readSomethingFromFileSystem = readSomethingFromFileSystem;
+```
+
+Now within your test module:
+```javascript
+// test/myModule.test.js
+
+var rewire = require("rewire");
// rewire acts exactly like require.
var myModule = rewire("../lib/myModule.js");
-
// Just with one difference:
// Your module will now export a special setter and getter for private variables.
myModule.__set__("myPrivateVar", 123);
myModule.__get__("myPrivateVar"); // = 123
-
// This allows you to mock almost everything within the module e.g. the fs-module.
// Just pass the variable name as first parameter and your mock as second.
myModule.__set__("fs", {
@@ -51,7 +74,6 @@ myModule.readSomethingFromFileSystem(function (err, data) {
console.log(data); // = Success!
});
-
// You can set different variables with one call.
myModule.__set__({
fs: fsMock,
@@ -59,7 +81,6 @@ myModule.__set__({
someOtherVar: "hello"
});
-
// You may also override globals. These changes are only within the module, so
// you don't have to be concerned that other modules are influenced by your mock.
myModule.__set__({
@@ -71,7 +92,6 @@ myModule.__set__({
}
});
-
// But be careful, if you do something like this you'll change your global
// console instance.
myModule.__set__("console.log", function () { /* be quiet */ });
@@ -145,4 +165,4 @@ var webpackOptions = {
require("rewire").bundlers.webpack(webpackOptions);
webpack("entry.js", webpackOptions, function () {});
-``` \ No newline at end of file
+```