Vue.js vuex的模拟存储操作未模拟

Vue.js vuex的模拟存储操作未模拟,vue.js,jestjs,vuex,vue-test-utils,Vue.js,Jestjs,Vuex,Vue Test Utils,我有一个小的vue组件,它在钩子上创建了一些动作 @Component export default class SomeComponent extends Vue { created() { store.dispatch('module/myAction', { root: true }); } } 我写了下一个测试 const localVue = createLocalVue(); localVue.use(Vuex); localVue.use(VueRoute

我有一个小的vue组件,它在钩子上创建了一些动作

@Component
export default class SomeComponent extends Vue {
  created() {
    store.dispatch('module/myAction', { root: true });
  }
}
我写了下一个测试

    const localVue = createLocalVue();
localVue.use(Vuex);
localVue.use(VueRouter);
const localRouter = new VueRouter();
describe('SomeComponent.vue Test', () => {
  let store: any;

  beforeEach(() => {
    store = new Vuex.Store({
      modules: {
        module: {
          namespaced: true,
          actions: {
            myAction: jest.fn()
          }
        }
      }
    });
  });
  it('is component created', () => {
    const wrapper = shallowMount(SomeComponent, {
      localVue,
      store,
      propsData: {}
    });

    expect(wrapper.isVueInstance()).toBeTruthy();
  });
});
但是由于某种原因,执行了“真实”代码,我得到了一个警告,isVueInstance()已被弃用。在测试中,您应该模拟$store对象及其分派函数。我修复了created()中的打字错误,这是我的SomeComponent版本和工作测试,希望能有所帮助


@Component
export default class SomeComponent extends Vue {
  created () {
    this.$store.dispatch('module/myAction', { root: true })
  }
}

import { shallowMount, Wrapper } from '@vue/test-utils'
import SomeComponent from '@/components/SomeComponent/SomeComponent.vue'

let wrapper: Wrapper<SomeComponent & { [key: string]: any }>

describe('SomeComponent.vue Test', () => {
  beforeEach(() => {
    wrapper = shallowMount(SomeComponent, {
      mocks: {
        $store: {
          dispatch: jest.fn()
        }
      }
    })
  })
  it('is component created', () => {
    expect(wrapper.vm.$store.dispatch).toBeCalled()
    expect(wrapper.vm.$store.dispatch).toBeCalledWith('module/myAction', { root: true })
  })
})

@组成部分
导出默认类SomeComponent扩展Vue{
创建(){
这个.$store.dispatch('module/myAction',{root:true})
}
}
从'@vue/test-utils'导入{shallowMount,Wrapper}
从“@/components/SomeComponent/SomeComponent.vue”导入SomeComponent
让包装器:包装器
描述('SomeComponent.vue测试',()=>{
在每个之前(()=>{
包装=浅装(SomeComponent{
模拟:{
$store:{
快讯:jest.fn()
}
}
})
})
它('是组件创建的',()=>{
expect(wrapper.vm.$store.dispatch).toBeCalled()
expect(wrapper.vm.$store.dispatch).toBeCalledWith('module/myAction',{root:true})
})
})
还请记住,当您测试某个组件或任何其他组件时,您不应该测试存储功能,而应该测试某些操作/突变是否使用某些参数调用。商店应单独进行测试。因此,在测试组件时不需要创建真正的Vuex存储,只需模拟$Store对象即可