Mock dependency in jest with typescript

前端 未结 9 1070
梦谈多话
梦谈多话 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:31

    I use the pattern from @types/jest/index.d.ts just above the type def for Mocked (line 515):

    import { Api } from "../api";
    jest.mock("../api");
    
    const myApi: jest.Mocked = new Api() as any;
    myApi.myApiMethod.mockImplementation(() => "test");
    

提交回复
热议问题