恢复错误会导致Jest测试中的VUE警告
我有一个vue应用程序,该应用程序存在于页面组件和一个嵌套的子组件,这是异步后重新要求的。如果请求失败,则会丢弃错误,该错误将传播到顶部组件(page.vue
)。此顶部组件使用errorcaptrud
-lifecycle钩子处理孩子的所有错误。 的示例
这是 stackblitz 机制工作正常 而且我的应用程序没有显示任何警告。但是,我想用开玩笑和vue-test-utils测试我的孩子成分。因此,我进行了一个测试:
import { mount } from "@vue/test-utils";
import Child from "./Child";
jest.mock("./api", () => ({
createSomething: jest.fn(),
}));
it("creation is erroneous", async () => {
const wrapper = mount(Child);
createSomething.mockImplementation(() => {
throw new Error("error");
});
let createAction = wrapper.find("[data-test-create]");
await createAction.trigger("click");
});
但是,因为错误是重新返回的,并且从未被父级捕获,所以我的测试输出中总是有一个控制台日志语句:
console.warn
[Vue warn]: Unhandled error during execution of native event handler
at <Child ref="VTU_COMPONENT" >
at <VTUROOT>
我该如何解决?我尝试将包装等待createAction.trigger(“ click”);
try/catch ,但它不起作用。我需要嘲笑父母的错误限制生命周期吗?
I've got a vue application which exists of a page component and a nested child component, which makes a async post-request. If the request is failing, an error is thrown, which is propagated to the top component (Page.vue
). This top component handles all errors of its child with the errorCaptured
-Lifecycle hook. Here is an working example on stackblitz
The mechanism is working fine and no warning is displayed for my app. However, I want to test my child component isolated with jest and vue-test-utils. Therefore, I made a test:
import { mount } from "@vue/test-utils";
import Child from "./Child";
jest.mock("./api", () => ({
createSomething: jest.fn(),
}));
it("creation is erroneous", async () => {
const wrapper = mount(Child);
createSomething.mockImplementation(() => {
throw new Error("error");
});
let createAction = wrapper.find("[data-test-create]");
await createAction.trigger("click");
});
However, because the error is rethrown and never catched by the parent component, there is always a console log statement in my test output:
console.warn
[Vue warn]: Unhandled error during execution of native event handler
at <Child ref="VTU_COMPONENT" >
at <VTUROOT>
How can i solve this? I tried to wrap await createAction.trigger("click");
with try/catch
but its not working. Do I need to mock the errorCaptured lifecycle of my parent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种解决方案是设置一个no-op
app.config.config.config.errorhandler < /code>
通过
global.global.config
安装选项:演示
One solution is to set a no-op
app.config.errorHandler
via theglobal.config
mounting option:demo