jest-mock-axios

  • Version 4.7.3
  • Published
  • 64.5 kB
  • 3 dependencies
  • MIT license

Install

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

Overview

Axios mock for Jest

Index

Variables

variable MockAxios

const MockAxios: any;
  • TypeScript version of Axios mock for unit testing with [Jest](https://facebook.github.io/jest/). This file is based on https://gist.github.com/tux4/36006a1859323f779ab0

    knee-cola <nikola.derezic@gmail.com> MIT License, http://www.opensource.org/licenses/MIT

Interfaces

interface AxiosAPI

interface AxiosAPI {}

    property all

    all: jest.Mock<(promises: any) => Promise<any>>;

      property create

      create: jest.Mock<() => AxiosMockType>;

        property defaults

        defaults: AxiosDefaults;

          property delete

          delete: jest.Mock<MockUnresolvedPromise>;

            property get

            get: jest.Mock<MockUnresolvedPromise>;

              property head

              head: jest.Mock<MockUnresolvedPromise>;

                property interceptors

                interceptors: Interceptors;

                  property isAxiosError

                  isAxiosError: (error: any) => boolean;

                    property options

                    options: jest.Mock<MockUnresolvedPromise>;

                      property patch

                      patch: jest.Mock<MockUnresolvedPromiseBody>;

                        property post

                        post: jest.Mock<MockUnresolvedPromiseBody>;

                          property put

                          put: jest.Mock<MockUnresolvedPromiseBody>;

                            property request

                            request: jest.Mock<(options?: any) => UnresolvedSynchronousPromise<any>>;

                              interface AxiosMockAPI

                              interface AxiosMockAPI {}

                                property Cancel

                                Cancel: CancelStatic;

                                  property CancelToken

                                  CancelToken: CancelTokenStatic;

                                    property getReqByMatchUrl

                                    getReqByMatchUrl: (url: RegExp) => AxiosMockQueueItem;
                                    • Returns request item of the most recent request with the given regex url Returns undefined if no matching request could be found

                                      The result can then be used with

                                      Parameter url

                                      the url of the request to be found

                                      See Also

                                      • mockResponse

                                    property getReqByRegex

                                    getReqByRegex: (opts: {
                                    [key in keyof AxiosMockQueueItem]+?: RegExp;
                                    }) => AxiosMockQueueItem;
                                    • Returns the most recent request matching any key with the regex (e.g.: url, data, config) Can also use multiple keys for research. For example: getReqByRegex({ url: /batch/, data: /employees/ })

                                      Returns undefined if no matching request could be found

                                      The result can then be used with

                                      Parameter opts

                                      key_x: The key of the request to be found RegExp_x: The RegExp to be tested against that key

                                      See Also

                                      • mockResponse

                                    property getReqByUrl

                                    getReqByUrl: (url: string) => AxiosMockQueueItem;
                                    • Returns request item of the most recent request with the given url The url must equal the url given in the 1st parameter when the request was made Returns undefined if no matching request could be found

                                      The result can then be used with

                                      Parameter url

                                      the url of the request to be found

                                      See Also

                                      • mockResponse

                                    property getReqMatching

                                    getReqMatching: (criteria: AxiosMockRequestCriteria) => AxiosMockQueueItem;
                                    • Returns request item of the most recent request with the given criteria Returns undefined if no matching request could be found

                                      The result can then be used with

                                      Parameter criteria

                                      the criteria by which to find the request

                                      See Also

                                      • mockResponse; in most cases it is better to use

                                      • mockResponseFor instead of calling this function yourself

                                    property lastPromiseGet

                                    lastPromiseGet: () => SynchronousPromise<any>;
                                    • Returns promise of the most recent request

                                    property lastReqGet

                                    lastReqGet: () => AxiosMockQueueItem;
                                    • Returns request item of the most recent request

                                    property mockError

                                    mockError: (
                                    error?: any,
                                    queueItem?: Promise<any> | AxiosMockQueueItem,
                                    silentMode?: boolean
                                    ) => void;
                                    • Simulates an error in server request

                                      Parameter error

                                      (optional) error object

                                      Parameter queueItem

                                      (optional) request promise for which response should be resolved

                                      Parameter silentMode

                                      (optional) specifies whether the call should throw an error or only fail quietly if no matching request is found.

                                    property mockResponse

                                    mockResponse: (
                                    response?: HttpResponse,
                                    queueItem?: Promise<any> | AxiosMockQueueItem,
                                    silentMode?: boolean
                                    ) => void;
                                    • Simulates a server response, (optionally) with the given data

                                      Parameter response

                                      (optional) response returned by the server

                                      Parameter queueItem

                                      (optional) request promise for which response should be resolved

                                      Parameter silentMode

                                      (optional) specifies whether the call should throw an error or only fail quietly if no matching request is found. Defaults to false.

                                    property mockResponseFor

                                    mockResponseFor: (
                                    criteria: string | AxiosMockRequestCriteria,
                                    response?: HttpResponse,
                                    silentMode?: boolean
                                    ) => void;
                                    • Simulates a server response for a specific request, (optionally) with the given data

                                      Parameter criteria

                                      specifies which request should be resolved; it can be just the URL or an object containing url and/or method

                                      Parameter response

                                      (optional) response returned by the server

                                      Parameter silentMode

                                      (optional) specifies whether the call should throw an error or only fail quietly if no matching request is found. Defaults to false.

                                    property popPromise

                                    popPromise: (
                                    promise?: UnresolvedSynchronousPromise<any>
                                    ) => UnresolvedSynchronousPromise<any>;
                                    • Removes the give promise from the queue

                                      Parameter promise

                                    property popRequest

                                    popRequest: (promise?: AxiosMockQueueItem) => AxiosMockQueueItem;
                                    • Removes the give request from the queue

                                      Parameter promise

                                    property queue

                                    queue: () => AxiosMockQueueItem[];
                                    • Return the queued requests

                                    property reset

                                    reset: () => void;
                                    • Clears all of the queued requests

                                    property useRequestHandler

                                    useRequestHandler: (handler: RequestHandler) => void;
                                    • Set a request handler that gets invoked every time a new request comes in The handler is invoked with the new request item

                                      Parameter handler

                                      the function to invoke with the new request item every time a new request comes in

                                    method isCancel

                                    isCancel: (value: any) => boolean;

                                      interface AxiosMockQueueItem

                                      interface AxiosMockQueueItem {}

                                        property config

                                        config?: any;

                                          property data

                                          data?: any;

                                            property method

                                            method: string;

                                              property promise

                                              promise: UnresolvedSynchronousPromise<any>;

                                                property url

                                                url: string;

                                                  interface AxiosMockRequestCriteria

                                                  interface AxiosMockRequestCriteria {}

                                                    property method

                                                    method?: string;

                                                      property params

                                                      params?: any;

                                                        property url

                                                        url?: string;

                                                          interface HttpResponse

                                                          interface HttpResponse {}

                                                            property config

                                                            config?: object;

                                                              property data

                                                              data: any;

                                                                property headers

                                                                headers?: object;

                                                                  property status

                                                                  status?: number;

                                                                    property statusText

                                                                    statusText?: string;

                                                                      interface InterceptorsStack

                                                                      interface InterceptorsStack {}

                                                                        method onFulfilled

                                                                        onFulfilled: (value: any) => any | Promise<any>;

                                                                          method onRejected

                                                                          onRejected: (error: any) => any;

                                                                            Type Aliases

                                                                            type AxiosMockType

                                                                            type AxiosMockType = AxiosAPI & AxiosMockAPI & jest.Mock<(options?: any) => any>;
                                                                            • Axios object can be called like a function, that's why we're defining it as a spy

                                                                            type RequestHandler

                                                                            type RequestHandler = (req: AxiosMockQueueItem) => void;

                                                                              Package Files (1)

                                                                              Dependencies (3)

                                                                              Dev Dependencies (7)

                                                                              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-mock-axios.

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