Nuxt.js: Cannot read property $loading of undefined when using jest with nuxt component

Created on 5 Mar 2019  路  2Comments  路  Source: nuxt/nuxt.js

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 question is available on Nuxt community (#c8773)

Most helpful comment

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

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

All 2 comments

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: () => {}
      }
    }
  }
})
Was this page helpful?
0 / 5 - 0 ratings

Related issues

vadimsg picture vadimsg  路  3Comments

gary149 picture gary149  路  3Comments

vadimsg picture vadimsg  路  3Comments

VincentLoy picture VincentLoy  路  3Comments

bimohxh picture bimohxh  路  3Comments