From 4cc4a2acc62850612f6c558ad2df12bd8823f49a Mon Sep 17 00:00:00 2001 From: borislavjivkov Date: Mon, 22 Feb 2016 23:30:55 +0200 Subject: [PATCH] Added rewire (https://github.com/jhnns/rewire) definitions --- rewire/rewire-tests.ts | 48 ++++++++++++++++++++++++++++++++++++++++++ rewire/rewire.d.ts | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 rewire/rewire-tests.ts create mode 100644 rewire/rewire.d.ts diff --git a/rewire/rewire-tests.ts b/rewire/rewire-tests.ts new file mode 100644 index 000000000..3e81050e4 --- /dev/null +++ b/rewire/rewire-tests.ts @@ -0,0 +1,48 @@ +/// + +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 diff --git a/rewire/rewire.d.ts b/rewire/rewire.d.ts new file mode 100644 index 000000000..78ece94ee --- /dev/null +++ b/rewire/rewire.d.ts @@ -0,0 +1,38 @@ +// Type definitions for rewire v2.5.1 +// Project: https://github.com/jhnns/rewire +// Definitions by: Borislav Zhivkov +// 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; +}