UNPKG

4.2 kBTypeScriptView Raw
1// TypeScript Version: 3.0
2import Global = NodeJS.Global;
3import "jest";
4
5declare global {
6 const fetchMock: FetchMock;
7 namespace NodeJS {
8 interface Global {
9 fetch: FetchMock;
10 }
11 }
12}
13
14export interface GlobalWithFetchMock extends Global {
15 fetchMock: FetchMock;
16 fetch: FetchMock;
17}
18
19export interface FetchMock
20 extends jest.MockInstance<Promise<Response>, [string | Request | undefined, RequestInit | undefined]> {
21 (input?: string | Request, init?: RequestInit): Promise<Response>;
22
23 // Response mocking
24 mockResponse(fn: MockResponseInitFunction): FetchMock;
25 mockResponse(response: string, responseInit?: MockParams): FetchMock;
26
27 mockResponseOnce(fn: MockResponseInitFunction): FetchMock;
28 mockResponseOnce(response: string, responseInit?: MockParams): FetchMock;
29
30 // alias for mockResponseOnce
31 once(fn: MockResponseInitFunction): FetchMock;
32 once(url: string, responseInit?: MockParams): FetchMock;
33
34 mockResponses(...responses: Array<string | [string, MockParams] | MockResponseInitFunction>): FetchMock;
35
36 // Error/Reject mocking
37 mockReject(error?: ErrorOrFunction): FetchMock;
38 mockRejectOnce(error?: ErrorOrFunction): FetchMock;
39
40 mockAbort(): FetchMock;
41 mockAbortOnce(): FetchMock;
42
43 // Conditional Mocking
44 isMocking(input: string | Request): boolean;
45
46 doMock(fn?: MockResponseInitFunction): FetchMock;
47 doMock(response: string, responseInit?: MockParams): FetchMock;
48
49 doMockOnce(fn?: MockResponseInitFunction): FetchMock;
50 doMockOnce(response: string, responseInit?: MockParams): FetchMock;
51 // alias for doMockOnce
52 mockOnce(fn?: MockResponseInitFunction): FetchMock;
53 mockOnce(response: string, responseInit?: MockParams): FetchMock;
54
55 doMockIf(urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
56 doMockIf(urlOrPredicate: UrlOrPredicate, response: string, responseInit?: MockParams): FetchMock;
57 // alias for doMockIf
58 mockIf(urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
59 mockIf(urlOrPredicate: UrlOrPredicate, response: string, responseInit?: MockParams): FetchMock;
60
61 doMockOnceIf(urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
62 doMockOnceIf(urlOrPredicate: UrlOrPredicate, response: string, responseInit?: MockParams): FetchMock;
63 // alias for doMocKOnceIf
64 mockOnceIf(urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
65 mockOnceIf(urlOrPredicate: UrlOrPredicate, response: string, responseInit?: MockParams): FetchMock;
66
67 dontMock(fn?: MockResponseInitFunction): FetchMock;
68 dontMock(response: string, responseInit?: MockParams): FetchMock;
69
70 dontMockOnce(fn?: MockResponseInitFunction): FetchMock;
71 dontMockOnce(response: string, responseInit?: MockParams): FetchMock;
72
73 dontMockIf(urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
74 dontMockIf(urlOrPredicate: UrlOrPredicate, response: string, responseInit?: MockParams): FetchMock;
75
76 dontMockOnceIf(urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
77 dontMockOnceIf(urlOrPredicate: UrlOrPredicate, response: string, responseInit?: MockParams): FetchMock;
78
79 resetMocks(): void;
80 enableMocks(): void;
81 disableMocks(): void;
82}
83
84export interface MockParams {
85 status?: number;
86 statusText?: string;
87 headers?: string[][] | { [key: string]: string }; // HeadersInit
88 url?: string;
89}
90
91export interface MockResponseInit extends MockParams {
92 body?: string;
93 init?: MockParams;
94}
95
96export type ErrorOrFunction = Error | ((...args: any[]) => Promise<any>);
97export type UrlOrPredicate = string | RegExp | ((input: Request) => boolean);
98
99export type MockResponseInitFunction = (request: Request) => Promise<MockResponseInit | string>;
100
101// alias of fetchMock.enableMocks() for ES6 import syntax to not clash with other libraries
102export function enableFetchMocks(): void;
103// alias of fetchMock.disableMocks() for ease of ES6 import syntax to not clash with other libraries
104export function disableFetchMocks(): void;
105
106declare const fetchMock: FetchMock;
107
108export default fetchMock;