Merge pull request #32077 from captain-igloo/master

fetch-mock - add fetchMock.config, see #32030
This commit is contained in:
Daniel Rosenwasser
2019-01-22 16:02:38 -08:00
committed by GitHub
2 changed files with 91 additions and 9 deletions
+14 -1
View File
@@ -1,4 +1,4 @@
import * as fetchMock from "fetch-mock";
import fetchMock = require('fetch-mock');
fetchMock.mock("http://test.com", 200);
fetchMock.mock("http://test.com", 200, {
@@ -141,3 +141,16 @@ sandbox.get("http://test.com", {
const response: fetchMock.MockResponseObject = {
throws: new Error('error'),
};
fetchMock.config.sendAsJson = true;
fetchMock.config.includeContentLength = true;
fetchMock.config.fallbackToNetwork = true;
fetchMock.config.fallbackToNetwork = 'always';
fetchMock.config.overwriteRoutes = true;
fetchMock.config.overwriteRoutes = undefined;
fetchMock.config.warnOnFallback = true;
fetchMock.config.Promise = Promise;
fetchMock.config.fetch = (): Promise<Response> => new Promise(() => { });
fetchMock.config.Headers = Headers;
fetchMock.config.Request = Request;
fetchMock.config.Response = Response;
+77 -8
View File
@@ -523,14 +523,83 @@ declare namespace fetchMock {
options?: InspectionOptions,
): MockOptions | undefined;
/**
* Set some global config options, which include
* sendAsJson [default `true`] - by default fetchMock will
* convert objects to JSON before sending. This is overrideable
* for each call but for some scenarios, e.g. when dealing with a
* lot of array buffers, it can be useful to default to `false`
*/
configure(opts: {}): void;
config: {
/**
* Convert objects into JSON before delivering as stub reponses.
* Can be useful to set to false globally if e.g. dealing with a
* lot of array buffers. If true, will also add
* content-type: application/json header.
* @default true
*/
sendAsJson?: boolean;
/**
* Automatically sets a content-length header on each response.
* @default true
*/
includeContentLength?: boolean;
/**
* - true: Unhandled calls fall through to the network
* - false: Unhandled calls throw an error
* - 'always': All calls fall through to the network, effectively
* disabling fetch-mock.
* @default false
*/
fallbackToNetwork?: boolean | 'always';
/**
* Determines behaviour if a new route has the same name (or
* inferred name) as an existing one
* - undefined: An error will be throw when routes clash
* - true: Overwrites the existing route
* - false: Appends the new route to the list of routes
* @default undefined
*/
overwriteRoutes?: boolean;
/**
* Print a warning if any call is caught by a fallback handler (set
* using the fallbackToNetwork option or catch())
* @default true
*/
warnOnFallback?: boolean;
/**
* Reference to the Promise constructor of a custom Promise
* implementation.
*/
Promise?: new (executor: (
resolve: () => void,
reject: () => void,
) => void) => Promise<Response>;
/**
* Reference to a custom fetch implementation.
*/
fetch?: (
input?: string | Request,
init?: RequestInit,
) => Promise<Response>;
/**
* Reference to the Headers constructor of a custom fetch
* implementation.
*/
Headers?: new () => Headers;
/**
* Reference to the Request constructor of a custom fetch
* implementation.
*/
Request?: new (input: string | Request, init?: RequestInit) => Request;
/**
* Reference to the Response constructor of a custom fetch
* implementation.
*/
Response?: new () => Response;
};
}
interface FetchMockSandbox extends FetchMockStatic {