I'm trying to test some components with Jest in Vue. I'm doing it with shallowMount so it renders the whole component. I have read a couple of texts of how to mock or test axios but I don't find any that does it like I do in the main.js
I'm getting this error:
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 | }
I call that method with window.api = axios
because I use a couple of API's in my main.js
window.api = axios.create({
baseURL: 'apiLink'
})
I'll show my test.js
because that possible duplicate is not what I'm even trying.
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);
});
});
I have also tried with:
const api = jest.fn()
Object.defineProperty(window, 'api', api);
How can I register window.api
in my Component.test.js
file?