Marcelo Fonseca
Marcelo Fonseca

Reputation: 1844

@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"
},

Upvotes: 6

Views: 2591

Answers (3)

I found two different solutions:

One, register vuetify in test file (of moment i did not found a way to declare globally)

import Vue from 'vue'
import Vuetify from 'vuetify'
Vue.use(Vuetify)

Two, add stubs to wrapper with specific vuetify components

wrapper = shallowMount(ProjetoShow, {stubs: ['v-col']})

Edit, i found the solution for declare globally, is necessary create a file setup.js in tests folder as the doc says and add the path in test configuration file, in case of jest in jest.config.js adding setupFiles: ['./tests/setup.js']

Upvotes: 1

Marcelo Fonseca
Marcelo Fonseca

Reputation: 1844

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')
    })

})

Upvotes: 2

Tanner
Tanner

Reputation: 2421

In Vuetify's documentation on Unit Testing, they declare let vuetify in the describe block, then in beforeEach, assign that variable to a new Vuetify()

I don't see you actually initializing Vuetify anywhere in your test code, so perhaps that's what's needed here.

Upvotes: 1

Related Questions