How to access namespaced Vuex getters in Mocha unit test How to access namespaced Vuex getters in Mocha unit test vue.js vue.js

How to access namespaced Vuex getters in Mocha unit test


Use modules with namespaced: true:

import { createLocalVue, shallowMount } from '@vue/test-utils';import Vuex from 'vuex';import PlayerFilterTable from '~/whatever';const localVue = createLocalVue();localVue.use(Vuex);let propsData, getters, store, wrapper, consoleSpy;describe('PlayerFilterTable', () => {  beforeEach(() => {    consoleSpy = jest.spyOn(console, 'error');    propsData = {      players: samplePlayerObject,      metadata: sampleMetadataObject    };    getters = {      allColumns: () => ["playerid", "last", "first", "birthday", "height"]    };    store = new Vuex.Store({      modules: {        playerFilter: {          namespaced: true,          getters        }      }    });    wrapper = shallowMount(PlayerFilterTable, {      propsData,      localVue,      store    });  });  afterEach(() => {    expect(consoleSpy).not.toHaveBeenCalled();  });  it('should render correctly', () => {    expect(wrapper.is(PlayerFilterTable)).toBe(true);    expect(wrapper.html()).toMatchSnapshot();  })})

If you use getters from more than one module you could group them up under different props of getters and assign to each module accordingly.