首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >vue3/ts/jest/axios,如何模拟方法内的axios进行测试?

vue3/ts/jest/axios,如何模拟方法内的axios进行测试?

提问于 2022-08-10 12:22:33
回答 0关注 0查看 100
代码语言:js
复制
const checkEmail = (formEl: FormInstance | undefined) => {
            if (!formEl) return
            formEl.validate((valid) => {
                if (valid) {
                    axios({
                        url: data.api.loginCheckEmail,
                        method: "get",
                        params: {email: data.userForm.email}
                    }).then((res: any) => {
                        console.log(res)
                    })
                } else {
                    console.log('error submit!')
                    return false
                }
            })
        }

以上是一个方法内的axios的请求,我应该如何测试这个axios方法???

代码语言:js
复制
const wrapper = mount(Login, {
  global: {
    plugins: [i18n,ElementPlus,router],
  }
})
it("Test:login check Email is correct", async() => {
   router.push('/')
   await router.isReady()
   wrapper.find('.email-input input').setValue('my@mail.com')
   const spy = jest.spyOn(wrapper.vm, 'checkEmail')
   wrapper.find('.checkEmail').trigger('click')
   expect(spy).toHaveBeenCalled()
})

我尝试了jest.spyOn但是覆盖报告都没有显示测试已覆盖,是不是我哪里写的有问题?实际还没有生效?

希望知识渊博的人士能指导我一下,初入jest

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档