Nuxt.js 中的 Jest 全局插件

Posted

技术标签:

【中文标题】Nuxt.js 中的 Jest 全局插件【英文标题】:Jest Global Plugin in Nuxt.js 【发布时间】:2021-06-04 19:20:25 【问题描述】:

我正在尝试模拟一个在 jest 单元测试中的 created() 挂钩上运行的插件,但是,我不断返回 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
)

我做错了吗?

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

以上是关于Nuxt.js 中的 Jest 全局插件的主要内容,如果未能解决你的问题,请参考以下文章