0

我对 Vue 组件的单元测试很少。在大多数测试中, shallowMounted 组件只需要计算属性。但是对于两个测试,我需要一个 store(Vuex) 实例。有没有办法将实例添加到已经很浅的组件中?可能是下面的代码将有助于理解。它只是一个例子。谢谢你。

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...);
  });
});
4

1 回答 1

0

商店实例是什么意思?的一个实例Vuex.Store

如果是这样,您可以尝试这样做:

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 
   });
});
于 2018-07-13T10:53:36.403 回答