Last active
November 1, 2019 01:00
-
-
Save McCauliflower/d5dac5952d5f9c72b879353349856757 to your computer and use it in GitHub Desktop.
Mock Vuex modules with Jest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Jest Automock Features with Vuex Modules | |
const store = jest.genMockFromModule(‘@/store/index.js').default | |
// and to mount | |
wrapper = shallowMount(SideFilters, { | |
store, | |
localVue | |
}) | |
Manual Mocking helper with Jest if your running into issues with the automock (mock all your modularized actions) | |
/* | |
* Creates a mock-store config from a real-store config. | |
* @param store Actual vuex store you want to mock | |
*/ | |
export const mockActions = (store: any) => { | |
const modules = store.modules | |
if (modules) { | |
return { | |
state: store.state, | |
modules: Object.keys(modules).reduce((accumulator: any, key: string) => { | |
const { actions, ...stateGettersAndMutations } = modules[key] | |
const mockedActions = Object.keys(actions).reduce((acc: any, actionKey: string) => { | |
acc[actionKey] = jest.fn().mockResolvedValue('-'); | |
return acc; | |
}, {}) | |
accumulator[key] = { namespaced: true, 'actions': mockedActions, ...stateGettersAndMutations } | |
return accumulator | |
}, {}) | |
} | |
} else { | |
return console.error('No modules found') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment