【问题标题】:Jest unable to load module vue-cookies开玩笑无法加载模块 vue-cookies
【发布时间】:2019-05-30 11:50:38
【问题描述】:
console.error node_modules/vue/dist/vue.runtime.common.dev.js:621
  [Vue warn]: Error in mounted hook (Promise/async): "TypeError: Cannot read property 'get' of undefined"

  found in

  ---> <Anonymous>
         <Root>
console.error node_modules/vue/dist/vue.runtime.common.dev.js:1884
  TypeError: Cannot read property 'get' of undefined
      at VueComponent.mounted (/home/ubuntu/vue-testing-skel/src/components/ChatApp.vue:66:1)

导致错误的行:

    this.current_nickname = this.$cookies.get('nickname')

this.$cookies 由一个名为 vue-cookies 的模块提供

我已将它安装并保存在我的 devDependencies 中,但似乎 jest 无法找到或加载它。

我不确定我必须做些什么来确保 Jest 正确加载这些模块。

【问题讨论】:

    标签: vue.js jestjs


    【解决方案1】:

    使用 Jest 进行测试是独立的。这意味着像 this.$cookies 这样的全局对象不可用,因为它们与浏览器中的 cookie 交互。解决这个问题的方法是模拟全局函数。有关如何在此处执行此操作的更多信息: https://lmiller1990.github.io/vue-testing-handbook/mocking-global-objects.html#example-with-vue-i18n

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-13
      • 1970-01-01
      • 2017-12-19
      • 2020-03-05
      • 1970-01-01
      • 2021-05-30
      • 2020-02-06
      相关资源
      最近更新 更多