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