1 | // Project: https://github.com/wheresrhys/fetch-mock, http://www.wheresrhys.co.uk/fetch-mock
|
2 | // Definitions by: Alexey Svetliakov <https://github.com/asvetliakov>
|
3 | // Tamir Duberstein <https://github.com/tamird>
|
4 | // Risto Keravuori <https://github.com/merrywhether>
|
5 | // Chris Sinclair <https://github.com/chrissinclair>
|
6 | // Matt Tennison <https://github.com/matttennison>
|
7 | // Quentin Bouygues <https://github.com/quentinbouygues>
|
8 | // Fumiaki Matsushima <https://github.com/mtsmfm>
|
9 | // Colin Doig <https://github.com/captain-igloo>
|
10 | // Felix Chen <https://github.com/ChenNima>
|
11 | // Katsuya Hino <https://github.com/dobogo>
|
12 | //
|
13 | // Please note that I - wheresrys - don't use Typescript
|
14 | // These types have ben copied in here as a convenience for (some of)
|
15 | // fetch-mock's users
|
16 | // If you are a Typescript user and find a problem in these types, please
|
17 | // submit a PR
|
18 | //
|
19 | // TypeScript Version: 2.2
|
20 |
|
21 | declare namespace fetchMock {
|
22 | type MockRequest = Request | RequestInit;
|
23 |
|
24 | /**
|
25 | * Mock matcher function
|
26 | */
|
27 | type MockMatcherFunction = (url: string, opts: MockRequest) => boolean;
|
28 |
|
29 |
|
30 | type MockMatcherUrl = string | RegExp;
|
31 |
|
32 |
|
33 | /**
|
34 | * Mock matcher. Can be one of following:
|
35 | * string: Either
|
36 | * * an exact url to match e.g. 'http://www.site.com/page.html'
|
37 | * * if the string begins with a `^`, the string following the `^` must
|
38 | * begin the url e.g. '^http://www.site.com' would match
|
39 | * 'http://www.site.com' or 'http://www.site.com/page.html'
|
40 | * * '*' to match any url
|
41 | * RegExp: A regular expression to test the url against
|
42 | * Function(url, opts): A function (returning a Boolean) that is passed the
|
43 | * url and opts fetch() is called with (or, if fetch() was called with one,
|
44 | * the Request instance)
|
45 | */
|
46 | type MockMatcher = MockMatcherUrl | MockMatcherFunction;
|
47 |
|
48 | /**
|
49 | * Inspection filter. Can be one of the following:
|
50 | * boolean:
|
51 | * * true retrieves all calls matched by fetch.
|
52 | * fetchMock.MATCHED is an alias for true and may be used to make tests
|
53 | * more readable.
|
54 | * * false retrieves all calls not matched by fetch (i.e. those handled
|
55 | * by catch() or spy(). fetchMock.UNMATCHED is an alias for false and
|
56 | * may be used to make tests more readable.
|
57 | * MockMatcher (routeIdentifier):
|
58 | * All routes have an identifier:
|
59 | * * If it’s a named route, the identifier is the route’s name
|
60 | * * If the route is unnamed, the identifier is the matcher passed in to
|
61 | * .mock()
|
62 | * All calls that were handled by the route with the given identifier
|
63 | * will be retrieved
|
64 | * MockMatcher (matcher):
|
65 | * Any matcher compatible with the mocking api can be passed in to filter
|
66 | * the calls arbitrarily.
|
67 | */
|
68 | type InspectionFilter = MockMatcher | boolean;
|
69 |
|
70 | /**
|
71 | * Either an object compatible with the mocking api or a string specifying
|
72 | * a http method to filter by. This will be used to filter the list of
|
73 | * calls further.
|
74 | */
|
75 | type InspectionOptions = MockOptions | string;
|
76 |
|
77 | /**
|
78 | * Mock response object
|
79 | */
|
80 | interface MockResponseObject {
|
81 | /**
|
82 | * Set the response body
|
83 | */
|
84 | body?: string | {};
|
85 |
|
86 | /**
|
87 | * Set the response status
|
88 | * @default 200
|
89 | */
|
90 | status?: number;
|
91 |
|
92 | /**
|
93 | * Set the response headers.
|
94 | */
|
95 | headers?: { [key: string]: string };
|
96 |
|
97 | /**
|
98 | * If this property is present then a Promise rejected with the value
|
99 | * of throws is returned
|
100 | */
|
101 | throws?: Error;
|
102 |
|
103 | /**
|
104 | * The URL the response should be from (to imitate followed redirects
|
105 | * - will set redirected: true on the response)
|
106 | */
|
107 | redirectUrl?: string;
|
108 | }
|
109 |
|
110 | /**
|
111 | * Response: A Response instance - will be used unaltered
|
112 | * number: Creates a response with this status
|
113 | * string: Creates a 200 response with the string as the response body
|
114 | * object: As long as the object is not a MockResponseObject it is
|
115 | * converted into a json string and returned as the body of a 200 response
|
116 | * If MockResponseObject was given then it's used to configure response
|
117 | * Function(url, opts): A function that is passed the url and opts fetch()
|
118 | * is called with and that returns any of the responses listed above
|
119 | */
|
120 | type MockResponse = Response | Promise<Response>
|
121 | | number | Promise<number>
|
122 | | string | Promise<string>
|
123 | | {} | Promise<{}>
|
124 | | MockResponseObject | Promise<MockResponseObject>;
|
125 |
|
126 | /**
|
127 | * Mock response function
|
128 | */
|
129 | type MockResponseFunction = (url: string, opts: MockRequest) => MockResponse;
|
130 |
|
131 |
|
132 | /**
|
133 | * Mock options object
|
134 | */
|
135 | interface MockOptions {
|
136 | /**
|
137 | * A unique string naming the route. Used to subsequently retrieve
|
138 | * references to the calls, grouped by name.
|
139 | * @default matcher.toString()
|
140 | *
|
141 | * Note: If a non-unique name is provided no error will be thrown
|
142 | * (because names are optional, auto-generated ones may legitimately
|
143 | * clash)
|
144 | */
|
145 | name?: string;
|
146 |
|
147 | /**
|
148 | * http method to match
|
149 | */
|
150 | method?: string;
|
151 |
|
152 | /**
|
153 | * key/value map of headers to match
|
154 | */
|
155 | headers?: { [key: string]: string | number };
|
156 |
|
157 | /**
|
158 | * key/value map of query strings to match, in any order
|
159 | */
|
160 | query?: object;
|
161 |
|
162 | /**
|
163 | * key/value map of express style path params to match
|
164 | */
|
165 | params?: { [key: string]: string };
|
166 |
|
167 | /**
|
168 | * JSON serialisable object literal. Allowing any object for now
|
169 | * But in typescript 3.7 will change to JSON
|
170 | */
|
171 | body?: object;
|
172 |
|
173 | /**
|
174 | * A function for arbitrary matching
|
175 | */
|
176 | functionMatcher?: MockMatcherFunction;
|
177 |
|
178 | /**
|
179 | * as specified above
|
180 | */
|
181 | matcher?: MockMatcher;
|
182 |
|
183 | url?: MockMatcherUrl;
|
184 |
|
185 | /**
|
186 | * This option allows for existing routes in a mock to be overwritten.
|
187 | * It’s also possible to define multiple routes with ‘the same’ matcher.
|
188 | * Default behaviour is to error
|
189 | */
|
190 | overwriteRoutes?: boolean;
|
191 |
|
192 | /**
|
193 | * as specified above
|
194 | */
|
195 | response?: MockResponse | MockResponseFunction;
|
196 |
|
197 | /**
|
198 | * integer, n, limiting the number of times the matcher can be used.
|
199 | * If the route has already been called n times the route will be
|
200 | * ignored and the call to fetch() will fall through to be handled by
|
201 | * any other routes defined (which may eventually result in an error
|
202 | * if nothing matches it).
|
203 | */
|
204 | repeat?: number;
|
205 |
|
206 | /**
|
207 | * integer, n, delays responding for the number of milliseconds
|
208 | * specified.
|
209 | */
|
210 | delay?: number;
|
211 |
|
212 | /**
|
213 | * Convert objects into JSON before delivering as stub responses. Can
|
214 | * be useful to set to false globally if e.g. dealing with a lot of
|
215 | * array buffers. If true, will also add content-type: application/json
|
216 | * header.
|
217 | * @default true
|
218 | */
|
219 | sendAsJson?: boolean;
|
220 |
|
221 | /**
|
222 | * Automatically sets a content-length header on each response.
|
223 | * @default true
|
224 | */
|
225 | includeContentLength?: boolean;
|
226 |
|
227 | /**
|
228 | * Match calls that only partially match a specified body json.
|
229 | */
|
230 | matchPartialBody?: boolean;
|
231 | }
|
232 |
|
233 | interface MockCall extends Array<string | RequestInit | undefined> {
|
234 | 0: string;
|
235 | 1: RequestInit | undefined;
|
236 | identifier: string;
|
237 | isUnmatched: boolean | undefined;
|
238 | request: Request | undefined;
|
239 | response: Response | undefined;
|
240 | }
|
241 |
|
242 | interface MockOptionsMethodGet extends MockOptions {
|
243 | method?: 'GET';
|
244 | }
|
245 |
|
246 | interface MockOptionsMethodPost extends MockOptions {
|
247 | method?: 'POST';
|
248 | }
|
249 |
|
250 | interface MockOptionsMethodPut extends MockOptions {
|
251 | method?: 'PUT';
|
252 | }
|
253 |
|
254 | interface MockOptionsMethodDelete extends MockOptions {
|
255 | method?: 'DELETE';
|
256 | }
|
257 |
|
258 | interface MockOptionsMethodPatch extends MockOptions {
|
259 | method?: 'PATCH';
|
260 | }
|
261 |
|
262 | interface MockOptionsMethodHead extends MockOptions {
|
263 | method?: 'HEAD';
|
264 | }
|
265 |
|
266 | interface FetchMockStatic {
|
267 | MATCHED: true;
|
268 | UNMATCHED: false;
|
269 |
|
270 | /**
|
271 | * Replaces fetch() with a stub which records its calls, grouped by
|
272 | * route, and optionally returns a mocked Response object or passes the
|
273 | * call through to fetch(). Calls to .mock() can be chained.
|
274 | * @param matcher Condition for selecting which requests to mock
|
275 | * @param response Configures the http response returned by the mock
|
276 | * @param [options] Additional properties defining the route to mock
|
277 | */
|
278 | mock(matcher: MockMatcher | MockOptions, response: MockResponse | MockResponseFunction, options?: MockOptions): this;
|
279 |
|
280 | /**
|
281 | * Replaces fetch() with a stub which records its calls, grouped by
|
282 | * route, and optionally returns a mocked Response object or passes the
|
283 | * call through to fetch(). Calls to .mock() can be chained.
|
284 | * @param options The route to mock
|
285 | */
|
286 | mock(options: MockOptions): this;
|
287 |
|
288 | /**
|
289 | * Replaces fetch() with a stub which records its calls, grouped by
|
290 | * route, and optionally returns a mocked Response object or passes the
|
291 | * call through to fetch(). Calls to .mock() can be chained.
|
292 | * @param options The route to mock
|
293 | */
|
294 | mock(): this;
|
295 |
|
296 | /**
|
297 | * Returns a drop-in mock for fetch which can be passed to other mocking
|
298 | * libraries. It implements the full fetch-mock api and maintains its
|
299 | * own state independent of other instances, so tests can be run in
|
300 | * parallel.
|
301 | */
|
302 | sandbox(): FetchMockSandbox;
|
303 |
|
304 | /**
|
305 | * Replaces fetch() with a stub which records its calls, grouped by
|
306 | * route, and optionally returns a mocked Response object or passes the
|
307 | * call through to fetch(). Shorthand for mock() limited to being
|
308 | * called one time only. Calls to .once() can be chained.
|
309 | * @param matcher Condition for selecting which requests to mock
|
310 | * @param response Configures the http response returned by the mock
|
311 | * @param [options] Optional additional properties defining the route to mock
|
312 | */
|
313 | once(matcher: MockMatcher | MockOptions, response: MockResponse | MockResponseFunction, options?: MockOptions): this;
|
314 |
|
315 | /**
|
316 | * Replaces fetch() with a stub which records its calls, grouped by
|
317 | * route, and optionally returns a mocked Response object or passes the
|
318 | * call through to fetch(). Shorthand for mock() restricted to the GET
|
319 | * method. Calls to .get() can be chained.
|
320 | * @param matcher Condition for selecting which requests to mock
|
321 | * @param response Configures the http response returned by the mock
|
322 | * @param [options] Additional properties defining the route to mock
|
323 | */
|
324 | get(matcher: MockMatcher | MockOptionsMethodGet, response: MockResponse | MockResponseFunction, options?: MockOptionsMethodGet): this;
|
325 |
|
326 | /**
|
327 | * Replaces fetch() with a stub which records its calls, grouped by
|
328 | * route, and optionally returns a mocked Response object or passes the
|
329 | * call through to fetch(). Shorthand for mock() restricted to the GET
|
330 | * method and limited to being called one time only. Calls to .getOnce()
|
331 | * can be chained.
|
332 | * @param matcher Condition for selecting which requests to mock
|
333 | * @param response Configures the http response returned by the mock
|
334 | * @param [options] Additional properties defining the route to mock
|
335 | */
|
336 | getOnce(matcher: MockMatcher | MockOptionsMethodGet, response: MockResponse | MockResponseFunction, options?: MockOptionsMethodGet): this;
|
337 |
|
338 | /**
|
339 | * Replaces fetch() with a stub which records its calls, grouped by
|
340 | * route, and optionally returns a mocked Response object or passes the
|
341 | * call through to fetch(). Shorthand for mock() restricted to the POST
|
342 | * method. Calls to .post() can be chained.
|
343 | * @param matcher Condition for selecting which requests to mock
|
344 | * @param response Configures the http response returned by the mock
|
345 | * @param [options] Additional properties defining the route to mock
|
346 | */
|
347 | post(matcher: MockMatcher | MockOptionsMethodPost, response: MockResponse | MockResponseFunction, options?: MockOptionsMethodPost): this;
|
348 |
|
349 | /**
|
350 | * Replaces fetch() with a stub which records its calls, grouped by
|
351 | * route, and optionally returns a mocked Response object or passes the
|
352 | * call through to fetch(). Shorthand for mock() restricted to the POST
|
353 | * method and limited to being called one time only. Calls to .postOnce()
|
354 | * can be chained.
|
355 | * @param matcher Condition for selecting which requests to mock
|
356 | * @param response Configures the http response returned by the mock
|
357 | * @param [options] Additional properties defining the route to mock
|
358 | */
|
359 | postOnce(matcher: MockMatcher | MockOptionsMethodPost, response: MockResponse | MockResponseFunction, options?: MockOptionsMethodPost): this;
|
360 |
|
361 | /**
|
362 | * Replaces fetch() with a stub which records its calls, grouped by
|
363 | * route, and optionally returns a mocked Response object or passes the
|
364 | * call through to fetch(). Shorthand for mock() restricted to the PUT
|
365 | * method. Calls to .put() can be chained.
|
366 | * @param matcher Condition for selecting which requests to mock
|
367 | * @param response Configures the http response returned by the mock
|
368 | * @param [options] Additional properties defining the route to mock
|
369 | */
|
370 | put(matcher: MockMatcher | MockOptionsMethodPut, response: MockResponse | MockResponseFunction, options?: MockOptionsMethodPut): this;
|
371 |
|
372 | /**
|
373 | * Replaces fetch() with a stub which records its calls, grouped by
|
374 | * route, and optionally returns a mocked Response object or passes the
|
375 | * call through to fetch(). Shorthand for mock() restricted to the PUT
|
376 | * method and limited to being called one time only. Calls to .putOnce()
|
377 | * can be chained.
|
378 | * @param matcher Condition for selecting which requests to mock
|
379 | * @param response Configures the http response returned by the mock
|
380 | * @param [options] Additional properties defining the route to mock
|
381 | */
|
382 | putOnce(matcher: MockMatcher | MockOptionsMethodPut, response: MockResponse | MockResponseFunction, options?: MockOptionsMethodPut): this;
|
383 |
|
384 | /**
|
385 | * Replaces fetch() with a stub which records its calls, grouped by
|
386 | * route, and optionally returns a mocked Response object or passes the
|
387 | * call through to fetch(). Shorthand for mock() restricted to the
|
388 | * DELETE method. Calls to .delete() can be chained.
|
389 | * @param matcher Condition for selecting which requests to mock
|
390 | * @param response Configures the http response returned by the mock
|
391 | * @param [options] Additional properties defining the route to mock
|
392 | */
|
393 | delete(matcher: MockMatcher | MockOptionsMethodDelete, response: MockResponse | MockResponseFunction, options?: MockOptionsMethodDelete): this;
|
394 |
|
395 | /**
|
396 | * Replaces fetch() with a stub which records its calls, grouped by
|
397 | * route, and optionally returns a mocked Response object or passes the
|
398 | * call through to fetch(). Shorthand for mock() restricted to the
|
399 | * DELETE method and limited to being called one time only. Calls to
|
400 | * .deleteOnce() can be chained.
|
401 | * @param matcher Condition for selecting which requests to mock
|
402 | * @param response Configures the http response returned by the mock
|
403 | * @param [options] Additional properties defining the route to mock
|
404 | */
|
405 | deleteOnce(matcher: MockMatcher | MockOptionsMethodDelete, response: MockResponse | MockResponseFunction, options?: MockOptionsMethodDelete): this;
|
406 |
|
407 | /**
|
408 | * Replaces fetch() with a stub which records its calls, grouped by
|
409 | * route, and optionally returns a mocked Response object or passes the
|
410 | * call through to fetch(). Shorthand for mock() restricted to the HEAD
|
411 | * method. Calls to .head() can be chained.
|
412 | * @param matcher Condition for selecting which requests to mock
|
413 | * @param response Configures the http response returned by the mock
|
414 | * @param [options] Additional properties defining the route to mock
|
415 | */
|
416 | head(matcher: MockMatcher | MockOptionsMethodHead, response: MockResponse | MockResponseFunction, options?: MockOptionsMethodHead): this;
|
417 |
|
418 | /**
|
419 | * Replaces fetch() with a stub which records its calls, grouped by
|
420 | * route, and optionally returns a mocked Response object or passes the
|
421 | * call through to fetch(). Shorthand for mock() restricted to the HEAD
|
422 | * method and limited to being called one time only. Calls to .headOnce()
|
423 | * can be chained.
|
424 | * @param matcher Condition for selecting which requests to mock
|
425 | * @param response Configures the http response returned by the mock
|
426 | * @param [options] Additional properties defining the route to mock
|
427 | */
|
428 | headOnce(matcher: MockMatcher | MockOptionsMethodHead, response: MockResponse | MockResponseFunction, options?: MockOptionsMethodHead): this;
|
429 |
|
430 | /**
|
431 | * Replaces fetch() with a stub which records its calls, grouped by
|
432 | * route, and optionally returns a mocked Response object or passes the
|
433 | * call through to fetch(). Shorthand for mock() restricted to the PATCH
|
434 | * method. Calls to .patch() can be chained.
|
435 | * @param matcher Condition for selecting which requests to mock
|
436 | * @param response Configures the http response returned by the mock
|
437 | * @param [options] Additional properties defining the route to mock
|
438 | */
|
439 | patch(matcher: MockMatcher | MockOptionsMethodPatch, response: MockResponse | MockResponseFunction, options?: MockOptionsMethodPatch): this;
|
440 |
|
441 | /**
|
442 | * Replaces fetch() with a stub which records its calls, grouped by
|
443 | * route, and optionally returns a mocked Response object or passes the
|
444 | * call through to fetch(). Shorthand for mock() restricted to the PATCH
|
445 | * method and limited to being called one time only. Calls to .patchOnce()
|
446 | * can be chained.
|
447 | * @param matcher Condition for selecting which requests to mock
|
448 | * @param response Configures the http response returned by the mock
|
449 | * @param [options] Additional properties defining the route to mock
|
450 | */
|
451 | patchOnce(matcher: MockMatcher | MockOptionsMethodPatch, response: MockResponse | MockResponseFunction, options?: MockOptionsMethodPatch): this;
|
452 |
|
453 | /**
|
454 | * Chainable method that defines how to respond to calls to fetch that
|
455 | * don't match any of the defined mocks. It accepts the same types of
|
456 | * response as a normal call to .mock(matcher, response). It can also
|
457 | * take an arbitrary function to completely customise behaviour of
|
458 | * unmatched calls. If .catch() is called without any parameters then
|
459 | * every unmatched call will receive a 200 response.
|
460 | * @param [response] Configures the http response returned by the mock
|
461 | */
|
462 | catch(response?: MockResponse | MockResponseFunction): this;
|
463 |
|
464 | /**
|
465 | * Chainable method that records the call history of unmatched calls,
|
466 | * but instead of responding with a stubbed response, the request is
|
467 | * passed through to native fetch() and is allowed to communicate
|
468 | * over the network. Similar to catch().
|
469 | */
|
470 | spy(response?: MockResponse | MockResponseFunction): this;
|
471 |
|
472 | /**
|
473 | * Restores fetch() to its unstubbed state and clears all data recorded
|
474 | * for its calls. reset() is an alias for restore().
|
475 | */
|
476 | restore(): this;
|
477 |
|
478 | /**
|
479 | * Restores fetch() to its unstubbed state and clears all data recorded
|
480 | * for its calls. reset() is an alias for restore().
|
481 | */
|
482 | reset(): this;
|
483 |
|
484 | /**
|
485 | * Clears all data recorded for fetch()’s calls. It will not restore
|
486 | * fetch to its default implementation.
|
487 | */
|
488 | resetHistory(): this;
|
489 |
|
490 | /**
|
491 | * Removes mocking behaviour without resetting call history.
|
492 | */
|
493 | resetBehavior(): this;
|
494 |
|
495 | /**
|
496 | * Returns a promise that resolves once all fetches handled by fetch-mock
|
497 | * have resolved.
|
498 | * @param [waitForBody] Wait for all body parsing methods(res.json(),
|
499 | * res.text(), etc.) to resolve too.
|
500 | */
|
501 | flush(waitForBody?: boolean): Promise<MockResponse[]>;
|
502 |
|
503 | /**
|
504 | * Returns an array of all calls to fetch matching the given filters.
|
505 | * Each call is returned as a [url, options] array. If fetch was called
|
506 | * using a Request instance, this will be available as a request
|
507 | * property on this array.
|
508 | * @param [filter] Allows filtering of calls to fetch based on various
|
509 | * criteria
|
510 | * @param [options] Either an object compatible with the mocking api or
|
511 | * a string specifying a http method to filter by. This will be used to
|
512 | * filter the list of calls further.
|
513 | */
|
514 | calls(filter?: InspectionFilter, options?: InspectionOptions): MockCall[];
|
515 |
|
516 | /**
|
517 | * Returns a Boolean indicating whether any calls to fetch matched the
|
518 | * given filter.
|
519 | * @param [filter] Allows filtering of calls to fetch based on various
|
520 | * criteria
|
521 | * @param [options] Either an object compatible with the mocking api or
|
522 | * a string specifying a http method to filter by. This will be used to
|
523 | * filter the list of calls further.
|
524 | */
|
525 | called(filter?: InspectionFilter, options?: InspectionOptions): boolean;
|
526 |
|
527 | /**
|
528 | * Returns a Boolean indicating whether fetch was called the expected
|
529 | * number of times (or has been called at least once if repeat is
|
530 | * undefined for the route).
|
531 | * @param [filter] Rule for matching calls to fetch.
|
532 | */
|
533 | done(filter?: InspectionFilter): boolean;
|
534 |
|
535 | /**
|
536 | * Returns the arguments for the last call to fetch matching the given
|
537 | * filter.
|
538 | * @param [filter] Allows filtering of calls to fetch based on various
|
539 | * criteria
|
540 | * @param [options] Either an object compatible with the mocking api or
|
541 | * a string specifying a http method to filter by. This will be used to
|
542 | * filter the list of calls further.
|
543 | */
|
544 | lastCall(
|
545 | filter?: InspectionFilter,
|
546 | options?: InspectionOptions,
|
547 | ): MockCall | undefined;
|
548 |
|
549 | /**
|
550 | * Returns the url for the last call to fetch matching the given
|
551 | * filter. If fetch was last called using a Request instance, the url
|
552 | * will be extracted from this.
|
553 | * @param [filter] Allows filtering of calls to fetch based on various
|
554 | * criteria
|
555 | * @param [options] Either an object compatible with the mocking api or
|
556 | * a string specifying a http method to filter by. This will be used to
|
557 | * filter the list of calls further.
|
558 | */
|
559 | lastUrl(
|
560 | filter?: InspectionFilter,
|
561 | options?: InspectionOptions,
|
562 | ): string | undefined;
|
563 |
|
564 | /**
|
565 | * Returns the options for the call to fetch matching the given filter.
|
566 | * If fetch was last called using a Request instance, a set of options
|
567 | * inferred from the Request will be returned.
|
568 | * @param [filter] Allows filtering of calls to fetch based on various
|
569 | * criteria
|
570 | * @param [options] Either an object compatible with the mocking api or
|
571 | * a string specifying a http method to filter by. This will be used to
|
572 | * filter the list of calls further.
|
573 | */
|
574 | lastOptions(
|
575 | filter?: InspectionFilter,
|
576 | options?: InspectionOptions,
|
577 | ): MockOptions | undefined;
|
578 |
|
579 | /**
|
580 | * Returns the options for the call to fetch matching the given filter.
|
581 | * This is an experimental feature, very difficult to implement well given
|
582 | * fetch’s very private treatment of response bodies.
|
583 | * When doing all the following:
|
584 | - using node-fetch
|
585 | - responding with a real network response (using spy() or fallbackToNetwork)
|
586 | - using `fetchMock.LastResponse()`
|
587 | - awaiting the body content
|
588 | … the response will hang unless your source code also awaits the response body.
|
589 | This is an unavoidable consequence of the nodejs implementation of streams.
|
590 | * @param [filter] Allows filtering of calls to fetch based on various
|
591 | * criteria
|
592 | * @param [options] Either an object compatible with the mocking api or
|
593 | * a string specifying a http method to filter by. This will be used to
|
594 | * filter the list of calls further.
|
595 | */
|
596 | lastResponse(
|
597 | filter?: InspectionFilter,
|
598 | options?: InspectionOptions,
|
599 | ): Response | undefined;
|
600 |
|
601 | config: {
|
602 | /**
|
603 | * Convert objects into JSON before delivering as stub responses.
|
604 | * Can be useful to set to false globally if e.g. dealing with a
|
605 | * lot of array buffers. If true, will also add
|
606 | * content-type: application/json header.
|
607 | * @default true
|
608 | */
|
609 | sendAsJson?: boolean;
|
610 |
|
611 | /**
|
612 | * Automatically sets a content-length header on each response.
|
613 | * @default true
|
614 | */
|
615 | includeContentLength?: boolean;
|
616 |
|
617 | /**
|
618 | * - true: Unhandled calls fall through to the network
|
619 | * - false: Unhandled calls throw an error
|
620 | * - 'always': All calls fall through to the network, effectively
|
621 | * disabling fetch-mock.
|
622 | * @default false
|
623 | */
|
624 | fallbackToNetwork?: boolean | 'always';
|
625 |
|
626 | /**
|
627 | * Determines behaviour if a new route has the same name (or
|
628 | * inferred name) as an existing one
|
629 | * - undefined: An error will be throw when routes clash
|
630 | * - true: Overwrites the existing route
|
631 | * - false: Appends the new route to the list of routes
|
632 | * @default undefined
|
633 | */
|
634 | overwriteRoutes?: boolean;
|
635 |
|
636 | /**
|
637 | * Print a warning if any call is caught by a fallback handler (set
|
638 | * using the fallbackToNetwork option or catch())
|
639 | * @default true
|
640 | */
|
641 | warnOnFallback?: boolean;
|
642 |
|
643 | /**
|
644 | * Reference to the Promise constructor of a custom Promise
|
645 | * implementation.
|
646 | */
|
647 | Promise?: new (executor: (
|
648 | // Should be (value?: T | PromiseLike<T>) => void
|
649 | // But not sure if that's compatible with older typescript
|
650 | resolve: (value?: any) => void,
|
651 | reject: (value?: any) => void,
|
652 | ) => void) => Promise<any>;
|
653 |
|
654 | /**
|
655 | * Reference to a custom fetch implementation.
|
656 | */
|
657 | fetch?: (
|
658 | input?: string | Request,
|
659 | init?: RequestInit,
|
660 | ) => Promise<Response>;
|
661 |
|
662 | /**
|
663 | * Reference to the Headers constructor of a custom fetch
|
664 | * implementation.
|
665 | */
|
666 | Headers?: new () => Headers;
|
667 |
|
668 | /**
|
669 | * Reference to the Request constructor of a custom fetch
|
670 | * implementation.
|
671 | */
|
672 | Request?: new (input: string | Request, init?: RequestInit) => Request;
|
673 |
|
674 | /**
|
675 | * Reference to the Response constructor of a custom fetch
|
676 | * implementation.
|
677 | */
|
678 | Response?: new () => Response;
|
679 | };
|
680 | }
|
681 |
|
682 | interface FetchMockSandbox extends FetchMockStatic {
|
683 | /**
|
684 | * Also callable as fetch(). Use `typeof fetch` in your code to define
|
685 | * a field that accepts both `fetch()` and a fetch-mock sandbox.
|
686 | */
|
687 | (input?: string | Request , init?: RequestInit): Promise<Response>;
|
688 | }
|
689 | }
|
690 |
|
691 | declare var fetchMock: fetchMock.FetchMockStatic;
|
692 | export = fetchMock;
|
693 |
|
694 | declare module 'fetch-mock/esm/client' {
|
695 | const fetchMock: fetchMock.FetchMockStatic;
|
696 | export default fetchMock;
|
697 | }
|