将 Jest 与 Nuxt 组件一起使用时无法读取 属性 $loading of undefined

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

当我尝试使用 nuxt 和 jest 测试我的组件之一时,出现以下错误:

Cannot read property '$loading' of undefined

这是由我的组件中的以下代码行引起的

this.$nuxt.$loading.start()

如何防止在 运行 我的组件测试时发生此错误?

测试文件如下所示:

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

我找到了解决办法。解决方案是像这样模拟 nuxt:

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