jest-mock

  • Version 30.5.1
  • Published
  • 53.5 kB
  • 4 dependencies
  • MIT license

Install

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

Overview

**Note:** More details on user side API can be found in [Jest documentation](https://jestjs.io/docs/mock-function-api).

Index

Variables

variable mocked

const mocked: {
<T extends object>(source: T, options?: { shallow: false }): Mocked<T>;
<T extends object>(source: T, options: { shallow: true }): MockedShallow<T>;
};

    variable spyOn

    const spyOn: {
    <
    T extends object,
    K extends Exclude<
    keyof T,
    | keyof {
    [K in keyof T as Required<T>[K] extends ClassLike
    ? K
    : never]: T[K];
    }
    | keyof {
    [K in keyof T as Required<T>[K] extends FunctionLike
    ? K
    : never]: T[K];
    }
    >,
    V extends Required<T>[K],
    A extends 'get' | 'set'
    >(
    object: T,
    methodKey: K,
    accessType: A
    ): A extends 'get' ? SpiedGetter<V> : A extends 'set' ? SpiedSetter<V> : never;
    <
    T extends object,
    K extends
    | keyof {
    [K in keyof T as Required<T>[K] extends ClassLike
    ? K
    : never]: T[K];
    }
    | keyof {
    [K in keyof T as Required<T>[K] extends FunctionLike
    ? K
    : never]: T[K];
    },
    V extends Required<T>[K]
    >(
    object: T,
    methodKey: K
    ): V extends FunctionLike | ClassLike ? Spied<V> : never;
    };

      Functions

      function fn

      fn: <T extends FunctionLike = UnknownFunction>(implementation?: T) => Mock<T>;

        function replaceProperty

        replaceProperty: <T extends object, K extends keyof T>(
        object: T,
        propertyKey: K,
        value: T[K]
        ) => Replaced<T[K]>;

          Classes

          class ModuleMocker

          class ModuleMocker {}

            constructor

            constructor(global: typeof globalThis);
            • Parameter global

              Global object of the test environment, used to create mocks

              See Also

              • README.md

            method clearAllMocks

            clearAllMocks: () => void;

              method clearMocksOnScope

              clearMocksOnScope: (scope: object) => void;
              • Walks the own keys of scope and calls .mockClear() on each value that is a Jest mock function. Used by resetModules to clear mocks that user code installed directly on the test environment's global object (where the mock-fn registry doesn't see them).

              method fn

              fn: <T extends FunctionLike = UnknownFunction>(implementation?: T) => Mock<T>;

                method generateFromMetadata

                generateFromMetadata: <T>(metadata: MockMetadata<T>) => Mocked<T>;
                • Parameter metadata

                  Metadata for the mock in the schema returned by the getMetadata method of this module.

                  See Also

                  • README.md

                method getMetadata

                getMetadata: <T = unknown>(
                component: T,
                _refs?: Map<T, number>
                ) => MockMetadata<T> | null;
                • Parameter component

                  The component for which to retrieve metadata.

                  See Also

                  • README.md

                method isMockFunction

                isMockFunction: {
                <T extends FunctionLike = UnknownFunction>(
                fn: MockInstance<T>
                ): fn is MockInstance<T>;
                <P extends unknown[], R>(fn: (...args: P) => R): fn is Mock<
                (...args: P) => R
                >;
                (fn: unknown): fn is Mock<UnknownFunction>;
                };

                  method mocked

                  mocked: {
                  <T extends object>(source: T, options?: { shallow: false }): Mocked<T>;
                  <T extends object>(source: T, options: { shallow: true }): MockedShallow<T>;
                  };

                    method replaceProperty

                    replaceProperty: <T extends object, K extends keyof T>(
                    object: T,
                    propertyKey: K,
                    value: T[K]
                    ) => Replaced<T[K]>;

                      method resetAllMocks

                      resetAllMocks: () => void;

                        method restoreAllMocks

                        restoreAllMocks: () => void;

                          method spyOn

                          spyOn: {
                          <
                          T extends object,
                          K extends Exclude<
                          keyof T,
                          | keyof {
                          [K in keyof T as Required<T>[K] extends ClassLike
                          ? K
                          : never]: T[K];
                          }
                          | keyof {
                          [K in keyof T as Required<T>[K] extends FunctionLike
                          ? K
                          : never]: T[K];
                          }
                          >,
                          V extends Required<T>[K],
                          A extends 'get' | 'set'
                          >(
                          object: T,
                          methodKey: K,
                          accessType: A
                          ): A extends 'get'
                          ? SpiedGetter<V>
                          : A extends 'set'
                          ? SpiedSetter<V>
                          : never;
                          <
                          T extends object,
                          K extends
                          | keyof {
                          [K in keyof T as Required<T>[K] extends ClassLike
                          ? K
                          : never]: T[K];
                          }
                          | keyof {
                          [K in keyof T as Required<T>[K] extends FunctionLike
                          ? K
                          : never]: T[K];
                          },
                          V extends Required<T>[K]
                          >(
                          object: T,
                          methodKey: K
                          ): V extends FunctionLike | ClassLike ? Spied<V> : never;
                          };

                            Interfaces

                            interface Mock

                            interface Mock<T extends FunctionLike = UnknownFunction>
                            extends Function,
                            MockInstance<T> {}
                            • All what the internal typings need is to be sure that we have any-function. FunctionLike type ensures that and helps to constrain the type as well. The default of UnknownFunction makes sure that anys do not leak to the user side. For instance, calling fn() without implementation will return a mock of (...args: Array<unknown>) => unknown type. If implementation is provided, its typings are inferred correctly.

                            construct signature

                            new (...args: Parameters<T>): ReturnType<T>;

                              call signature

                              (...args: Parameters<T>): ReturnType<T>;

                                interface MockInstance

                                interface MockInstance<T extends FunctionLike = UnknownFunction>
                                extends Disposable {}

                                  property mock

                                  mock: MockFunctionState<T>;

                                    method getMockImplementation

                                    getMockImplementation: () => T | undefined;

                                      method getMockName

                                      getMockName: () => string;

                                        method mockClear

                                        mockClear: () => this;

                                          method mockImplementation

                                          mockImplementation: (fn: T) => this;

                                            method mockImplementationOnce

                                            mockImplementationOnce: (fn: T) => this;

                                              method mockName

                                              mockName: (name: string) => this;

                                                method mockRejectedValue

                                                mockRejectedValue: (value: RejectType<T>) => this;

                                                  method mockRejectedValueOnce

                                                  mockRejectedValueOnce: (value: RejectType<T>) => this;

                                                    method mockReset

                                                    mockReset: () => this;

                                                      method mockResolvedValue

                                                      mockResolvedValue: (value: ResolveType<T>) => this;

                                                        method mockResolvedValueOnce

                                                        mockResolvedValueOnce: (value: ResolveType<T>) => this;

                                                          method mockRestore

                                                          mockRestore: () => void;

                                                            method mockReturnThis

                                                            mockReturnThis: () => this;

                                                              method mockReturnValue

                                                              mockReturnValue: (value: ReturnType<T>) => this;

                                                                method mockReturnValueOnce

                                                                mockReturnValueOnce: (value: ReturnType<T>) => this;

                                                                  method whenCalledWith

                                                                  whenCalledWith: (...args: FunctionParameters<T>) => Mock<T>;

                                                                    method withImplementation

                                                                    withImplementation: {
                                                                    (fn: T, callback: () => Promise<unknown>): Promise<void>;
                                                                    (fn: T, callback: () => void): void;
                                                                    };

                                                                      interface Replaced

                                                                      interface Replaced<T = unknown> {}

                                                                        method replaceValue

                                                                        replaceValue: (value: T) => this;
                                                                        • Change the value of the property.

                                                                        method restore

                                                                        restore: () => void;
                                                                        • Restore property to its original value known at the time of mocking.

                                                                        Type Aliases

                                                                        type ClassLike

                                                                        type ClassLike = new (...args: any) => any;

                                                                          type ConstructorLikeKeys

                                                                          type ConstructorLikeKeys<T> = keyof {
                                                                          [K in keyof T as Required<T>[K] extends ClassLike ? K : never]: T[K];
                                                                          };

                                                                            type FunctionLike

                                                                            type FunctionLike = (...args: any) => any;

                                                                              type MethodLikeKeys

                                                                              type MethodLikeKeys<T> = keyof {
                                                                              [K in keyof T as Required<T>[K] extends FunctionLike ? K : never]: T[K];
                                                                              };

                                                                                type Mocked

                                                                                type Mocked<T> = T extends ClassLike
                                                                                ? MockedClass<T>
                                                                                : T extends FunctionLike
                                                                                ? MockedFunction<T>
                                                                                : T extends object
                                                                                ? MockedObject<T>
                                                                                : T;

                                                                                  type MockedClass

                                                                                  type MockedClass<T extends ClassLike> = MockInstance<
                                                                                  (...args: ConstructorParameters<T>) => Mocked<InstanceType<T>>
                                                                                  > &
                                                                                  MockedObject<T>;

                                                                                    type MockedFunction

                                                                                    type MockedFunction<T extends FunctionLike> = MockInstance<T> & MockedObject<T>;

                                                                                      type MockedFunctionShallow

                                                                                      type MockedFunctionShallow<T extends FunctionLike> = MockInstance<T> & T;

                                                                                        type MockedObject

                                                                                        type MockedObject<T extends object> = {
                                                                                        [K in keyof T]: T[K] extends ClassLike
                                                                                        ? MockedClass<T[K]>
                                                                                        : T[K] extends FunctionLike
                                                                                        ? MockedFunction<T[K]>
                                                                                        : T[K] extends object
                                                                                        ? MockedObject<T[K]>
                                                                                        : T[K];
                                                                                        } & T;

                                                                                          type MockedObjectShallow

                                                                                          type MockedObjectShallow<T extends object> = {
                                                                                          [K in keyof T]: T[K] extends ClassLike
                                                                                          ? MockedClass<T[K]>
                                                                                          : T[K] extends FunctionLike
                                                                                          ? MockedFunctionShallow<T[K]>
                                                                                          : T[K];
                                                                                          } & T;

                                                                                            type MockedShallow

                                                                                            type MockedShallow<T> = T extends ClassLike
                                                                                            ? MockedClass<T>
                                                                                            : T extends FunctionLike
                                                                                            ? MockedFunctionShallow<T>
                                                                                            : T extends object
                                                                                            ? MockedObjectShallow<T>
                                                                                            : T;

                                                                                              type MockFunctionResult

                                                                                              type MockFunctionResult<T extends FunctionLike = UnknownFunction> =
                                                                                              | MockFunctionResultIncomplete
                                                                                              | MockFunctionResultReturn<T>
                                                                                              | MockFunctionResultThrow;

                                                                                                type MockFunctionResultIncomplete

                                                                                                type MockFunctionResultIncomplete = {
                                                                                                type: 'incomplete';
                                                                                                /**
                                                                                                * Result of a single call to a mock function that has not yet completed.
                                                                                                * This occurs if you test the result from within the mock function itself,
                                                                                                * or from within a function that was called by the mock.
                                                                                                */
                                                                                                value: undefined;
                                                                                                };

                                                                                                  type MockFunctionResultReturn

                                                                                                  type MockFunctionResultReturn<T extends FunctionLike = UnknownFunction> = {
                                                                                                  type: 'return';
                                                                                                  /**
                                                                                                  * Result of a single call to a mock function that returned.
                                                                                                  */
                                                                                                  value: ReturnType<T>;
                                                                                                  };

                                                                                                    type MockFunctionResultThrow

                                                                                                    type MockFunctionResultThrow = {
                                                                                                    type: 'throw';
                                                                                                    /**
                                                                                                    * Result of a single call to a mock function that threw.
                                                                                                    */
                                                                                                    value: unknown;
                                                                                                    };

                                                                                                      type MockFunctionState

                                                                                                      type MockFunctionState<T extends FunctionLike = UnknownFunction> = {
                                                                                                      /**
                                                                                                      * List of the call arguments of all calls that have been made to the mock.
                                                                                                      */
                                                                                                      calls: Array<Parameters<T>>;
                                                                                                      /**
                                                                                                      * List of all the object instances that have been instantiated from the mock.
                                                                                                      */
                                                                                                      instances: Array<ReturnType<T>>;
                                                                                                      /**
                                                                                                      * List of all the function contexts that have been applied to calls to the mock.
                                                                                                      */
                                                                                                      contexts: Array<ThisParameterType<T>>;
                                                                                                      /**
                                                                                                      * List of the call order indexes of the mock. Jest is indexing the order of
                                                                                                      * invocations of all mocks in a test file. The index is starting with `1`.
                                                                                                      */
                                                                                                      invocationCallOrder: Array<number>;
                                                                                                      /**
                                                                                                      * List of the call arguments of the last call that was made to the mock.
                                                                                                      * If the function was not called, it will return `undefined`.
                                                                                                      */
                                                                                                      lastCall?: Parameters<T>;
                                                                                                      /**
                                                                                                      * List of the results of all calls that have been made to the mock.
                                                                                                      */
                                                                                                      results: Array<MockFunctionResult<T>>;
                                                                                                      };

                                                                                                        type MockMetadata

                                                                                                        type MockMetadata<T, MetadataType = MockMetadataType> = {
                                                                                                        ref?: number;
                                                                                                        members?: Record<string, MockMetadata<T>>;
                                                                                                        mockImpl?: T;
                                                                                                        name?: string;
                                                                                                        refID?: number;
                                                                                                        type?: MetadataType;
                                                                                                        value?: T;
                                                                                                        length?: number;
                                                                                                        };

                                                                                                          type MockMetadataType

                                                                                                          type MockMetadataType =
                                                                                                          | 'object'
                                                                                                          | 'array'
                                                                                                          | 'regexp'
                                                                                                          | 'function'
                                                                                                          | 'constant'
                                                                                                          | 'collection'
                                                                                                          | 'null'
                                                                                                          | 'undefined';

                                                                                                            type OverloadedReturnType

                                                                                                            type OverloadedReturnType<T> = T extends {
                                                                                                            (...args: Array<any>): infer R1;
                                                                                                            (...args: Array<any>): infer R2;
                                                                                                            (...args: Array<any>): infer R3;
                                                                                                            (...args: Array<any>): infer R4;
                                                                                                            }
                                                                                                            ? R1 | R2 | R3 | R4
                                                                                                            : T extends {
                                                                                                            (...args: Array<any>): infer R1;
                                                                                                            (...args: Array<any>): infer R2;
                                                                                                            (...args: Array<any>): infer R3;
                                                                                                            }
                                                                                                            ? R1 | R2 | R3
                                                                                                            : T extends {
                                                                                                            (...args: Array<any>): infer R1;
                                                                                                            (...args: Array<any>): infer R2;
                                                                                                            }
                                                                                                            ? R1 | R2
                                                                                                            : T extends (...args: Array<any>) => infer R
                                                                                                            ? R
                                                                                                            : never;

                                                                                                              type PropertyLikeKeys

                                                                                                              type PropertyLikeKeys<T> = Exclude<
                                                                                                              keyof T,
                                                                                                              ConstructorLikeKeys<T> | MethodLikeKeys<T>
                                                                                                              >;

                                                                                                                type RejectType

                                                                                                                type RejectType<T extends FunctionLike> = [
                                                                                                                Extract<OverloadedReturnType<T>, PromiseLike<any>>
                                                                                                                ] extends [never]
                                                                                                                ? never
                                                                                                                : unknown;

                                                                                                                  type ResolveType

                                                                                                                  type ResolveType<T extends FunctionLike> = [
                                                                                                                  Extract<OverloadedReturnType<T>, PromiseLike<any>>
                                                                                                                  ] extends [never]
                                                                                                                  ? never
                                                                                                                  : Extract<OverloadedReturnType<T>, PromiseLike<any>> extends PromiseLike<infer U>
                                                                                                                  ? U
                                                                                                                  : never;

                                                                                                                    type Spied

                                                                                                                    type Spied<T extends ClassLike | FunctionLike> = T extends ClassLike
                                                                                                                    ? SpiedClass<T>
                                                                                                                    : T extends FunctionLike
                                                                                                                    ? SpiedFunction<T>
                                                                                                                    : never;

                                                                                                                      type SpiedClass

                                                                                                                      type SpiedClass<T extends ClassLike = UnknownClass> = MockInstance<
                                                                                                                      (...args: ConstructorParameters<T>) => InstanceType<T>
                                                                                                                      >;

                                                                                                                        type SpiedFunction

                                                                                                                        type SpiedFunction<T extends FunctionLike = UnknownFunction> = MockInstance<
                                                                                                                        (...args: Parameters<T>) => ReturnType<T>
                                                                                                                        >;

                                                                                                                          type SpiedGetter

                                                                                                                          type SpiedGetter<T> = MockInstance<() => T>;

                                                                                                                            type SpiedSetter

                                                                                                                            type SpiedSetter<T> = MockInstance<(arg: T) => void>;

                                                                                                                              type UnknownClass

                                                                                                                              type UnknownClass = new (...args: Array<unknown>) => unknown;

                                                                                                                                type UnknownFunction

                                                                                                                                type UnknownFunction = (...args: Array<unknown>) => unknown;

                                                                                                                                  Package Files (1)

                                                                                                                                  Dependencies (4)

                                                                                                                                  Dev Dependencies (0)

                                                                                                                                  No dev dependencies.

                                                                                                                                  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.

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