testing-library / vue-testing-library

🦎 Simple and complete Vue.js testing utilities that encourage good testing practices.
http://testing-library.com/vue
MIT License
1.06k stars 110 forks source link

Vuex store's mutations are lost when passing options.store in Vue2 #286

Open romanlamsal opened 1 year ago

romanlamsal commented 1 year ago

Describe the bug

When passing a store directly into render as option, all mutations are lost on Vue2 components.

// won't work
const store = new Vuex.Store({ mutations: { foo(){} }})
const rendered = render(Component, { store })

// in the component
$store.commit('foo') // -> "[vuex] unknown mutation type: foo"

When inspecting the $store instance in the component, you'll find $store._mutations = {}.

A fix is to pass the store not directly but as mock:

// will work
const store = new Vuex.Store({ mutations: { foo(){} }})
const rendered = render(Component, { mocks: { $store: store } })

// in the component
$store.commit('foo') // -> no warning, works properly

Oddly enough, mapState works in either case - so reading the state works either way.

To Reproduce

See https://github.com/romanlamsal/testing-library-vue2-vuex-example

Expected behavior

Setting a store via options.store should not magically remove all mutations but keep passing on $store.state. It's confusing why this would work as intended if a store is specified in options.mocks.$store.

Screenshots

Related information:

Some more infos:

Relevant code or config (if any)

Additional context