[Vuejs]-Setup test for Vue.js-electron app which uses Vuex

0👍

It looks like you need to instantiate Vuex within the test.

Your component has this created() hook, so just by creating it you are referencing the vuex instance.

created() {
  this.$store.commit('userStore/setDefaultUserStatus');
  this.$store.dispatch('userStore/getAllUsers');
},

You can use createLocalVue() to get an isolated Vue instance, and add Vuex to it, then pass it via mountOptions to mount().

You can also mock the store so that other tests have control over the data.

import Vuex from 'vuex'
import { mount, createLocalVue } from '@vue/test-utils'

const localVue = createLocalVue()
localVue.use(Vuex)

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

describe('user-information.vue', () => {

  let wrapper;
  beforeEach(() => {
    const mountOptions = {
      localVue,
      store: mockStore
    }
    wrapper = mount(UserInformation, mountOptions)
  });

  test('is a Vue instance', () => {
    expect(wrapper.isVueInstance()).toBeTruthy();
  });
});

Leave a comment