Mock dependency in jest with typescript

前端 未结 9 1063
梦谈多话
梦谈多话 2020-12-07 12:03

When testing a module that has a dependency in a different file. When assigning that module to be jest.Mock typescript gives an error that the method mock

9条回答
  •  鱼传尺愫
    2020-12-07 12:30

    I have found this in @types/jest:

    /**
      * Wrap a function with mock definitions
      *
      * @example
      *
      *  import { myFunction } from "./library";
      *  jest.mock("./library");
      *
      *  const mockMyFunction = myFunction as jest.MockedFunction;
      *  expect(mockMyFunction.mock.calls[0][0]).toBe(42);
    */
    

    Note: When you do const mockMyFunction = myFunction and then something like mockFunction.mockReturnValue('foo'), you're a changing myFunction as well.

    Source: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/jest/index.d.ts#L1089

提交回复
热议问题