How to test a composable that uses a vuex store inside it? #2004
-
I have some composable that uses a store. I would like to test the code with jest and mock the store with some predefined values. As we don't mount a composable I'm not sure how to proceed. I was researching and just found some answers to Pinia but not for Vuex (like setActivePinia). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
We have a dedicated guide for testing Vuex but it's more focused on testing components that use a store https://test-utils.vuejs.org/guide/advanced/vuex.html You're indeed going to find more answers around Pinia, as it is the recommended solution over Vuex in Vue 3, but the principles should be the same Testing a composable can be done by creating a test component that uses it, and then mounting and testing this component. |
Beta Was this translation helpful? Give feedback.
Hi @gabriellatavares
We have a dedicated guide for testing Vuex but it's more focused on testing components that use a store https://test-utils.vuejs.org/guide/advanced/vuex.html
You're indeed going to find more answers around Pinia, as it is the recommended solution over Vuex in Vue 3, but the principles should be the same
Testing a composable can be done by creating a test component that uses it, and then mounting and testing this component.
When you mount the component, you can then mock the store as explained in the guide I linked above.