Mock 3rd party library constructor with jest

末鹿安然 提交于 2019-12-05 23:07:58
CharybdeBE

I finally managed to do something about it. I have created a mock module manually (because jest.genmockfromModule does not seem to work)

jest.mock ('popper.js', () =>
{
  class Popper {
    constructor(a,b,c){
      this.spy(a,b,c);
    }
    spy(a,b,c) {}
    destroy() {}
  }
  return Popper;
});

The spy function is the one that you can "spyOn" when you want to know if the constructor has been called with the good parameters

(here you have 3 arguments because of popper.js)

Thus I use it like so in my spec file :

import Popper from 'popper.js';
 ...
jest.spyOn(Popper.prototype, 'spy');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!