【问题标题】:Jest Global Plugin in Nuxt.jsNuxt.js 中的 Jest 全局插件
【发布时间】: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
})

我做错了吗?

【问题讨论】:

    标签: vue.js jestjs nuxt.js


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-04-01
      • 1970-01-01
      • 2021-09-17
      • 2017-03-19
      • 2021-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      相关资源
      最近更新 更多