[Vuejs]-Unit testing in Vue2 with Jest

0👍

dispatch is usually related to Vuex store actions. You need to add Vuex and your store to the mounted component. You can use createLocalVue if you’re using Vue 2.x to install Vuex on it.

import { shallowMount, createLocalVue } from '@vue/test-utils'
import Vuex from 'vuex'

const localVue = createLocalVue()
localVue.use(Vuex)

describe('Login', () => {
    const store = new Vuex.Store({
        actions: {
            // any actions or getters you access in Login.vue
        },
    })

    it("renders with message", () => {
        const wrapper = shallowMount(Login, {
            localVue,
            store,
            propsData: { msg: 'Hello' }
        })
        
        // ...
    })
})

Leave a comment