mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-29 01:45:49 +08:00
New typings migrated to 2.0
This commit is contained in:
31
mock-require/index.d.ts
vendored
Normal file
31
mock-require/index.d.ts
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
// Type definitions for mock-require v1.3.0
|
||||
// Project: https://github.com/boblauer/mock-require
|
||||
// Definitions by: Daniel Pereira <https://github.com/djpereira>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
/** 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;
|
||||
45
mock-require/mock-require-tests.ts
Normal file
45
mock-require/mock-require-tests.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import mock = require('mock-require');
|
||||
|
||||
function testMock() {
|
||||
mock('http', {
|
||||
request: function () {
|
||||
console.log('http.request called');
|
||||
}
|
||||
});
|
||||
|
||||
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
|
||||
}
|
||||
19
mock-require/tsconfig.json
Normal file
19
mock-require/tsconfig.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": false,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"mock-require-tests.ts"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user