jest-mock

  • Version 27.4.6
  • Published
  • 38.9 kB
  • 2 dependencies
  • MIT license

Install

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

Overview

## API

Index

Variables

variable mocked

const mocked: {
<T>(item: T, deep?: false | undefined): MaybeMocked<T>;
<T_2>(item: T_2, deep: true): MaybeMockedDeep<T_2>;
};

    variable spyOn

    const spyOn: {
    <T extends {}, M extends NonFunctionPropertyNames<T>>(
    object: T,
    methodName: M,
    accessType: 'get'
    ): SpyInstance<T[M], []>;
    <T_2 extends {}, M_2 extends NonFunctionPropertyNames<T_2>>(
    object: T_2,
    methodName: M_2,
    accessType: 'set'
    ): SpyInstance<void, [T_2[M_2]]>;
    <T_4 extends {}, M_4 extends FunctionPropertyNames<T_4>>(
    object: T_4,
    methodName: M_4
    ): T_4[M_4] extends (...args: Array<any>) => any
    ? SpyInstance<ReturnType<T_4[M_4]>, Parameters<T_4[M_4]>>
    : never;
    };

      Functions

      function fn

      fn: <T, Y extends unknown[]>(implementation?: (...args: Y) => T) => Mock<T, Y>;

        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 fn

            fn: <T, Y extends unknown[]>(implementation?: (...args: Y) => T) => Mock<T, Y>;

              method generateFromMetadata

              generateFromMetadata: <T, Y extends unknown[]>(
              _metadata: MockFunctionMetadata<T, Y>
              ) => Mock<T, Y>;
              • 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, Y extends unknown[]>(
              component: T,
              _refs?: Map<T, number>
              ) => MockFunctionMetadata<T, Y> | null;
              • Parameter component

                The component for which to retrieve metadata.

                See Also

                • README.md

              method isMockFunction

              isMockFunction: <T>(fn: unknown) => fn is Mock<T, unknown[]>;

                method mocked

                mocked: {
                <T>(item: T, deep?: false): MaybeMocked<T>;
                <T>(item: T, deep: true): MaybeMockedDeep<T>;
                };

                  method resetAllMocks

                  resetAllMocks: () => void;

                    method restoreAllMocks

                    restoreAllMocks: () => void;

                      method spyOn

                      spyOn: {
                      <T extends {}, M extends NonFunctionPropertyNames<T>>(
                      object: T,
                      methodName: M,
                      accessType: 'get'
                      ): SpyInstance<T[M], []>;
                      <T extends {}, M extends NonFunctionPropertyNames<T>>(
                      object: T,
                      methodName: M,
                      accessType: 'set'
                      ): SpyInstance<void, [T[M]]>;
                      <T extends {}, M extends FunctionPropertyNames<T>>(
                      object: T,
                      methodName: M
                      ): T[M] extends (...args: any[]) => any
                      ? SpyInstance<ReturnType<T[M]>, Parameters<T[M]>>
                      : never;
                      };

                        Interfaces

                        interface Constructable

                        interface Constructable {}

                          construct signature

                          new (...args: Array<any>): any;

                            interface Mock

                            interface Mock<T, Y extends Array<unknown> = Array<unknown>>
                            extends Function,
                            MockInstance<T, Y> {}

                              construct signature

                              new (...args: Y): T;

                                call signature

                                (...args: Y): T;

                                  interface MockInstance

                                  interface MockInstance<T, Y extends Array<unknown>> {}

                                    property mock

                                    mock: MockFunctionState<T, Y>;

                                      method getMockImplementation

                                      getMockImplementation: () => Function | undefined;

                                        method getMockName

                                        getMockName: () => string;

                                          method mockClear

                                          mockClear: () => this;

                                            method mockImplementation

                                            mockImplementation: {
                                            (fn: (...args: Y) => T): this;
                                            (fn: () => Promise<T>): this;
                                            };

                                              method mockImplementationOnce

                                              mockImplementationOnce: {
                                              (fn: (...args: Y) => T): this;
                                              (fn: () => Promise<T>): 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: Unpromisify<T>) => this;

                                                          method mockResolvedValueOnce

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

                                                            method mockRestore

                                                            mockRestore: () => void;

                                                              method mockReturnThis

                                                              mockReturnThis: () => this;

                                                                method mockReturnValue

                                                                mockReturnValue: (value: T) => this;

                                                                  method mockReturnValueOnce

                                                                  mockReturnValueOnce: (value: T) => this;

                                                                    interface MockWithArgs

                                                                    interface MockWithArgs<T extends MockableFunction>
                                                                    extends MockInstance<ReturnType<T>, ArgumentsOf<T>> {}

                                                                      construct signature

                                                                      new (...args: ConstructorArgumentsOf<T>): T;

                                                                        call signature

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

                                                                          interface SpyInstance

                                                                          interface SpyInstance<T, Y extends Array<unknown>> extends MockInstance<T, Y> {}

                                                                            Type Aliases

                                                                            type ArgsType

                                                                            type ArgsType<T> = T extends (...args: infer A) => any ? A : never;

                                                                              type ArgumentsOf

                                                                              type ArgumentsOf<T> = T extends (...args: infer A) => any ? A : never;

                                                                                type ConstructorArgumentsOf

                                                                                type ConstructorArgumentsOf<T> = T extends new (...args: infer A) => any ? A : never;

                                                                                  type MaybeMocked

                                                                                  type MaybeMocked<T> = T extends MockableFunction
                                                                                  ? MockedFunction<T>
                                                                                  : T extends object
                                                                                  ? MockedObject<T>
                                                                                  : T;

                                                                                    type MaybeMockedConstructor

                                                                                    type MaybeMockedConstructor<T> = T extends new (...args: Array<any>) => infer R
                                                                                    ? MockInstance<R, ConstructorArgumentsOf<T>>
                                                                                    : T;

                                                                                      type MaybeMockedDeep

                                                                                      type MaybeMockedDeep<T> = T extends MockableFunction
                                                                                      ? MockedFunctionDeep<T>
                                                                                      : T extends object
                                                                                      ? MockedObjectDeep<T>
                                                                                      : T;

                                                                                        type MethodKeysOf

                                                                                        type MethodKeysOf<T> = {
                                                                                        [K in keyof T]: T[K] extends MockableFunction ? K : never;
                                                                                        }[keyof T];

                                                                                          type MockableFunction

                                                                                          type MockableFunction = (...args: Array<any>) => any;

                                                                                            type Mocked

                                                                                            type Mocked<T> = {
                                                                                            [P in keyof T]: T[P] extends (...args: Array<any>) => any
                                                                                            ? MockInstance<ReturnType<T[P]>, ArgsType<T[P]>>
                                                                                            : T[P] extends Constructable
                                                                                            ? MockedClass<T[P]>
                                                                                            : T[P];
                                                                                            } & T;

                                                                                              type MockedClass

                                                                                              type MockedClass<T extends Constructable> = MockInstance<
                                                                                              InstanceType<T>,
                                                                                              T extends new (...args: infer P) => any ? P : never
                                                                                              > & {
                                                                                              prototype: T extends {
                                                                                              prototype: any;
                                                                                              }
                                                                                              ? Mocked<T['prototype']>
                                                                                              : never;
                                                                                              } & T;

                                                                                                type MockedFunction

                                                                                                type MockedFunction<T extends MockableFunction> = MockWithArgs<T> & {
                                                                                                [K in keyof T]: T[K];
                                                                                                };

                                                                                                  type MockedFunctionDeep

                                                                                                  type MockedFunctionDeep<T extends MockableFunction> = MockWithArgs<T> &
                                                                                                  MockedObjectDeep<T>;

                                                                                                    type MockedObject

                                                                                                    type MockedObject<T> = MaybeMockedConstructor<T> & {
                                                                                                    [K in MethodKeysOf<T>]: T[K] extends MockableFunction
                                                                                                    ? MockedFunction<T[K]>
                                                                                                    : T[K];
                                                                                                    } & {
                                                                                                    [K in PropertyKeysOf<T>]: T[K];
                                                                                                    };

                                                                                                      type MockedObjectDeep

                                                                                                      type MockedObjectDeep<T> = MaybeMockedConstructor<T> & {
                                                                                                      [K in MethodKeysOf<T>]: T[K] extends MockableFunction
                                                                                                      ? MockedFunctionDeep<T[K]>
                                                                                                      : T[K];
                                                                                                      } & {
                                                                                                      [K in PropertyKeysOf<T>]: MaybeMockedDeep<T[K]>;
                                                                                                      };

                                                                                                        type MockFunctionMetadata

                                                                                                        type MockFunctionMetadata<
                                                                                                        T,
                                                                                                        Y extends Array<unknown>,
                                                                                                        Type = MockFunctionMetadataType
                                                                                                        > = {
                                                                                                        ref?: number;
                                                                                                        members?: Record<string, MockFunctionMetadata<T, Y>>;
                                                                                                        mockImpl?: (...args: Y) => T;
                                                                                                        name?: string;
                                                                                                        refID?: number;
                                                                                                        type?: Type;
                                                                                                        value?: T;
                                                                                                        length?: number;
                                                                                                        };

                                                                                                          type MockFunctionMetadataType

                                                                                                          type MockFunctionMetadataType =
                                                                                                          | 'object'
                                                                                                          | 'array'
                                                                                                          | 'regexp'
                                                                                                          | 'function'
                                                                                                          | 'constant'
                                                                                                          | 'collection'
                                                                                                          | 'null'
                                                                                                          | 'undefined';
                                                                                                          • Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.

                                                                                                            This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree.

                                                                                                          type PropertyKeysOf

                                                                                                          type PropertyKeysOf<T> = {
                                                                                                          [K in keyof T]: T[K] extends MockableFunction ? never : K;
                                                                                                          }[keyof T];

                                                                                                            Package Files (1)

                                                                                                            Dependencies (2)

                                                                                                            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>