首页 > 解决方案 > 如何修复此错误 [Vue 警告]:未知的自定义元素:在用 Jest 进行单元测试

问题描述

我在运行 npm 运行测试时遇到问题。错误是

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

边栏CMS.spect.js

import { shallowMount } from "@vue/test-utils";
import SidebarCMS from "../layouts/SidebarCMS";

const factory = () => {
  return shallowMount(SidebarCMS, {});
};

describe("SidebarCMS", () => {

  test("renders properly", () => {
    const wrapper = factory();
    expect(wrapper.html()).toMatchSnapshot();
  });
});

谁能帮我?

标签: vue.jsnuxt.js

解决方案


您可以stub在创建实例时创建子组件。有关存根组件的更多信息,请查看此链接

试试这样,这将解决您的警告!

const factory = () => {
  return shallowMount(SidebarCMS, {
     stubs: {
      'nuxt-link': true,
      'any-other-child': true
     }
   });
};

推荐阅读