championswimmer / vuex-module-decorators

TypeScript/ES7 Decorators to create Vuex modules declaratively
https://championswimmer.in/vuex-module-decorators/
MIT License
1.8k stars 170 forks source link

Unit Testing a decorated vuex module #256

Open Jamiewarb opened 4 years ago

Jamiewarb commented 4 years ago

Hi there,

Really struggling for direction on how to Unit Test a store using Jest (or any test runner).

I can't seem to find any examples of this. I can see quite a lot of issus relating to difficulty testing, but can't seem to find any for testing the store - they seem to be about testing components that use the store.

I'm looking to unit test the store actions. I wondered if anyone has had any luck with this so far and can point to some code that may shine a light on it?

jkolivuo commented 4 years ago

What I have done with unit tests by using Jest is following. I have rewrite the existing class to test file (may not be necessary, but in my case this is because of mocking axios calls.). Then creating a mock store like this

const store = new Vuex.Store({ modules: { yourModule: YourModule } }); And now I can use that within my tests cases like this. const { state: { yourModule } } = store; store.dispatch('yourAction') Then you can just check that action has worked as expected. Hopefully this helps a bit

Jamiewarb commented 4 years ago

Ah interesting, thanks. And 'YourModule' is just the store/module.ts file, that's written using vuex-module-decorators syntax?

jkolivuo commented 4 years ago

Yep