VUE单元测试:未找到注射变量

发布于 2025-01-30 15:29:17 字数 1642 浏览 0 评论 0原文

我有一个带有单元测试文件的vue文件。当我运行测试时:utils我会收到以下警告。在测试文件中安装组件时,使用提供的正确方法是什么?我试图以与VUE测试文档中所示的方式相同,但仍然不正确。

export default {
  name: "Navbar",
  inject: ["emitter"],
  props: {
  ishome: {
      default: true,
      type: Boolean,
    },
  },
  data() {
    return {
      isFeedback: false,
    };
  },

  mounted() {
    this.emitter.on("openFeedback", () => {
      this.isFeedback = true;
    });
    this.emitter.on("closeFeedback", () => {
      this.isFeedback = false;
    });
  },
}
import { mount } from "@vue/test-utils";
import Navbar from "../components/Navbar.vue";
import mitt from "mitt";

describe("Mounted App isHome true", () => {
  try {
    const emit = mitt();
    const wrapper = mount(Navbar, {
      provide: {
        emitter() {
          return emit;
        },
      },
      props: {
        isHome: true,
      },
    });
  } catch (error) {
    expect(error).toBe("");
  }
console.warn node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:6533
      [Vue warn]: injection "emitter" not found. 
        at <Navbar isHome=true ref="VTU_COMPONENT" > 
        at <VTUROOT>
    console.warn node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:6533
      [Vue warn]: Unhandled error during execution of mounted hook 
        at <Navbar isHome=true ref="VTU_COMPONENT" > 
        at <VTUROOT>
    console.warn node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:6533
      [Vue warn]: injection "emitter" not found. 
        at <Navbar isHome=false ref="VTU_COMPONENT" > 
        at <VTUROOT>

I have a vue file having inject which also has a unit test file. When I run the test: utils I'm getting the following warning. What is the proper way of using provide while mounting the component in test file? I tried to do it the same way as shown in vue test docuentation but still something is not right.

export default {
  name: "Navbar",
  inject: ["emitter"],
  props: {
  ishome: {
      default: true,
      type: Boolean,
    },
  },
  data() {
    return {
      isFeedback: false,
    };
  },

  mounted() {
    this.emitter.on("openFeedback", () => {
      this.isFeedback = true;
    });
    this.emitter.on("closeFeedback", () => {
      this.isFeedback = false;
    });
  },
}
import { mount } from "@vue/test-utils";
import Navbar from "../components/Navbar.vue";
import mitt from "mitt";

describe("Mounted App isHome true", () => {
  try {
    const emit = mitt();
    const wrapper = mount(Navbar, {
      provide: {
        emitter() {
          return emit;
        },
      },
      props: {
        isHome: true,
      },
    });
  } catch (error) {
    expect(error).toBe("");
  }
console.warn node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:6533
      [Vue warn]: injection "emitter" not found. 
        at <Navbar isHome=true ref="VTU_COMPONENT" > 
        at <VTUROOT>
    console.warn node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:6533
      [Vue warn]: Unhandled error during execution of mounted hook 
        at <Navbar isHome=true ref="VTU_COMPONENT" > 
        at <VTUROOT>
    console.warn node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:6533
      [Vue warn]: injection "emitter" not found. 
        at <Navbar isHome=false ref="VTU_COMPONENT" > 
        at <VTUROOT>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染墨丶若流云 2025-02-06 15:29:17

使用VUE 3选项API,似乎您需要在全球上指定提供选项。不过,我在文档中找不到
此后,您似乎需要提供服务作为属性而不是方法。在任何在线文档中都找不到这一点...

const wrapper = mount(Navbar, {
      global: {
        provide: {
          "emitter": emit;
        },
        props: {
          isHome: true,
        },
      });

With Vue 3 options API it seems that you need to specify the provide option globally. Though, i cannot find that in documentation
Thereafter it seems that you need to provide the service as an attribute, rather than a method. Cannot find that either in any on-line documentation...

const wrapper = mount(Navbar, {
      global: {
        provide: {
          "emitter": emit;
        },
        props: {
          isHome: true,
        },
      });

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文