Created
August 8, 2018 12:52
-
-
Save matvaleriano/d05f6b4013c81c00067f93952c49e7f8 to your computer and use it in GitHub Desktop.
Helpers for Jest + Vue tests
This file contains hidden or 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
// set this file into the store -> store/__mocks__/index.js | |
import Vue from 'vue' | |
import Vuex from 'vuex' | |
// your mocks | |
import state from './state' | |
import * as getters from './getters' | |
import mutations from './mutations' | |
import * as actions from './actions' | |
Vue.use(Vuex) | |
// eslint-disable-next-line no-underscore-dangle | |
export function createStoreMocks (custom = { | |
getters: {}, mutations: {}, actions: {}, state: {} | |
}) { | |
const mockGetters = { ...getters, ...custom.getters } | |
const mockMutations = { ...mutations, ...custom.mutations } | |
const mockActions = { ...actions, ...custom.actions } | |
const mockState = { ...state, ...custom.state } | |
return { | |
getters: mockGetters, | |
mutations: mockMutations, | |
actions: mockActions, | |
state: mockState, | |
store: new Vuex.Store({ | |
getters: mockGetters, | |
mutations: mockMutations, | |
actions: mockActions, | |
state: mockState | |
}) | |
} | |
} | |
export const store = createStoreMocks().store |
This file contains hidden or 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
import Vuex from 'vuex' | |
import { createLocalVue, shallowMount } from '@vue/test-utils' | |
import { createStoreMocks } from '~/store/__mocks__' | |
export const configureLocalVue = ({ plugins, mixins, localVue }) => { | |
// add plugins passed on params | |
for (let plugin of plugins) { | |
localVue.use(plugin) | |
} | |
// add mixins passed on params | |
for (let mixin of mixins) { | |
localVue.mixin(mixin) | |
} | |
// insert here your directives + default plugins | |
// ex.: | |
// localVue.use(VueRouter) | |
} | |
export const configureStoreMocks = ({ localVue }) => { | |
// Tell Jest to use the mock | |
// implementation of the store. | |
jest.mock('~/store') | |
// use vuex | |
localVue.use(Vuex) | |
// return mocked store | |
return createStoreMocks() | |
} | |
export const mountComponent = ({ | |
plugins = [], | |
component, | |
mountOptions, | |
mixins = [], | |
localVue = createLocalVue() | |
}) => { | |
configureLocalVue({ | |
plugins, | |
mixins, | |
localVue | |
}) | |
// mount component with options | |
return shallowMount( | |
component, | |
{ | |
localVue, | |
...mountOptions | |
}) | |
} |
This file contains hidden or 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
import { createLocalVue } from '@vue/test-utils' | |
import { mountComponent, configureStoreMocks } from './helpers' | |
import Foo from '~/components/Foo.vue' | |
describe('Company', () => { | |
let wrapper | |
let storeMocks | |
beforeEach(() => { | |
const localVue = createLocalVue() | |
storeMocks = configureStoreMocks({localVue}) | |
wrapper = mountComponent({component: Foo, localVue, mountOptions: {store: storeMocks.store}}) | |
}) | |
it('should match with snapshot', () => { | |
expect(wrapper.element).toMatchSnapshot() | |
}) | |
it('should have the expected fakeStoreGetter', () => { | |
expect(wrapper.vm.fakeStoreGetter).toEqual({ | |
name: null | |
}) | |
}) | |
}) |
This file contains hidden or 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
{ | |
// if you need alias to recognize ~/ | |
// add this into the jest configuration | |
{ | |
"moduleNameMapper": { | |
"~(.*)$": "<rootDir>/$1" | |
} | |
}, | |
// if you need recognize dynamic imports | |
// add this into the babel config | |
{ | |
"plugins": [ | |
"transform-runtime", | |
"dynamic-import-node" | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment