vue.js - Jest 无法加载模块 vue-cookies

标签 vue.js jestjs

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 的模块提供

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

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

最佳答案

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

关于vue.js - Jest 无法加载模块 vue-cookies,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56377777/

相关文章:

node.js - 为 vue-cli 运行 "npm run dev"时出现跨环境问题

javascript - Vue 将输入规则的箭头函数传递给 Stancil 组件

javascript - Vue.js vuex props 不重新渲染

reactjs - 对于连接的组件, enzyme wrapper.instance()为空

javascript - 如何使用 Jest 和 Enzyme 测试 getDerivedStateFromProps

Vue.js 将字符串文字中的 `<` 和 `>` 解释为自定义元素

vue.js - vue js 的响应式图像网格库

reactjs - 有没有办法使用 webpack 和 mocha 在我的单元测试中模拟 `process.env.NODE_ENV` ?

reactjs - JEST 测试套件无法运行 : TypeError: (0 , _reactRedux.connect) 不是函数

javascript - 在 Jest 中,如何使测试失败?