ktsn / vuex-class

Binding helpers for Vuex and vue-class-component
MIT License
1.73k stars 86 forks source link

How to write unit test for a VUE typescript component #60

Closed re2005 closed 4 years ago

re2005 commented 4 years ago

I've posted the question on StackOverflow:

https://stackoverflow.com/questions/59245479/how-to-unit-test-a-typescript-vue-component-that-uses-vuex-class

Any help ?

re2005 commented 4 years ago

Seems pretty obvious like this:

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

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

const getters = {
    user: jest.fn()
};

const store = new Vuex.Store({
    modules: {
        user: {
            namespaced: true,
            getters
        }
    }
});

describe('Header.vue', () => {
    it('Instantiate component', () => {
        const wrapper = shallowMount(Header, {
            store,
            localVue
        });
        expect(wrapper.isVueInstance()).toBeTruthy();
    });
});