Claudiu
Claudiu

Reputation: 4108

Testing setup for Nuxt + Vuex + Vuetify with Jest

I spent one day solving undocumented issues regarding testing setup for Nuxt + Vuex + Vuetify with Jest.

I had issues like:

Upvotes: 3

Views: 3284

Answers (1)

Claudiu
Claudiu

Reputation: 4108

After gathering multiple sources I've ended up with the following setup, which I'm documenting it here for anyone up for saving a day of headache.

Working setup:

// ./jest.config.js

module.exports = {
    // ... other stuff
     setupFilesAfterEnv: ['./test/jest.setup.js']
}
// ./test/jest.setup.js

import Vue from 'vue'
import Vuetify from 'vuetify'
import VueTestUtils from '@vue/test-utils'

Vue.use(Vuetify)

// Mock Nuxt components
VueTestUtils.config.stubs['nuxt'] = '<div />'
VueTestUtils.config.stubs['nuxt-link'] = '<a><slot /></a>'
VueTestUtils.config.stubs['no-ssr'] = '<span><slot /></span>'
// ./test/Header.test.js

import { mount, createLocalVue } from '@vue/test-utils'
import Vuetify from 'vuetify'
import Vuex from 'vuex'

import Header from '~/components/layout/Header'

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

let wrapper

beforeEach(() => {
    let vuetify = new Vuetify()

    wrapper = mount(Header, {
        store: new Vuex.Store({
            state: { products: [] }
        }),
        localVue,
        vuetify
    })
})

afterEach(() => {
    wrapper.destroy()
})

describe('Header', () => {
    test('is fully functional', () => {
        expect(wrapper.element).toMatchSnapshot()
    })
})

Upvotes: 8

Related Questions