Search code examples
vue.jsvuejs2vuetify.jsvue-test-utils

Unknown custom element Vue Jest Testing on vuetify plugin 2021 - Vue Cli 2


Configuring setup.js inside jest.config.js doesn't solve the error

setup.js

import Vue from "vue";
import Vuetify from "vuetify";
Vue.config.productionTip = false;
Vue.use(Vuetify);

jest.config.js

module.exports = {
  preset: "@vue/cli-plugin-unit-jest",
  setupFiles: ["./tests/unit/setup.js"],
};

Error Occuring:

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

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

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


Solution

  • Setup testing for Vuetify

    For vuetify, you will need some changes to make sure everything will work fine. First of all, create a setup.js file under the project’s tests folder with the following lines:

    import Vue from 'vue';
    import Vuetify from 'vuetify';
    Vue.use(Vuetify);
    Vue.config.productionTip = false;
    

    After that open package.json file and replace

    From

    "test:unit": "vue-cli-service test:unit"

    To

    "test:unit": "vue-cli-service test:unit --setupTestFrameworkScriptFile=./tests/setup.js"

    Let’s write a simple test

    import { mount } from "@vue/test-utils";
    import Vuetify from "vuetify";
    
    describe("example.vue", () => {
       const vuetify = new Vuetify();
       test("False Test", () => {
           const wrapper = mount(Login, {
               stubs: ["router-link", "router-view"],
               vuetify,
        });
        
         const h1 = wrapper.find("h1");
         expect(h1.html()).toBe("<div><p>Foo</p></div>");
      });
    
      test("True Test", () => {
        const wrapper = mount(Login, {
          stubs: ["router-link", "router-view"],
          vuetify,
        });
        const h1 = wrapper.find("h1");
        expect(h1.html()).toBe("<h1>Login</h1>");
      });