mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-09-09 11:13:57 +08:00
Merge pull request #8212 from borislavjivkov/rewire
Added rewire (https://github.com/jhnns/rewire) definitions
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
/// <reference path="rewire.d.ts" />
|
||||
|
||||
var myModule = rewire("../lib/myModule.js");
|
||||
|
||||
myModule.__set__("path", "/dev/null");
|
||||
myModule.__get__("path"); // = '/dev/null'
|
||||
|
||||
var fsMock = {
|
||||
readFile: function (path: string, encoding: string, cb: Function) {
|
||||
cb(null, "Success!");
|
||||
}
|
||||
};
|
||||
myModule.__set__("fs", fsMock);
|
||||
|
||||
myModule.__set__({
|
||||
fs: fsMock,
|
||||
path: "/dev/null"
|
||||
});
|
||||
|
||||
myModule.__set__({
|
||||
console: {
|
||||
log: function () { /* be quiet */ }
|
||||
},
|
||||
process: {
|
||||
argv: ["testArg1", "testArg2"]
|
||||
}
|
||||
});
|
||||
|
||||
var revert = myModule.__set__("port", 3000);
|
||||
|
||||
// port is now 3000
|
||||
revert();
|
||||
// port is now the previous value
|
||||
|
||||
myModule.__with__({
|
||||
port: 3000
|
||||
})(function () {
|
||||
// within this function port is 3000
|
||||
});
|
||||
// now port is the previous value again
|
||||
|
||||
myModule.__with__({
|
||||
port: 3000
|
||||
})(function () {
|
||||
}).then(function () {
|
||||
// now port is the previous value again
|
||||
});
|
||||
// port is still 3000 here because the promise hasn't been resolved yet
|
||||
Vendored
+38
@@ -0,0 +1,38 @@
|
||||
// Type definitions for rewire v2.5.1
|
||||
// Project: https://github.com/jhnns/rewire
|
||||
// Definitions by: Borislav Zhivkov <https://github.com/borislavjivkov>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
declare module RewireInterfaces {
|
||||
interface Rewire {
|
||||
/**
|
||||
* Returns a rewired version of the module found at filename. Use rewire() exactly like require().
|
||||
*/
|
||||
(filename: string): RewiredModule;
|
||||
}
|
||||
|
||||
interface RewiredModule {
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
__set__(obj: Object): Function;
|
||||
/**
|
||||
* Sets the internal variable name to the given value. Returns a function which can be called to revert the change.
|
||||
*/
|
||||
__set__(name: string, value: any): Function;
|
||||
/**
|
||||
* Returns the private variable with the given name.
|
||||
*/
|
||||
__get__(name: string): any;
|
||||
/**
|
||||
* 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
|
||||
* the promise has been resolved or rejected. For your convenience the returned function passes the received promise through.
|
||||
*/
|
||||
__with__(obj: Object): (callback: Function) => any;
|
||||
}
|
||||
}
|
||||
|
||||
declare var rewire: RewireInterfaces.Rewire;
|
||||
declare module "rewire" {
|
||||
export = rewire;
|
||||
}
|
||||
Reference in New Issue
Block a user