首页 > 解决方案 > Nuxt.js 中的 Jest 全局插件

问题描述

我正在尝试模拟一个在created()jest 单元测试中的钩子上运行的插件,但是,我不断返回TypeError: this.$translations is not a function,这导致我的所有测试都因该错误而失败:

文件内Component

created () {
  this.$translations(this.page.translations)
}

文件内Test

import { translations } from '~/helpers/plugins/translations'

const localVue = createLocalVue()
localVue.use(translations)

const wrapper = shallowMount(TestComponent, {
  localVue,
  propsData
})

难道我做错了什么?

标签: vue.jsjestjsnuxt.js

解决方案


我已经解决了这个问题,我要做的是mocks用来模拟this.$translations()作为一个函数:

const wrapper = shallowMount(TBase, {
  localVue,
  mocks: { $translations: () => {} },
  propsData
})

推荐阅读