最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jest spyOn not working for ES6 class methods? - Stack Overflow

programmeradmin2浏览0评论

I have created a class with some methods and I want to mock the methods inside that class so I tried to use spyOn() but it is not working any idea what could be wrong? I am using myClass.prototype instead of myClass in spyOn() but still it does not work.

class myClass {

    constructor(){

    }

    _methodA () {

    }

    _methodB() {

    }

    main () {
        const res1 = _methodA();
        const res2 = _methodB();
    
    }

}

tests:

it('Testing' , () => {

// Some mock data passed below

    jest.spyOn(myClass.prototype, '_methodA').mockReturnValue(mockData1)
    jest.spyOn(myClass.prototype, '_methodB').mockReturnValue(mockData2)

    const obj = new myClass();
    obj.main();

    expect(myClass._methodA).toHaveBeenCalledTimes(1);
    expect(myClass._methodB).toHaveBeenCalledTimes(1);

});

I have created a class with some methods and I want to mock the methods inside that class so I tried to use spyOn() but it is not working any idea what could be wrong? I am using myClass.prototype instead of myClass in spyOn() but still it does not work.

class myClass {

    constructor(){

    }

    _methodA () {

    }

    _methodB() {

    }

    main () {
        const res1 = _methodA();
        const res2 = _methodB();
    
    }

}

tests:

it('Testing' , () => {

// Some mock data passed below

    jest.spyOn(myClass.prototype, '_methodA').mockReturnValue(mockData1)
    jest.spyOn(myClass.prototype, '_methodB').mockReturnValue(mockData2)

    const obj = new myClass();
    obj.main();

    expect(myClass._methodA).toHaveBeenCalledTimes(1);
    expect(myClass._methodB).toHaveBeenCalledTimes(1);

});
Share Improve this question edited Dec 8, 2020 at 14:35 jonrsharpe 122k30 gold badges267 silver badges474 bronze badges asked Dec 8, 2020 at 14:35 rock stonerock stone 4933 gold badges10 silver badges21 bronze badges 6
  • Don't spy on the thing you're supposed to be testing. The system under test is the class, internal method usage is an implementation detail. – jonrsharpe Commented Dec 8, 2020 at 14:37
  • @jonrsharpe Sorry did not get your point can you please ellaborate? – rock stone Commented Dec 8, 2020 at 14:39
  • @jonrsharpe Do you mean to say I should not test methods inside class? – rock stone Commented Dec 8, 2020 at 14:40
  • 1 I mean you shouldn't test that main uses other methods inside the same class, test the overall behaviour instead. It seems like methodA and methodB are effectively private, and private methods shouldn't be tested directly. – jonrsharpe Commented Dec 8, 2020 at 14:55
  • @jonrsharpe How to test private methods or we should not test private methods? – rock stone Commented Dec 8, 2020 at 15:13
 |  Show 1 more ment

1 Answer 1

Reset to default 6

You can spy the class instance methods:

it("Testing", () => {
  // Some mock data passed below
  const obj = new myClass();

  const spyA = jest.spyOn(obj, "_methodA").mockReturnValue({});
  const spyB = jest.spyOn(obj, "_methodB").mockReturnValue({});

  obj.main();

  expect(spyA).toHaveBeenCalledTimes(1);
  expect(spyB).toHaveBeenCalledTimes(1);
});

You can spy the class methods:

it("Testing", () => {
  // Some mock data passed below
  const spyA = jest.spyOn(myClass.prototype, "_methodA").mockReturnValue({});
  const spyB = jest.spyOn(myClass.prototype, "_methodB").mockReturnValue({});

  const obj = new myClass(); 
  obj.main();

  expect(spyA).toHaveBeenCalledTimes(1);
  expect(spyB).toHaveBeenCalledTimes(1);
});

The other case using Db data is possible too:

it("Testing", () => {
  const obj = new classA();
  obj.main();
  const spyA = jest.spyOn(DBClass.prototype, "getData").mockReturnValue({}); // DBClass could be a db class or a mock class
  expect(spyA).toHaveBeenCalledTimes(1);
});
发布评论

评论列表(0)

  1. 暂无评论