From 29f9af1fb0e13cc69ae3bbd9419393894070631d Mon Sep 17 00:00:00 2001 From: Colin Doig Date: Sat, 12 Jan 2019 22:13:07 +1300 Subject: [PATCH 1/2] add fetchMock.config, see #32030 --- types/fetch-mock/fetch-mock-tests.ts | 16 +++++- types/fetch-mock/index.d.ts | 85 +++++++++++++++++++++++++--- 2 files changed, 92 insertions(+), 9 deletions(-) diff --git a/types/fetch-mock/fetch-mock-tests.ts b/types/fetch-mock/fetch-mock-tests.ts index a5dc13c348..cf90d7e4df 100644 --- a/types/fetch-mock/fetch-mock-tests.ts +++ b/types/fetch-mock/fetch-mock-tests.ts @@ -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,17 @@ 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 => new Promise(() => { }); +fetchMock.config.Headers = Headers; +fetchMock.config.Request = Request; +fetchMock.config.Response = Response; + diff --git a/types/fetch-mock/index.d.ts b/types/fetch-mock/index.d.ts index a872a824d9..122983c62c 100644 --- a/types/fetch-mock/index.d.ts +++ b/types/fetch-mock/index.d.ts @@ -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 | undefined; + + /** + * 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: Function, + reject: Function, + ) => void) => Promise; + + /** + * Reference to a custom fetch implementation. + */ + fetch?: ( + input?: string | Request, + init?: RequestInit, + ) => Promise; + + /** + * 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 { From a1f3491bdfa77330251d71aa00bd2296a60fca98 Mon Sep 17 00:00:00 2001 From: Colin Doig Date: Sat, 12 Jan 2019 22:32:28 +1300 Subject: [PATCH 2/2] fix linting errors --- types/fetch-mock/fetch-mock-tests.ts | 1 - types/fetch-mock/index.d.ts | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/types/fetch-mock/fetch-mock-tests.ts b/types/fetch-mock/fetch-mock-tests.ts index cf90d7e4df..50b4d79252 100644 --- a/types/fetch-mock/fetch-mock-tests.ts +++ b/types/fetch-mock/fetch-mock-tests.ts @@ -154,4 +154,3 @@ fetchMock.config.fetch = (): Promise => new Promise(() => { }); fetchMock.config.Headers = Headers; fetchMock.config.Request = Request; fetchMock.config.Response = Response; - diff --git a/types/fetch-mock/index.d.ts b/types/fetch-mock/index.d.ts index 122983c62c..c9984dea89 100644 --- a/types/fetch-mock/index.d.ts +++ b/types/fetch-mock/index.d.ts @@ -556,7 +556,7 @@ declare namespace fetchMock { * - false: Appends the new route to the list of routes * @default undefined */ - overwriteRoutes?: boolean | undefined; + overwriteRoutes?: boolean; /** * Print a warning if any call is caught by a fallback handler (set @@ -570,8 +570,8 @@ declare namespace fetchMock { * implementation. */ Promise?: new (executor: ( - resolve: Function, - reject: Function, + resolve: () => void, + reject: () => void, ) => void) => Promise; /**