[Vuejs]-Mock of store action of vuex does not mocked

0👍

isVueInstance() is deprecated. In your test you should mock $store object and it’s dispatch function. I fixed typo in created(), here’s my version of SomeComponent and working test, hope that would help.


@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 })
  })
})

Also keep in mind that when you test SomeComponent or any other component you should not test store functionality, you should just test, that certain actions/mutations were called with certain arguments. The store should be tested separately. Therefore you don’t need to create real Vuex Store when you test components, you just need to mock $store object.

Leave a comment