使用vue-test-utils和jest使用方法参数测试创建的钩子函数调用

我正在尝试测试vue组​​件,其中在创建的钩子中进行了服务调用,并传递了来自vue组件的methods对象的函数。例如

....

created() {
  GlobalMessageRegistry.addUpdateListener(this.update)
},methods: {
  update(updateMessages) {
     //do stuff
  },},....

我想测试通过传递this.update来调用GlobalMessageRegistry.addUpdateListener,以便我的笑话测试看起来像这样(注意GlobalMessageRegistry在测试中已正确模拟)

....

it('Adds update as a GlobalMessageRegistry update listener,() => {
  const mockUpdate = jest.fn();
  const component = shallowMount(MyComponent,{
    methods: {
      update: mockUpdate
    },});
  expect(GlobalMessageRegistry.addUpdateListener).toHaveBeenCalledWith(mockUpdated);  
});

....
cuterose 回答:使用vue-test-utils和jest使用方法参数测试创建的钩子函数调用

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3158367.html

大家都在问