JS/Jest: mocking a class to check if method called

  javascript, jestjs

I am trying to test the loading of a player in my main js file. It just created a new instance of an IVSPlayer class and then calls init() on it.
MAIN.js

const ivsPlayer = new IVSPlayer({
  id: VIDEO_PLAYER_ID,
  config: VIDEO_JS_CONFIG,
  ivsTech: win.registerIVSTech,
});
ivsPlayer.init();

player = ivsPlayer.player;

I am trying to mock the implementation below

MAIN.test.js

import IVSPlayer from './ivs-player';
  //

  it('should load the  player', async () => {

    IVSPlayer.init = jest.fn().mockImplementation(() => {
      return {
        init: jest.fn(),
      }
    });

    expect(IVSPlayer.init).toHaveBeenCalled();
  });

but I am getting the following error:

    expect(IVSPlayer.init).toHaveBeenCalled()

    Expected number of calls: >= 1
    Received number of calls:    0

Source: Ask Javascript Questions

LEAVE A COMMENT