恢复错误会导致Jest测试中的VUE警告

发布于 2025-01-22 15:33:49 字数 1198 浏览 0 评论 0原文

我有一个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 技术交流群。

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

发布评论

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

评论(1

无人问我粥可暖 2025-01-29 15:33:49

一种解决方案是设置一个no-op app.config.config.config.errorhandler < /code>通过 global.global.config 安装选项:

const wrapper = mount(Child, {
  global: {
    config: {
      errorHandler(err) { /* ignore */ },
    },
  },
})

演示

One solution is to set a no-op app.config.errorHandler via the global.config mounting option:

const wrapper = mount(Child, {
  global: {
    config: {
      errorHandler(err) { /* ignore */ },
    },
  },
})

demo

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