Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sticky types #621

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions types/fetch-mock-tests.ts
Expand Up @@ -186,6 +186,10 @@ sandbox.get("http://test.com", {
redirectUrl: "http://example.org"
});

const stickySandbox = fetchMock.sandbox();
stickySandbox.sticky("http://test.com", 200);
stickySandbox.mock("http://test.com", 200, { sticky: true });

const response: fetchMock.MockResponseObject = {
throws: new Error('error'),
};
Expand Down
18 changes: 18 additions & 0 deletions types/index.d.ts
Expand Up @@ -228,6 +228,12 @@ declare namespace fetchMock {
* Match calls that only partially match a specified body json.
*/
matchPartialBody?: boolean;

/**
* Avoids a route being removed when reset(), restore() or resetBehavior() are called.
* Note - this does not preserve the history of calls to the route
*/
sticky?: boolean;
}

interface MockCall extends Array<string | RequestInit | undefined> {
Expand Down Expand Up @@ -301,6 +307,18 @@ declare namespace fetchMock {
*/
sandbox(): FetchMockSandbox;

/**
* Replaces fetch() with a stub which records its calls, grouped by
* route, and optionally returns a mocked Response object or passes the
* call through to fetch(). Shorthand for mock() which creates a route
* that persists even when restore(), reset() or resetbehavior() are called.
* Calls to .sticky() can be chained.
* @param matcher Condition for selecting which requests to mock
* @param response Configures the http response returned by the mock
* @param [options] Additional properties defining the route to mock
*/
sticky(matcher: MockMatcher | MockOptions, response: MockResponse | MockResponseFunction, options?: MockOptions): this;

/**
* Replaces fetch() with a stub which records its calls, grouped by
* route, and optionally returns a mocked Response object or passes the
Expand Down