Nuxt + Vuex + Vuetify with Jest的测试设置

问题描述 投票:0回答:1

我花了一天的时间解决有关Nuxt + Vuex + Vuetify with Jest的测试设置的未记录的问题。

我有类似的问题:

  • Unknown custom element: <nuxt-link> - When running jest unit tests

  • Cannot read property 'register' of undefined

  • [Vuetify] Multiple instances of Vue detected

javascript vue.js jestjs vuetify.js nuxt.js
1个回答
0
投票

收集了多个资源后,我得到了以下设置,在此将其记录在这里,以供任何节省时间的人使用。

工作设置:

// ./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-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()
    })
})
© www.soinside.com 2019 - 2024. All rights reserved.