简体   繁体   中英

Jest - how to mock a method within a function

I have an ES6 class A, that I need to test, the only thing I want to change is the implementation of one method, and all the other things to remain as they are.

First, I wanted manually to create a mock class like "myMock extends A" where I would change that method and use it for tests, but there were some problems.

Anyway, is there another way with jest to specify replacement of a specific method within a class?

What I tried to do now is following:

jest.mock("../myclass", () => {
return jest.fn().mockImplementation(() => {
    return {
        loadPopularItems: jest.fn(() => new Promise((resolve): void => 
           resolve()))
    };
  });
 });

This now gives me an error that myclass is not a constructor:

TypeError: myclass`.default is not a constructor

All I want to do is remove some async method from there, that makes HTTP requests.

You can stub out the method on the prototype in your test:

A.prototype.loadPopularItems = jest.fn()

Or you can use jest.spyOn to allow for easily resetting mocks:

jest.spyOn(A.prototype, "loadPopularItems");
jest.restoreAllMocks(); // avoids having to manually track mocks

Better yet, have your class accept whatever object/function makes HTTP requests as a constructor argument:

class A {
  constructor(http) {
    this.http = http;
  }

  loadPopularItems() {
     return this.http.fetchSomething();
  }
}

Then in your tests you can just pass a fake implementation:

const fakeHttp = {
  fetchSomething: jest.fn()
};

const instance = new A(fakeHttp);

This is generally the preferable over stubbing out methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM