boenfu / vuex-along

:memo: auto save and restore state for vuex
https://boenfu.github.io/vuex-along/
MIT License
259 stars 36 forks source link

单元测试中,如何 mock vuex-along #40

Closed lizhongyue248 closed 4 years ago

lizhongyue248 commented 4 years ago

通过 jest.mock('vuex-along') 无法 mock,通过

jest.mock('vuex-along', () => {})`

mock import 的部分通过了,但是调用的那里就会报错了。。。

   TypeError: (0 , _vuexAlong.default) is not a function

      10 |     base: Base
      11 |   },
    > 12 |   plugins: [createVuexAlong({
         |             ^
      13 |     name: 'auth-center',
      14 |     local: {
      15 |       list: ['base']

请问下如何 mock 比较好呢?谢谢~

boenfu commented 4 years ago

不直接使用依赖本身是因为没有 localstorage 吗 https://stackoverflow.com/a/32911774

lizhongyue248 commented 4 years ago

是因为单元测试里面是使用的 shallowmount,不会去加载模块依赖。所以只能 mock vuex-along 模块,但是他是使用 import createVuexAlong from 'vuex-along' 进行导入的,不知道如何mockcreateVuexAlong` 这个方法。

boenfu commented 4 years ago

有试过 jest.mock('vuex-along', () => () => {}) 吗,因为 vuex 插件需要的是个函数

lizhongyue248 commented 4 years ago

感谢!使用 jest.mock('vuex-along', () => () => {}) mock 通过!~

boenfu commented 4 years ago

哈哈 没事 ,新年快乐