vue-test-utils-compat
vue-test-utils-compat copied to clipboard
Adding a router to localVue results in an error when using shallowMount
If you try to use a router with a localVue when calling shallowMount you will get the following error:
TypeError: Cannot redefine property: $router
at Function.defineProperty (<anonymous>)
89 | const { localVue, store, router } = setupLocalVue(modules);
90 |
> 91 | return shallowMount(component, {
| ^
92 | store,
93 | router,
94 | localVue,
at Function.install (node_modules/vue-router/dist/vue-router.common.js:1312:10)
at Object.use (node_modules/@vue/compat/dist/vue.cjs.js:5908:28)
at mount (node_modules/@vue/test-utils/dist/vue-test-utils.cjs.js:7860:25)
at call (node_modules/@vue/test-utils/dist/vue-test-utils.cjs.js:7934:12)
at patchedMount (node_modules/vue-test-utils-compat/src/index.js:123:35)
...
Steps to reproduce:
- Create an instance of a Vue 2 router
- Create localVue using
createLocalVuefrom test-utils - Pass that instance to localVue instance as the following:
localVue.use(VueRouter)
const routerInstance = new Router(routes)
const localVue = createLocalVue()
shallowMount({
localVue,
router,
})
This is code is valid in Vue 2, but fails in compat mode.
Similarly, passing store to mount/shallowMount doesn't work either. I think these options only worked prior because of VTU1's option merging behavior, but they're common enough patterns (and have easy shims, since they just need to be rewritten into plugins) that I think this package should handle them.