From 6abae6e969b629e8bd1a9006b7cceb1eaba2c1d8 Mon Sep 17 00:00:00 2001 From: Giorgio Delgado Date: Sun, 22 Oct 2017 19:37:47 -0400 Subject: [PATCH 1/2] update for v2 --- types/mock-require/index.d.ts | 32 +++++--------- types/mock-require/mock-require-tests.ts | 10 +++-- types/mock-require/tsconfig.json | 2 +- types/mock-require/tslint.json | 3 ++ types/mock-require/v1/index.d.ts | 31 ++++++++++++++ types/mock-require/v1/mock-require-tests.ts | 47 +++++++++++++++++++++ types/mock-require/v1/tsconfig.json | 23 ++++++++++ 7 files changed, 122 insertions(+), 26 deletions(-) create mode 100644 types/mock-require/tslint.json create mode 100644 types/mock-require/v1/index.d.ts create mode 100644 types/mock-require/v1/mock-require-tests.ts create mode 100644 types/mock-require/v1/tsconfig.json diff --git a/types/mock-require/index.d.ts b/types/mock-require/index.d.ts index da5db1c07c..75076ec739 100644 --- a/types/mock-require/index.d.ts +++ b/types/mock-require/index.d.ts @@ -1,31 +1,21 @@ -// Type definitions for mock-require v1.3.0 +// Type definitions for mock-require 2.0 // Project: https://github.com/boblauer/mock-require -// Definitions by: Daniel Pereira +// Definitions by: Giorgio Delgado // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 /// -/** Simple, intuitive mocking of Node.js modules. */ +type StubFunction = (...params: any[]) => any; +type Stub = object | StubFunction; + interface Mock { - /** - * @param {string} path The module you that you want to mock. - * @param {any} mockExport The function or object you want to be returned from require, instead of the path module's exports, or the module you want to be returned from require, instead of the path module's export. - */ - (path: string, mockExport: any | Function | string): void; - - /** - * @param {string} path The module you that you want to stop mocking. - */ + (path: string, mockExport: string | Stub): void; stop(path: string): void; - - /** This function can be used to remove all registered mocks without the need to remove them individually using mock.stop(). */ stopAll(): void; - - /** - * @param {string} path The file whose cache you want to refresh. - */ - reRequire(path: string): void; + reRequire(path: string): any; } -declare const myModule: Mock; -export = myModule; +declare var mock: Mock; + +export = mock; diff --git a/types/mock-require/mock-require-tests.ts b/types/mock-require/mock-require-tests.ts index 41052e2afc..2131bc56de 100644 --- a/types/mock-require/mock-require-tests.ts +++ b/types/mock-require/mock-require-tests.ts @@ -1,10 +1,12 @@ import mock = require('mock-require'); +const request = () => { + console.log('http.request called'); +}; + function testMock() { mock('http', { - request: function () { - console.log('http.request called'); - } + request }); const http = require('http'); @@ -42,4 +44,4 @@ function testReRequire() { let fileToTest = require('./fileToTest'); mock('fs', {}); // fileToTest is still using the unmocked fs module fileToTest = mock.reRequire('./fileToTest'); // fileToTest is now using your mock -} \ No newline at end of file +} diff --git a/types/mock-require/tsconfig.json b/types/mock-require/tsconfig.json index 2f5ec713da..1f6be404c6 100644 --- a/types/mock-require/tsconfig.json +++ b/types/mock-require/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ diff --git a/types/mock-require/tslint.json b/types/mock-require/tslint.json new file mode 100644 index 0000000000..e60c15844f --- /dev/null +++ b/types/mock-require/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} \ No newline at end of file diff --git a/types/mock-require/v1/index.d.ts b/types/mock-require/v1/index.d.ts new file mode 100644 index 0000000000..da5db1c07c --- /dev/null +++ b/types/mock-require/v1/index.d.ts @@ -0,0 +1,31 @@ +// Type definitions for mock-require v1.3.0 +// Project: https://github.com/boblauer/mock-require +// Definitions by: Daniel Pereira +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +/** Simple, intuitive mocking of Node.js modules. */ +interface Mock { + /** + * @param {string} path The module you that you want to mock. + * @param {any} mockExport The function or object you want to be returned from require, instead of the path module's exports, or the module you want to be returned from require, instead of the path module's export. + */ + (path: string, mockExport: any | Function | string): void; + + /** + * @param {string} path The module you that you want to stop mocking. + */ + stop(path: string): void; + + /** This function can be used to remove all registered mocks without the need to remove them individually using mock.stop(). */ + stopAll(): void; + + /** + * @param {string} path The file whose cache you want to refresh. + */ + reRequire(path: string): void; +} + +declare const myModule: Mock; +export = myModule; diff --git a/types/mock-require/v1/mock-require-tests.ts b/types/mock-require/v1/mock-require-tests.ts new file mode 100644 index 0000000000..2131bc56de --- /dev/null +++ b/types/mock-require/v1/mock-require-tests.ts @@ -0,0 +1,47 @@ +import mock = require('mock-require'); + +const request = () => { + console.log('http.request called'); +}; + +function testMock() { + mock('http', { + request + }); + + const http = require('http'); + http.request(); // 'http.request called' +} + +function testStop() { + mock('fs', { mockedFS: true }); + + const fs1 = require('fs'); + mock.stop('fs'); + + const fs2 = require('fs'); + fs1 === fs2; // false +} + +function testStopAll() { + mock('fs', {}); + mock('path', {}); + + const fs1 = require('fs'); + const path1 = require('path'); + + mock.stopAll(); + + const fs2 = require('fs'); + const path2 = require('path'); + + fs1 === fs2; // false + path1 === path2; // false +} + +function testReRequire() { + const fs = require('fs'); + let fileToTest = require('./fileToTest'); + mock('fs', {}); // fileToTest is still using the unmocked fs module + fileToTest = mock.reRequire('./fileToTest'); // fileToTest is now using your mock +} diff --git a/types/mock-require/v1/tsconfig.json b/types/mock-require/v1/tsconfig.json new file mode 100644 index 0000000000..1f6be404c6 --- /dev/null +++ b/types/mock-require/v1/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "mock-require-tests.ts" + ] +} \ No newline at end of file From 87364923db0d4ddc0cfb6f9498fb18c05ad5a2a9 Mon Sep 17 00:00:00 2001 From: Giorgio Delgado Date: Mon, 23 Oct 2017 21:06:40 -0400 Subject: [PATCH 2/2] remove v1 typedefs --- types/mock-require/v1/index.d.ts | 31 -------------- types/mock-require/v1/mock-require-tests.ts | 47 --------------------- types/mock-require/v1/tsconfig.json | 23 ---------- 3 files changed, 101 deletions(-) delete mode 100644 types/mock-require/v1/index.d.ts delete mode 100644 types/mock-require/v1/mock-require-tests.ts delete mode 100644 types/mock-require/v1/tsconfig.json diff --git a/types/mock-require/v1/index.d.ts b/types/mock-require/v1/index.d.ts deleted file mode 100644 index da5db1c07c..0000000000 --- a/types/mock-require/v1/index.d.ts +++ /dev/null @@ -1,31 +0,0 @@ -// Type definitions for mock-require v1.3.0 -// Project: https://github.com/boblauer/mock-require -// Definitions by: Daniel Pereira -// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped - -/// - -/** Simple, intuitive mocking of Node.js modules. */ -interface Mock { - /** - * @param {string} path The module you that you want to mock. - * @param {any} mockExport The function or object you want to be returned from require, instead of the path module's exports, or the module you want to be returned from require, instead of the path module's export. - */ - (path: string, mockExport: any | Function | string): void; - - /** - * @param {string} path The module you that you want to stop mocking. - */ - stop(path: string): void; - - /** This function can be used to remove all registered mocks without the need to remove them individually using mock.stop(). */ - stopAll(): void; - - /** - * @param {string} path The file whose cache you want to refresh. - */ - reRequire(path: string): void; -} - -declare const myModule: Mock; -export = myModule; diff --git a/types/mock-require/v1/mock-require-tests.ts b/types/mock-require/v1/mock-require-tests.ts deleted file mode 100644 index 2131bc56de..0000000000 --- a/types/mock-require/v1/mock-require-tests.ts +++ /dev/null @@ -1,47 +0,0 @@ -import mock = require('mock-require'); - -const request = () => { - console.log('http.request called'); -}; - -function testMock() { - mock('http', { - request - }); - - const http = require('http'); - http.request(); // 'http.request called' -} - -function testStop() { - mock('fs', { mockedFS: true }); - - const fs1 = require('fs'); - mock.stop('fs'); - - const fs2 = require('fs'); - fs1 === fs2; // false -} - -function testStopAll() { - mock('fs', {}); - mock('path', {}); - - const fs1 = require('fs'); - const path1 = require('path'); - - mock.stopAll(); - - const fs2 = require('fs'); - const path2 = require('path'); - - fs1 === fs2; // false - path1 === path2; // false -} - -function testReRequire() { - const fs = require('fs'); - let fileToTest = require('./fileToTest'); - mock('fs', {}); // fileToTest is still using the unmocked fs module - fileToTest = mock.reRequire('./fileToTest'); // fileToTest is now using your mock -} diff --git a/types/mock-require/v1/tsconfig.json b/types/mock-require/v1/tsconfig.json deleted file mode 100644 index 1f6be404c6..0000000000 --- a/types/mock-require/v1/tsconfig.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "compilerOptions": { - "module": "commonjs", - "lib": [ - "es6" - ], - "noImplicitAny": true, - "noImplicitThis": true, - "strictNullChecks": true, - "strictFunctionTypes": true, - "baseUrl": "../", - "typeRoots": [ - "../" - ], - "types": [], - "noEmit": true, - "forceConsistentCasingInFileNames": true - }, - "files": [ - "index.d.ts", - "mock-require-tests.ts" - ] -} \ No newline at end of file