How to mock a Vuex store in VueJS test-utils parentComponent

耗尽温柔 提交于 2019-12-02 09:33:55

Tried the solution Richard proposed but without much success, even though his guess was right.

The solution was far simnpler than I envisioned, I just stopped instantiating the Vuex.Store and just have the mocked $store in vue-test-utils config like so:

import { createLocalVue, shallowMount, config } from '@vue/test-utils';

config.mocks.$store = {
  state: {
    user: {
      sexy: true
    },
  },
};

I had no need to use an actual instance of Vuex as I only needed to mock the actual data so this worked perfectly.

How are you creating the mock store? It should be something like

const storeOptions = {
  state: {...},
  getters: {...},
  mutations: {...}
}
const mockStore = new Vuex.Store(storeOptions)

Since this.$store is undefined, I suspect you might just be passing the options object to shallowMount.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!