mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-06-05 20:02:05 +08:00
Merge pull request #20858 from gDelgado14/master
[Mock-Require] - add declaration file for v2.0
This commit is contained in:
32
types/mock-require/index.d.ts
vendored
32
types/mock-require/index.d.ts
vendored
@@ -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 <https://github.com/djpereira>
|
||||
// Definitions by: Giorgio Delgado <https://github.com/gDelgado14>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.4
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
/** 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;
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": false,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
|
||||
Reference in New Issue
Block a user