6

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

3 Answers 3

2

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

})
Sign up to request clarification or add additional context in comments.

Comments

1

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.

4 Comments

I tried the solution in vuetify docs but it still doesnt work
Would you be able to create a public git repo with your project as it is right now - or rather, just the relevant files?
It did work now. I made a mistake by adding Vuetify to localVue and not Vue instance. the Vuetify docs is correct. I Changed localVue.use(Vuetify) to import Vue from 'vue' ; Vue.use(Vuetify);
@MarceloFonseca Glad you figured it out. Please post that information as an answer and mark it as accepted so anyone else who comes across this question can get the answer quickly. Happy coding!
1

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']

3 Comments

I've been trying to pass it to my setup file without success. it gives me the error UnhandledPromiseRejectionWarning and SyntaxError: Unexpected identifier for import Vuetify from 'vuetify'. No idea why
Try import Vuetify from 'vuetify/lib' so i have it in my plugin file but don't worked for me in setup.js, declare it as works in your test file it should work..
Adding this line to the jest.config.js fixed it for me: setupFiles: ['./tests/setup.js']. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.