1

Have a Vue app with axios setup and is using it as this.$http.get in the components and that is working fine.

In running the specs using Jest I get an error: Cannot read property 'get' of undefined

I know I can mock axios with maxios. How do I get Jest to recognise $http so it does not throw an error?

1 Answer 1

1

You'll need to create a local instance of Vue for your tests, to which you introduce the Vue Plugins you are using, such as axios.

import { createLocalVue, shallowMount } from "@vue/test-utils"
import axios from 'axios'
import VueAxios from 'vue-axios'


const localVue = createLocalVue();
localVue.use(VueAxios, axios)

and later, in your actual tests:

it('should mount the component where I want to use $http', () => {
    const wrapper = shallowMount(MyApiComponent, { localVue });
    ... do stuff to wrapper here ...
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply Georg. I have done as above. Unfortunately I still get TypeError: Cannot read property 'get' of undefined when running the test

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.