Skip to content

Commit dab4bb8

Browse files
committed
Merge pull request DefinitelyTyped#8212 from borislavjivkov/rewire
Added rewire (https://github.com/jhnns/rewire) definitions
2 parents dade441 + 4cc4a2a commit dab4bb8

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

rewire/rewire-tests.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/// <reference path="rewire.d.ts" />
2+
3+
var myModule = rewire("../lib/myModule.js");
4+
5+
myModule.__set__("path", "/dev/null");
6+
myModule.__get__("path"); // = '/dev/null'
7+
8+
var fsMock = {
9+
readFile: function (path: string, encoding: string, cb: Function) {
10+
cb(null, "Success!");
11+
}
12+
};
13+
myModule.__set__("fs", fsMock);
14+
15+
myModule.__set__({
16+
fs: fsMock,
17+
path: "/dev/null"
18+
});
19+
20+
myModule.__set__({
21+
console: {
22+
log: function () { /* be quiet */ }
23+
},
24+
process: {
25+
argv: ["testArg1", "testArg2"]
26+
}
27+
});
28+
29+
var revert = myModule.__set__("port", 3000);
30+
31+
// port is now 3000
32+
revert();
33+
// port is now the previous value
34+
35+
myModule.__with__({
36+
port: 3000
37+
})(function () {
38+
// within this function port is 3000
39+
});
40+
// now port is the previous value again
41+
42+
myModule.__with__({
43+
port: 3000
44+
})(function () {
45+
}).then(function () {
46+
// now port is the previous value again
47+
});
48+
// port is still 3000 here because the promise hasn't been resolved yet

rewire/rewire.d.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Type definitions for rewire v2.5.1
2+
// Project: https://github.com/jhnns/rewire
3+
// Definitions by: Borislav Zhivkov <https://github.com/borislavjivkov>
4+
// Definitions: https://github.com/borisyankov/DefinitelyTyped
5+
6+
declare module RewireInterfaces {
7+
interface Rewire {
8+
/**
9+
* Returns a rewired version of the module found at filename. Use rewire() exactly like require().
10+
*/
11+
(filename: string): RewiredModule;
12+
}
13+
14+
interface RewiredModule {
15+
/**
16+
* Takes all enumerable keys of obj as variable names and sets the values respectively. Returns a function which can be called to revert the change.
17+
*/
18+
__set__(obj: Object): Function;
19+
/**
20+
* Sets the internal variable name to the given value. Returns a function which can be called to revert the change.
21+
*/
22+
__set__(name: string, value: any): Function;
23+
/**
24+
* Returns the private variable with the given name.
25+
*/
26+
__get__(name: string): any;
27+
/**
28+
* Returns a function which - when being called - sets obj, executes the given callback and reverts obj. If callback returns a promise, obj is only reverted after
29+
* the promise has been resolved or rejected. For your convenience the returned function passes the received promise through.
30+
*/
31+
__with__(obj: Object): (callback: Function) => any;
32+
}
33+
}
34+
35+
declare var rewire: RewireInterfaces.Rewire;
36+
declare module "rewire" {
37+
export = rewire;
38+
}

0 commit comments

Comments
 (0)