[Vue warn]: Unknown custom element: <nuxt-link> - When running jest unit tests

15,375

Solution 1

Although the answers provided could work. What I ended up using was based on this guide

const wrapper = mount(TestItem, {
  propsData: { item },
  localVue,
  store,
  stubs: {
    NuxtLink: true,
    // Any other component that you want stubbed
  },
});

Solution 2

This is how I was able to get rid of the annoying warning:

Include RouterLinkStub, eg.:

import { shallowMount, createLocalVue, RouterLinkStub } from '@vue/test-utils';

Map NuxtLink stub to RouterLinkStub

const wrapper = shallowMount(TestItem, {
  ...
  stubs: {
    NuxtLink: RouterLinkStub
  }
})

And in case you were checking nuxt-link text or something, change:

const link = wrapper.find('nuxt-link');

to

const link = wrapper.find(RouterLinkStub);

Found this gold on https://onigra.github.io/blog/2018/03/19/vue-test-utils-router-link-stub/

Good thing you don't need to know japanese to read code...

Solution 3

I added below lines of code to get this working.

  1. In your test file
import NuxtLink from "path to nuxt-link.js"

Mycomponent.components.NuxtLink = NuxtLink
  1. In your jest conf file
transformIgnorePatterns: [
   "path to nuxt-link.js"
],

Or you could add below line in mount options

mount(Mycomponent, {stubs: ["nuxt-link"]})

Solution 4

I managed to get it working using this workaround for Storybook:

import { mount, createLocalVue } from '@vue/test-utils'
import Component from '@/components/Component.vue'

const localVue = createLocalVue()

localVue.component('nuxt-link', {
  props:   ['to'],
  template: '<a href="#"><slot>NuxtLink</slot></a>',
})

describe('Test Component', () => {

    const wrapper = mount(Component, {
      stubs: ['nuxt-link'],
      localVue
    })
})

Solution 5

...
import NuxtLink from '../.nuxt/components/nuxt-link.js'

...
TestItem.components = TestItem.components || {};
TestItem.components.NuxtLink = NuxtLink;
const wrapper = shallow(TestItem, {
    ...
});
...
Share:
15,375
Anima-t3d
Author by

Anima-t3d

Creative Samurai who uses code to write poetry.

Updated on June 04, 2022

Comments

  • Anima-t3d
    Anima-t3d almost 2 years

    Problem

    I'm using nuxt 1.4 with routing using Jest to do unit testing. My application doesn't throw errors and seems to work perfectly. However when running my unit test npm run unit (which runs jest) it throws an error in the terminal: [Vue warn]: Unknown custom element: <nuxt-link> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

    Expected

    I would expect it to not throw this error since my application is working.

    Files

    package.json:

    {
      "name": "vue-starter",
      "version": "1.0.0",
      "description": "Nuxt.js project",
      "private": true,
      "scripts": {
        "dev": "nuxt",
        "build": "nuxt build",
        "start": "nuxt start",
        "generate": "nuxt generate",
        "lint": "eslint --ext .js,.vue --ignore-path .gitignore .",
        "precommit": "npm run lint",
        "test": "npm run lint && npm run unit",
        "unit": "jest",
        "unit:report": "jest --coverage"
      },
      "dependencies": {
        "babel-jest": "^22.4.1",
        "jest-serializer-vue": "^1.0.0",
        "node-sass": "^4.7.2",
        "npm": "^5.7.1",
        "nuxt": "^1.0.0",
        "sass-loader": "^6.0.7",
        "vue-jest": "^2.1.1"
      },
      "devDependencies": {
        "@vue/test-utils": "^1.0.0-beta.12",
        "babel-eslint": "^8.2.1",
        "eslint": "^4.15.0",
        "eslint-friendly-formatter": "^3.0.0",
        "eslint-loader": "^1.7.1",
        "eslint-plugin-vue": "^4.0.0",
        "jest": "^22.4.2"
      },
      "browserslist": [
        "> 1%",
        "last 2 versions",
        "not ie <= 8"
      ],
      "jest": {
        "moduleFileExtensions": [
          "js",
          "vue"
        ],
        "transform": {
          "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
          ".*\\.(vue)$": "<rootDir>/node_modules/vue-jest"
        },
        "snapshotSerializers": [
          "<rootDir>/node_modules/jest-serializer-vue"
        ]
      }
    }
    

    The component that I test:

    <template>
      <div>
        <nuxt-link class="name" :to="{ path: `entity/${item.id}`, params: { id: item.id }}">{{item.name}}</nuxt-link>
        <button class="connect" @click="connect">{{ btnText }}</button>
      </div>
    </template>
    
    <script>
      // import nuxtLink from '../.nuxt/components/nuxt-link';
    
      const connectionStatusMap = [
        'Connect',
        'Connected',
        'Pending',
        'Cancel',
      ];
    
      export default {
        /*components: {
          'nuxt-link': nuxtLink,
        },*/
        props: {
          item: {
            type: Object
          }
        },
        ...
      }
    </script>
    

    My test script:

    import TestItem from '../components/TestItem';
    import { shallow, mount, createLocalVue } from '@vue/test-utils';
    import Vuex from 'vuex';
    import VueRouter from 'vue-router';
    
    const localVue = createLocalVue()
    
    localVue.use(Vuex)
    localVue.use(VueRouter)
    
    ...
    it(`should show the entity`, () => {
        const wrapper = mount(TestItem, {
          propsData: { item },
          localVue,
          store,
          // stubs: ['nuxt-link'],
        })
        expect(wrapper.find('.name').text()).toBe(item.name);
      });
    
      it(`should show allow me to connect if I'm not yet connected`, () => {
        const wrapper = shallow(TestItem, {
          propsData: { item },
          localVue,
          store,
          stubs: ['nuxt-link'],
        })
        expect(wrapper.find('.connect').text()).toBe('Connect');
      });
      ...
    

    I tried

    I tried creating a localVue and also stubbing the component as suggested in this github comment I also tried shallow/mount but that did not seem to work either.

  • Anima-t3d
    Anima-t3d about 6 years
    Thanks for your reply. I now get this error console.error node_modules/vue/dist/vue.runtime.common.js:589 [Vue warn]: Unknown custom element: <router-link> - did you register the component correctly? For recursive components, make sure to provide the "name" option. (found in <Anonymous>) But I don't have any router-link element in my app, only nuxt-link
  • Oldenborg
    Oldenborg almost 6 years
    @Anima-t3d did you find a solution to the "router-link" error?. I have the exact same issue.
  • Anima-t3d
    Anima-t3d almost 6 years
    @Oldenborg I did not yet have the time to try out the solutions. But glad you found yours.
  • MacroMan
    MacroMan almost 2 years
    This has been renamed shallowMount now