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 () => {
const mockInit = () => jest.fn();
jest.mock('./ivs-player', () => {
return {
init: mockInit,
};
});
await createPlayers();
expect(?????).toHaveBeenCalled();
});
What do I put in for the expect to listen to since I mocked the ivs-player
I think your mocking is wrong. Try this (mock on the prototype)
it('should load the player', async () => {
IVSPlayer.prototype.init = jest.fn().mockImplementation(() => {
return {
init: jest.fn(),
};
});
// execute code that causes the init function to run
expect(IVSPlayer.prototype.init).toHaveBeenCalled();
});
Alternatively, you can try this
import { IVSPlayer } from './ivs-player';
jest.mock('./ivs-player'); // Automatic mock of the file
it('should load the player', async () => {
// execute code that causes the init function to run
const mockInit = IVSPlayer.mock.instances[0].init;
expect(mockInit).toHaveBeenCalled();
});
You can read more on class mocks here .
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.