vue.js - Vue 测试工具在测试中无法识别 $http
问题描述
有一个带有 axios 设置的 Vue 应用程序,并且this.$http.get
在组件中使用它,并且工作正常。
在使用 Jest 运行规范时,出现错误:Cannot read property 'get' of undefined
我知道我可以用 maxios 模拟 axios。如何让 Jest 识别 $http 使其不会引发错误?
解决方案
您需要为您的测试创建一个本地 Vue 实例,向其中介绍您正在使用的 Vue 插件,例如 axios。
import { createLocalVue, shallowMount } from "@vue/test-utils"
import axios from 'axios'
import VueAxios from 'vue-axios'
const localVue = createLocalVue();
localVue.use(VueAxios, axios)
后来,在您的实际测试中:
it('should mount the component where I want to use $http', () => {
const wrapper = shallowMount(MyApiComponent, { localVue });
... do stuff to wrapper here ...
})
推荐阅读
- powerbi - 在 PowerBI 桌面上访问问答?
- python - BotFramework Composer,在技能回调时收到 404 错误
- aframe - 如何在 AR.JS 中使画布全屏
- amazon-s3 - 如何通过 CLI 关闭 AWS s3 存储桶中对象的加密?
- html - spring 中的等效布局(asp.net 核心)是什么?
- android - 在 Android 中获取未知 URL 内容
- c# - 无法通过 FormData Append 传递 HTML 内容
- c++ - 如何将这些串行端口的参数传递给函数
- node.js - 如何修复“TypeError:Buffer.from 不是函数”?
- python - 静态文件在 django nginx 中不起作用