vue-test-utils-compat icon indicating copy to clipboard operation
vue-test-utils-compat copied to clipboard

Adding a router to localVue results in an error when using shallowMount

Open CyberAP opened this issue 4 years ago • 1 comments

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:

  1. Create an instance of a Vue 2 router
  2. Create localVue using createLocalVue from test-utils
  3. 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.

CyberAP avatar Nov 24 '21 19:11 CyberAP

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.

snoozbuster avatar May 27 '22 22:05 snoozbuster