相关疑难解决方法(0)

How to mock instance methods of a class mocked with jest.mock?

How can the instance methods be mocked for a class that is being mocked with jest.mock?

For example, a class Logger is mocked:

import Person from "./Person";
import Logger from "./Logger";

jest.mock("./Logger");

describe("Person", () => {
  it("calls Logger.method1() on instantiation", () => {
    Logger.method1.mockImplementation(() => {}) // This fails as `method1` is an instance method but how can the instance method be mocked here?
    new Person();
    
    expect(Logger.method1).toHaveBeenCalled();
  });
});
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing mocking jestjs

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

javascript ×1

jestjs ×1

mocking ×1

unit-testing ×1