0

I have few unit tests for a Vue component. In most of the tests, the shallowMounted component just need computed properties. But for two test I need a store(Vuex) instance. Is there a way to add instances to the already shallowMounted component? May be code below will help to understand. Its just an example. Thank you.

describe(('Vue component test') => {
  beforeEach(() => {
    wrapper = shallowMount(Component, {
      computed: {
        errors: () => null,
        isLoading: () => false,
      }
    })
  });

  describe('Test 1', => {
    it('is a vue instance', () => {
      expect(wrapper...);
    });
  });

  describe('Test 2' => {
    it('is a vue component', () => {
      expect(wrapper...);
    });
  })

  describe('Test with store instance', {
    // Add store(Vuex) instance to the `wrapper` defined inside beforeEach() above
    // Then use the wrapper
    expect(wrapper...);
  });
});
sven
  • 303
  • 1
  • 2
  • 8

1 Answers1

0

what do you mean by store instance? A instance of Vuex.Store ?

If so you could try to do:

import Vuex from 'vuex';

beforeEach(() => {

   const store = new Vuex.Store({
       // mock your store data here
   });

   wrapper = shallowMount(Component, { 
      store,
      // your other properties, e.g. computed 
   });
});
  • As described above, I just want to use the store instance in the last test whilst using the shallowMounted `wapper` already defined above in the code example. – sven Jul 13 '18 at 10:59
  • But its not clear to me what you mean by store instance – Thomas Rutzer Jul 13 '18 at 11:04
  • well i have change the question a little. I meant Vuex instance. thanks – sven Jul 13 '18 at 11:04
  • Updated my suggest as well. Its no problem to have your Store instance injected everytime your test mounts the component (because you said you'll only need it for the last test... – Thomas Rutzer Jul 13 '18 at 13:59
  • Well thats one way to do it which I already know. I use the same wrapper in multiple tests but I dont need the Vuex instance in all the tests so I was trying to find out if I could attach the Vuex instance to the `wrapper` only on the tests that needs it, not on all of them. Anyways thank you for your answer. :) – sven Jul 13 '18 at 14:07