I am currently trying to test my vue component with Jest. I have a beforeMount hook in my index.vue file which looks like that
beforeMount() {
this.ProjectName = this.$route.query.ProjectName
this.loadOutputs()
}
With the method loadOutputs()
loadOutputs() {
this.Project.name
const path = 'http://localhost:5000/loadoutputs'
axios
.post(path)
.then((res) => {
this.Results = res.data
})
}
I am trying to write a test but I cannot find how to mock the beforeMount hook in my wrapper
import { shallowMount, createLocalVue } from '@vue/test-utils'
import My_Page from '@/views/test/index'
import ProjectInputs from '../json/Project_inputs.json'
import ProjectStatusInputs from '../json/Project_status.json'
import Project_Results from '../json/Project.json'
import Vue from 'vue'
import axios from 'axios'
import Vuex from 'vuex'
jest.mock('axios')
describe('My_Page', () => {
const localVue = createLocalVue()
localVue.use(Vuex)
localVue.use(ElementUI)
let My_PageId = 1
const $router = {
push: jest.fn(),
}
let store = new Vuex.Store({
state: {
Project: ProjectInputs,
ProjectStatus: ProjectStatusInputs
}
})
const wrapper = shallowMount(My_Page, {
localVue,
store,
mocks: {
$router,
$route: {
params: {
My_PageId : My_PageId ,
},
query: {
ProjectName: 'Name'
}
}
}
})
And it always gives me the same error
Cannot read property 'variable' of undefined
because it does not mock the Results variable. I have an example of the Results variable in Project_Results but I do not know how to put it into my wrapper.
Any idea?