jest-fetch-mock

  • Version 4.2.0
  • Published
  • 75.9 kB
  • 1 dependency
  • MIT license

Install

npm i jest-fetch-mock
yarn add jest-fetch-mock
pnpm add jest-fetch-mock

Overview

fetch mock for jest

Index

Variables

variable fetchMock

const fetchMock: FetchMock;

    Functions

    function createFetchMock

    createFetchMock: (jestLike: MockFunctionFactory) => FetchMock;
    • Build a FetchMock bound to an explicitly provided jest object - for injectGlobals: false setups: pass the jest imported from '@jest/globals'. Also available dependency-free from 'jest-fetch-mock/factory'.

    function disableFetchMocks

    disableFetchMocks: () => FetchMock;

      function enableFetchMocks

      enableFetchMocks: () => FetchMock;

        Interfaces

        interface FetchMock

        interface FetchMock extends FetchMockInstance {}

          property defaultResponseInit

          defaultResponseInit: MockParams | undefined;
          • Optional init merged under every mocked response's own init - e.g. default headers.

          property Headers

          Headers: typeof Headers;

            property realFetch

            realFetch: FetchImplementation;
            • The implementation unmatched (dontMock'd) requests pass through to. Reassignable in tests.

            property Request

            Request: typeof Request;

              property Response

              Response: {
              (body?: unknown, init?: MockParams): Response;
              redirect(url: string | URL, status?: number): Response;
              error(): Response;
              /** Present when the backing Response class provides it (native fetch; not node-fetch 2). */
              json?(data: unknown, init?: ResponseInit): Response;
              };

                property usingNativeFetch

                usingNativeFetch: boolean;
                • True when the environment's own fetch primitives are in use (no fallback engaged).

                method clearRoutes

                clearRoutes: () => FetchMock;

                  method disableFetchMocks

                  disableFetchMocks: () => FetchMock;

                    method disableMocks

                    disableMocks: () => FetchMock;

                      method doMock

                      doMock: {
                      (fn?: MockResponseInitFunction): FetchMock;
                      (response: string, responseInit?: MockParams): FetchMock;
                      (response: Response): FetchMock;
                      };

                        method doMockIf

                        doMockIf: {
                        (urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
                        (
                        urlOrPredicate: UrlOrPredicate,
                        response: string,
                        responseInit?: MockParams
                        ): FetchMock;
                        (urlOrPredicate: UrlOrPredicate, response: Response): FetchMock;
                        };

                          method doMockOnce

                          doMockOnce: {
                          (fn?: MockResponseInitFunction): FetchMock;
                          (response: string, responseInit?: MockParams): FetchMock;
                          (response: Response): FetchMock;
                          };

                            method doMockOnceIf

                            doMockOnceIf: {
                            (urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
                            (
                            urlOrPredicate: UrlOrPredicate,
                            response: string,
                            responseInit?: MockParams
                            ): FetchMock;
                            (urlOrPredicate: UrlOrPredicate, response: Response): FetchMock;
                            };

                              method dontMock

                              dontMock: {
                              (fn?: MockResponseInitFunction): FetchMock;
                              (response: string, responseInit?: MockParams): FetchMock;
                              (response: Response): FetchMock;
                              };

                                method dontMockIf

                                dontMockIf: {
                                (urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
                                (
                                urlOrPredicate: UrlOrPredicate,
                                response: string,
                                responseInit?: MockParams
                                ): FetchMock;
                                (urlOrPredicate: UrlOrPredicate, response: Response): FetchMock;
                                };

                                  method dontMockOnce

                                  dontMockOnce: {
                                  (fn?: MockResponseInitFunction): FetchMock;
                                  (response: string, responseInit?: MockParams): FetchMock;
                                  (response: Response): FetchMock;
                                  };

                                    method dontMockOnceIf

                                    dontMockOnceIf: {
                                    (urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
                                    (
                                    urlOrPredicate: UrlOrPredicate,
                                    response: string,
                                    responseInit?: MockParams
                                    ): FetchMock;
                                    (urlOrPredicate: UrlOrPredicate, response: Response): FetchMock;
                                    };

                                      method enableFetchMocks

                                      enableFetchMocks: () => FetchMock;

                                        method enableMocks

                                        enableMocks: () => FetchMock;

                                          method isMocking

                                          isMocking: (input: FetchInput) => boolean;

                                            method mockAbort

                                            mockAbort: () => FetchMock;

                                              method mockAbortOnce

                                              mockAbortOnce: () => FetchMock;

                                                method mockIf

                                                mockIf: {
                                                (urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
                                                (
                                                urlOrPredicate: UrlOrPredicate,
                                                response: string,
                                                responseInit?: MockParams
                                                ): FetchMock;
                                                (urlOrPredicate: UrlOrPredicate, response: Response): FetchMock;
                                                };

                                                  method mockOnce

                                                  mockOnce: {
                                                  (fn?: MockResponseInitFunction): FetchMock;
                                                  (response: string, responseInit?: MockParams): FetchMock;
                                                  (response: Response): FetchMock;
                                                  };

                                                    method mockOnceIf

                                                    mockOnceIf: {
                                                    (urlOrPredicate: UrlOrPredicate, fn?: MockResponseInitFunction): FetchMock;
                                                    (
                                                    urlOrPredicate: UrlOrPredicate,
                                                    response: string,
                                                    responseInit?: MockParams
                                                    ): FetchMock;
                                                    (urlOrPredicate: UrlOrPredicate, response: Response): FetchMock;
                                                    };

                                                      method mockReject

                                                      mockReject: (error?: ErrorOrFunction) => FetchMock;

                                                        method mockRejectOnce

                                                        mockRejectOnce: (error?: ErrorOrFunction) => FetchMock;

                                                          method mockResponse

                                                          mockResponse: {
                                                          (fn: MockResponseInitFunction): FetchMock;
                                                          (response: string, responseInit?: MockParams): FetchMock;
                                                          (response: Response): FetchMock;
                                                          };

                                                            method mockResponseOnce

                                                            mockResponseOnce: {
                                                            (fn: MockResponseInitFunction): FetchMock;
                                                            (response: string, responseInit?: MockParams): FetchMock;
                                                            (response: Response): FetchMock;
                                                            };

                                                              method mockResponses

                                                              mockResponses: (
                                                              ...responses: Array<
                                                              string | Response | [string, MockParams] | MockResponseInitFunction
                                                              >
                                                              ) => FetchMock;

                                                                method once

                                                                once: {
                                                                (fn: MockResponseInitFunction): FetchMock;
                                                                (url: string, responseInit?: MockParams): FetchMock;
                                                                (response: Response): FetchMock;
                                                                };

                                                                  method resetMocks

                                                                  resetMocks: () => void;

                                                                    method route

                                                                    route: {
                                                                    (urlOrPredicate: UrlOrPredicate, fn: MockResponseInitFunction): FetchMock;
                                                                    (
                                                                    urlOrPredicate: UrlOrPredicate,
                                                                    response?: string | Response,
                                                                    responseInit?: MockParams
                                                                    ): FetchMock;
                                                                    };

                                                                      method routeOnce

                                                                      routeOnce: {
                                                                      (urlOrPredicate: UrlOrPredicate, fn: MockResponseInitFunction): FetchMock;
                                                                      (
                                                                      urlOrPredicate: UrlOrPredicate,
                                                                      response?: string | Response,
                                                                      responseInit?: MockParams
                                                                      ): FetchMock;
                                                                      };

                                                                        interface FetchMockInstance

                                                                        interface FetchMockInstance {}
                                                                        • The jest-mock surface of the fetch mock, declared structurally so no @types/jest is required.

                                                                        property mock

                                                                        mock: FetchMockState;

                                                                          method getMockImplementation

                                                                          getMockImplementation: () => FetchImplementation | undefined;

                                                                            method getMockName

                                                                            getMockName: () => string;

                                                                              method mockClear

                                                                              mockClear: () => this;

                                                                                method mockImplementation

                                                                                mockImplementation: (fn: FetchImplementation) => this;

                                                                                  method mockImplementationOnce

                                                                                  mockImplementationOnce: (fn: FetchImplementation) => this;

                                                                                    method mockName

                                                                                    mockName: (name: string) => this;

                                                                                      method mockRejectedValue

                                                                                      mockRejectedValue: (value: unknown) => this;

                                                                                        method mockRejectedValueOnce

                                                                                        mockRejectedValueOnce: (value: unknown) => this;

                                                                                          method mockReset

                                                                                          mockReset: () => this;

                                                                                            method mockResolvedValue

                                                                                            mockResolvedValue: (value: Response) => this;

                                                                                              method mockResolvedValueOnce

                                                                                              mockResolvedValueOnce: (value: Response) => this;

                                                                                                method mockRestore

                                                                                                mockRestore: () => void;

                                                                                                  method mockReturnValue

                                                                                                  mockReturnValue: (value: Promise<Response>) => this;

                                                                                                    method mockReturnValueOnce

                                                                                                    mockReturnValueOnce: (value: Promise<Response>) => this;

                                                                                                      call signature

                                                                                                      (input: FetchInput, init?: RequestInit): Promise<Response>;

                                                                                                        interface FetchMockResult

                                                                                                        interface FetchMockResult {}

                                                                                                          property type

                                                                                                          type: 'return' | 'throw' | 'incomplete';

                                                                                                            property value

                                                                                                            value: unknown;

                                                                                                              interface FetchMockState

                                                                                                              interface FetchMockState {}

                                                                                                                property calls

                                                                                                                calls: FetchMockCallArgs[];

                                                                                                                  property contexts

                                                                                                                  contexts: unknown[];

                                                                                                                    property instances

                                                                                                                    instances: unknown[];

                                                                                                                      property invocationCallOrder

                                                                                                                      invocationCallOrder: number[];

                                                                                                                        property lastCall

                                                                                                                        lastCall?: FetchMockCallArgs;

                                                                                                                          property results

                                                                                                                          results: FetchMockResult[];

                                                                                                                            interface GlobalWithFetchMock

                                                                                                                            interface GlobalWithFetchMock {}
                                                                                                                            • Deprecated

                                                                                                                              Cast your global object to typeof globalThis & { fetchMock: FetchMock } instead. Kept for backwards compatibility with older setup guides.

                                                                                                                            property fetch

                                                                                                                            fetch: FetchMock;

                                                                                                                              property fetchMock

                                                                                                                              fetchMock: FetchMock;

                                                                                                                                index signature

                                                                                                                                [key: string]: any;

                                                                                                                                  interface MockFunctionFactory

                                                                                                                                  interface MockFunctionFactory {}
                                                                                                                                  • Anything with a jest-compatible mock-function factory (the jest object itself qualifies).

                                                                                                                                  method dontMock

                                                                                                                                  dontMock: (moduleName: string) => unknown;

                                                                                                                                    method fn

                                                                                                                                    fn: (implementation?: (...args: any[]) => any) => any;

                                                                                                                                      method setMock

                                                                                                                                      setMock: (moduleName: string, moduleExports: unknown) => unknown;

                                                                                                                                        interface MockParams

                                                                                                                                        interface MockParams {}

                                                                                                                                          property counter

                                                                                                                                          counter?: number;
                                                                                                                                          • Set >= 1 to have redirected return true.

                                                                                                                                          property headers

                                                                                                                                          headers?: string[][] | { [key: string]: string };

                                                                                                                                            property status

                                                                                                                                            status?: number;

                                                                                                                                              property statusText

                                                                                                                                              statusText?: string;

                                                                                                                                                property url

                                                                                                                                                url?: string;

                                                                                                                                                  interface MockResponseInit

                                                                                                                                                  interface MockResponseInit extends MockParams {}

                                                                                                                                                    property body

                                                                                                                                                    body?: string;

                                                                                                                                                      property init

                                                                                                                                                      init?: MockParams;

                                                                                                                                                        Type Aliases

                                                                                                                                                        type ErrorOrFunction

                                                                                                                                                        type ErrorOrFunction = Error | ((...args: any[]) => Promise<any>);

                                                                                                                                                          type FetchImplementation

                                                                                                                                                          type FetchImplementation = (
                                                                                                                                                          input: FetchInput,
                                                                                                                                                          init?: RequestInit
                                                                                                                                                          ) => Promise<Response>;

                                                                                                                                                            type FetchInput

                                                                                                                                                            type FetchInput = string | URL | Request;

                                                                                                                                                              type FetchMockCallArgs

                                                                                                                                                              type FetchMockCallArgs = [FetchInput?, (RequestInit | undefined)?];

                                                                                                                                                                type MockResponseInitFunction

                                                                                                                                                                type MockResponseInitFunction = (
                                                                                                                                                                request: Request
                                                                                                                                                                ) =>
                                                                                                                                                                | MockResponseInit
                                                                                                                                                                | string
                                                                                                                                                                | Response
                                                                                                                                                                | Promise<MockResponseInit | string | Response>;

                                                                                                                                                                  type UrlOrPredicate

                                                                                                                                                                  type UrlOrPredicate = string | RegExp | ((input: Request) => boolean);

                                                                                                                                                                    Namespaces

                                                                                                                                                                    namespace global

                                                                                                                                                                    namespace global {}

                                                                                                                                                                      variable fetchMock

                                                                                                                                                                      var fetchMock: FetchMock;

                                                                                                                                                                        Package Files (1)

                                                                                                                                                                        Dependencies (1)

                                                                                                                                                                        Dev Dependencies (10)

                                                                                                                                                                        Peer Dependencies (0)

                                                                                                                                                                        No peer dependencies.

                                                                                                                                                                        Badge

                                                                                                                                                                        To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                                                                                                                                        You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/jest-fetch-mock.

                                                                                                                                                                        • Markdown
                                                                                                                                                                          [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/jest-fetch-mock)
                                                                                                                                                                        • HTML
                                                                                                                                                                          <a href="https://www.jsdocs.io/package/jest-fetch-mock"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>