When I try to test one of my components using nuxt and jest, I get the following error:
Cannot read property '$loading' of undefined
This is being caused by the following line of code in my component
this.$nuxt.$loading.start()
How do I prevent this error from occuring when running the test on my component?
The test file looks like this:
import { mount } from '@vue/test-utils'
import Converter from '@/components/Converter.vue'
describe('Converter', () => {
test('is a Vue instance', () => {
const wrapper = mount(Converter)
expect(wrapper.isVueInstance()).toBeTruthy()
})
})
This issue as been imported as question since it does not respect nuxt.js issue template. Only bug reports and feature requests stays open to reduce maintainers workload.
If your issue is not a question, please mention the repo admin or moderator to change its type and it will be re-opened automatically.
Your question is available at https://cmty.app/nuxt/nuxt.js/issues/c8773.
I found a solution. The solution is to mock nuxt like this:
const wrapper = mount(Converter, {
mocks: {
$nuxt: {
$loading: {
start: () => {}
}
}
}
})
Most helpful comment
I found a solution. The solution is to mock nuxt like this: