Search code examples
vue.jsmocha.jsvuetify.jsvue-test-utils

@Vue/test-utils shallowMount outputs "[Vue warn]: Unknown custom element" for vuetify components


My unit test in Vue outputs the following warning not just for <v-col> but for every single vuetify component:

[Vue warn]: Unknown custom element: < v-col> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

I created a localVue and added Vuetify, but that doesn't seem to work. This is my test case:

import { shallowMount, createLocalVue } from '@vue/test-utils'
import expect from 'expect'
import ProjetoShow from '../../views/Projeto/ProjetoShow.vue'
import Vuetify from 'vuetify'

describe('ProjetoShow component', () => {
    let wrapper
    let localVue
    beforeEach(() => {
        localVue = createLocalVue()
        localVue.use(Vuetify)
    })

    it('renders correctly', ()=> {
        let vuetify = new Vuetify()
        wrapper = shallowMount(ProjetoShow, {localVue, vuetify})
        expect(wrapper.find('h2').text()).toContain('PROJETO')
    })

})

my packages versions in package.json

"devDependencies": {
    "@vue/test-utils": "^1.0.0-beta.31",
    "axios": "^0.19.0",
    "cross-env": "^5.1",
    "expect": "^24.9.0",
    "jsdom": "^15.1.1",
    "jsdom-global": "^3.0.2",
    "laravel-mix": "^4.0.7",
    "lodash": "^4.17.13",
    "mocha": "^6.2.0",
    "mochapack": "^1.1.5",
    "resolve-url-loader": "^2.3.1",
    "sass": "^1.15.2",
    "sass-loader": "^7.1.0",
    "vue": "^2.5.17",
    "vue-template-compiler": "^2.6.10"
},
"dependencies": {
    "vue-router": "^3.1.3",
    "vuetify": "^2.2.15",
    "vuex": "^3.1.1"
},

Solution

  • I made a mistake by adding vuetify to localVue and not Vue. This changed fixed it. Depending on the version used this can still output some errors. Update vuetify, @vue/test-utils and mocha to latests versions if something goes wrong.

    import { shallowMount, createLocalVue } from '@vue/test-utils'
    import expect from 'expect'
    import ProjetoShow from '../../views/Projeto/ProjetoShow.vue'
    import Vuetify from 'vuetify'
    import VueRouter from 'vue-router'
    import Vue from 'vue'
    Vue.use(Vuetify)
    
    describe('ProjetoShow component', () => {
        let wrapper
        let localVue
        beforeEach(() => {
            localVue = createLocalVue()
            localVue.use(VueRouter)
        })
    
        it('renders correctly', ()=> {
            let router = new VueRouter()
            let vuetify = new Vuetify()
            wrapper = shallowMount(ProjetoShow, {localVue, router, vuetify})
            expect(wrapper.find('h2').text()).toContain('PROJETO')
        })
    
    })