After researching from various sources, I have curated the following setup which I am sharing here to help anyone looking to save themselves from a headache.
Effective Configuration:
// ./jest.config.js
module.exports = {
// ... other configurations
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)
// Mocking 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 operational', () => {
expect(wrapper.element).toMatchSnapshot()
})
})