VUE单元测试:未找到注射变量
我有一个带有单元测试文件的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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用VUE 3选项API,似乎您需要在全球上指定提供选项。不过,我在文档中找不到
此后,您似乎需要提供服务作为属性而不是方法。在任何在线文档中都找不到这一点...
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...