我正在尝试使用Vest中的Jest测试某些组件。我正在用shallowMount进行操作,因此它呈现了整个组件。我已经阅读了几本有关如何模拟或测试axios的文章,但找不到与main.js
我收到此错误:
TypeError: Cannot read property 'get' of undefined
71 | getInterventions(){
72 | this.isLoading = true;
> 73 | window.api.get('route/' + this.$store.state.user.id + '/restOfTheRoute', {
| ^
74 | headers: {
75 | 'X-Auth-Token': this.$store.state.user.apiToken
76 | }
我用window.api = axios
调用该方法,因为我在main.js
中使用了几个API
window.api = axios.create({
baseURL: 'apiLink'
})
我将显示test.js
,因为可能的重复不是我什至尝试的。
import { mount, shallowMount, createLocalVue } from '@vue/test-utils'
import Carte from '@/views/Carte.vue'
import HereMap from "@/components/HereMap.vue"
import SideBar from "@/components/Sidebar.vue"
import Vuex from 'vuex';
import vuetify from "vuetify"
import axios from 'axios'
jest.mock('axios');
const localVue = createLocalVue();
localVue.use(vuetify);
localVue.use(Vuex);
const mutations = {
testMutation: jest.fn()
}
const store = new Vuex.Store({ mutations })
const wrapper = shallowMount(Carte, {
store, localVue
});
describe('Carte.vue', () => {
it('needs to render the component', () => {
expect(shallowMount(Carte).isVueInstance()).toBe(true);
});
it('check if HereMap exists', ()=>{
expect(wrapper.contains(HereMap)).toBe(true);
});
it('check if SideBar component exists', ()=>{
expect(wrapper.contains(SideBar)).toBe(true);
});
});
我有also tried和:
const api = jest.fn()
Object.defineProperty(window, 'api', api);
如何在window.api
文件中注册Component.test.js
?