我正在使用 Nuxt 和 Nuxt-Fire (https://github.com/lupas/nuxt-fire)
当我启动测试时,我收到此错误 [Vue warn]: Error in config.errorHandler: "TypeError: Cannot read property 'ref' of undefined"
发生这种情况是因为我的应用程序中的这个部分
mounted() {
this.initiate(window.instaroomId)
let connected = this.$fireDb.ref(".info/connected")
this.getConnection(connected)
},
看起来 this.$fireDb 没有被调用。该模块通常在 nuxt.config.js 中加载。我怎样才能使这项工作?
最佳答案
如果要测试,那this.$fireDb.ref(".info/connected")
被称为你可以像这样模拟它:
import { shallowMount } from '@vue/test-utils'
import SomeComponent from '@/components/SomeComponent/SomeComponent.vue'
let wrapper
describe('SomeComponent.vue Test', () => {
beforeEach(() => {
wrapper = shallowMount(SomeComponent, {
mocks: {
$fireDb: {
ref: jest.fn()
}
}
})
})
it('$fireDb.ref was called', () => {
expect(wrapper.vm.$fireDb.ref).toBeCalled()
expect(wrapper.vm.$fireDb.ref).toBeCalledWith('.info/connected')
})
})
或者,如果您希望测试通过 created()
Hook 并测试您可以模拟的另一个功能 $fireDb.ref
没有测试它被调用。
关于vue.js - Jest + Nuxt + Nuxt-Fire 在测试套件中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53885177/