如何使用Jest在Vue JS中测试void函数?

时间:2020-02-20 07:31:55

标签: javascript vue.js jestjs nuxt.js

说我有一个名为async fetchItem() { this.meeting = await this.$MeetingApi.getMeeting( this.$route.params.id, false ) this.formReady = true this.meeting.initAnswerBills() } 的异步方法,该方法调用api等待其返回值并将表单设置为ready。

REGEXP_EXTRACT(Field, "^P\\. \\d+ - (.*)/./.*/./.*$")

我该如何使用Jest异步和模拟功能对此进行测试?我阅读了文档,但仍然不知道如何实施测试。

1 个答案:

答案 0 :(得分:0)

这个监视方法的实现对我有用:

test('fetchItem is called properly',  async () => {

  let wrapper = shallowMount(YourComponent)

  const spy = jest.spyOn(wrapper.vm, 'fetchItem')
    .mockImplementation(() => {
      expect(spy).toHaveBeenCalledWith()
  })
})