Skip to content

Instantly share code, notes, and snippets.

@oladimillion
Last active October 12, 2020 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oladimillion/4c9e2a406051b011813ee45816ee6aaf to your computer and use it in GitHub Desktop.
Save oladimillion/4c9e2a406051b011813ee45816ee6aaf to your computer and use it in GitHub Desktop.
Fix for Vuex mocking not working
https://github.com/vuejs/vue-test-utils/issues/116#issuecomment-521366881
```
-- MyComponent.spec.js --
import { shallowMount } from '@vue/test-utils'
import store from '@/store'
import router from '@/router'
import MyComponent from '@/components/MyComponent'
describe('MyComponent', () => {
const wrapper = shallowMount(MyComponent, {
router,
store,
})
it('render without crashing', () => {
...
})
})
```
```
-- store.js --
import Vue from 'vue'
import Vuex from 'vuex'
const test = {
namespaced: true,
state: { ... },
getters: { ... },
actions: { ... },
mutations: { ... },
}
Vue.use(Vuex)
const debug = process.env.NODE_ENV !== 'production'
export default new Vuex.Store({
modules: {
test,
},
strict: debug,
});
```
```
-- router.js --
import Vue from 'vue'
import VueRouter from 'vue-router'
import MyComponent from '@/components/MyComponent'
const routes = [
{ path: '/', name: 'My Component', component: MyComponent },
]
Vue.use(VueRouter)
export default new VueRouter({
mode: 'history',
routes,
})
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment