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/