Search code examples
unit-testingvue.jsjestjsnuxt.jsvue-test-utils

Cannot read property $loading of undefined when using Jest with Nuxt component


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 occurring 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()
  })
})

Solution

  • I found a solution. The solution is to mock nuxt like this:

    const wrapper = mount(Converter, {
      mocks: {
        $nuxt: {
          $loading: {
            start: () => {}
          }
        }
      }
    })