appErrorBoundary.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import { AppErrorBoundary, ScopedErrorBoundary } from "@/ErrorBoundary.js";
  5. import type { ArmsCustomEventPayload } from "@zcode/shared";
  6. import {
  7. clearReactErrorArmsReporterForTest,
  8. setReactErrorArmsReporter,
  9. } from "@/lib/reactErrorArmsTelemetry.js";
  10. function makeReporter() {
  11. const calls: ArmsCustomEventPayload[] = [];
  12. return {
  13. calls,
  14. reportArmsCustomEvent: vi.fn(async (payload: ArmsCustomEventPayload) => {
  15. calls.push(payload);
  16. }),
  17. };
  18. }
  19. afterEach(() => {
  20. clearReactErrorArmsReporterForTest();
  21. vi.restoreAllMocks();
  22. });
  23. describe("AppErrorBoundary", () => {
  24. it("出错后会渲染 fallback,而不是返回空白内容", () => {
  25. const boundary = new AppErrorBoundary({
  26. children: createElement("div", null, "ok"),
  27. });
  28. boundary.state = {
  29. error: new Error("boom"),
  30. componentStack: "\n at BrokenPanel",
  31. };
  32. const html = renderToStaticMarkup(boundary.render());
  33. expect(html).toMatch(/应用界面出了点问题|The app ran into a problem/);
  34. expect(html).toMatch(/重试|Try again/);
  35. expect(html).toMatch(/刷新应用|Reload app/);
  36. expect(html).toContain("boom");
  37. expect(html).toContain("BrokenPanel");
  38. });
  39. it("会把非 Error 抛出值包装成 Error 对象", () => {
  40. const state = AppErrorBoundary.getDerivedStateFromError("plain text failure");
  41. expect(state.error?.message).toBe("plain text failure");
  42. expect(state.componentStack).toBe("");
  43. });
  44. it("局部边界出错后只渲染区域 fallback", () => {
  45. const boundary = new ScopedErrorBoundary({
  46. children: createElement("div", null, "ok"),
  47. scope: "workspace-chat",
  48. resetKeys: ["workspace-a", "task-a"],
  49. });
  50. boundary.state = {
  51. error: new Error("chat exploded"),
  52. componentStack: "\n at ChatView",
  53. };
  54. const html = renderToStaticMarkup(boundary.render());
  55. expect(html).toMatch(/这块界面出了点问题|This section ran into a problem/);
  56. expect(html).toMatch(/重试此区域|Try this section again/);
  57. expect(html).toContain("chat exploded");
  58. expect(html).toContain("ChatView");
  59. });
  60. it("静默局部边界可以隐藏非关键浮层 fallback", () => {
  61. const boundary = new ScopedErrorBoundary({
  62. children: createElement("div", null, "ok"),
  63. scope: "desktop-top-overlay",
  64. variant: "silent",
  65. });
  66. boundary.state = {
  67. error: new Error("overlay exploded"),
  68. componentStack: "\n at DesktopTopOverlay",
  69. };
  70. expect(renderToStaticMarkup(boundary.render())).toBe("");
  71. });
  72. it("根级边界 componentDidCatch 会把错误上报到 ARMS(boundary_scope=app)", () => {
  73. const reporter = makeReporter();
  74. setReactErrorArmsReporter(reporter);
  75. const boundary = new AppErrorBoundary({
  76. children: createElement("div", null, "ok"),
  77. });
  78. // jsdom 下 setState 在脱离 React 树时会走 noop updater,这里只验证上报副作用。
  79. boundary.setState = vi.fn();
  80. boundary.componentDidCatch(new Error("root boom"), {
  81. componentStack: "\n at Root",
  82. });
  83. expect(reporter.calls).toHaveLength(1);
  84. expect(reporter.calls[0]!.name).toBe("perf_react_error");
  85. expect(reporter.calls[0]!.properties?.error_message).toBe("root boom");
  86. expect(reporter.calls[0]!.properties?.boundary_scope).toBe("app");
  87. });
  88. it("局部边界 componentDidCatch 会带 scope 上报到 ARMS", () => {
  89. const reporter = makeReporter();
  90. setReactErrorArmsReporter(reporter);
  91. const boundary = new ScopedErrorBoundary({
  92. children: createElement("div", null, "ok"),
  93. scope: "workspace-chat",
  94. });
  95. boundary.setState = vi.fn();
  96. boundary.componentDidCatch(new Error("chat boom"), {
  97. componentStack: "\n at ChatView",
  98. });
  99. expect(reporter.calls).toHaveLength(1);
  100. expect(reporter.calls[0]!.properties?.boundary_scope).toBe("workspace-chat");
  101. expect(reporter.calls[0]!.properties?.component_stack).toContain("ChatView");
  102. });
  103. });