e2eElectronServiceLifecycle.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { readFileSync } from "node:fs";
  2. import { describe, expect, it, vi } from "vitest";
  3. import {
  4. initializeE2EElectronServiceBridge,
  5. registerE2EElectronServiceBridgeReload,
  6. reloadE2EElectronServiceBridge,
  7. } from "./e2e/helpers/e2e-electron-service-lifecycle.js";
  8. describe("desktop e2e Electron service lifecycle", () => {
  9. it("does not reconnect when the first bridge probe succeeds", async () => {
  10. const events: string[] = [];
  11. await initializeE2EElectronServiceBridge({
  12. initialize: async () => {
  13. events.push("initialize");
  14. },
  15. probe: async () => {
  16. events.push("probe");
  17. },
  18. reset: () => {
  19. events.push("reset");
  20. },
  21. });
  22. expect(events).toEqual(["initialize", "probe"]);
  23. });
  24. it("rebuilds the bridge once when the first probe fails", async () => {
  25. const events: string[] = [];
  26. const probe = vi
  27. .fn<() => Promise<void>>()
  28. .mockRejectedValueOnce(new Error("ContextId missing"))
  29. .mockResolvedValueOnce();
  30. await initializeE2EElectronServiceBridge({
  31. initialize: async () => {
  32. events.push("initialize");
  33. },
  34. probe: async () => {
  35. events.push("probe");
  36. await probe();
  37. },
  38. reset: () => {
  39. events.push("reset");
  40. },
  41. });
  42. expect(events).toEqual(["initialize", "probe", "reset", "initialize", "probe"]);
  43. });
  44. it("surfaces the second bridge failure instead of looping", async () => {
  45. const initialize = vi.fn<() => Promise<void>>().mockResolvedValue();
  46. const reset = vi.fn<() => void>();
  47. await expect(
  48. initializeE2EElectronServiceBridge({
  49. initialize,
  50. probe: async () => {
  51. throw new Error("bridge unavailable");
  52. },
  53. reset,
  54. }),
  55. ).rejects.toThrow("两次初始化后仍不可用");
  56. expect(initialize).toHaveBeenCalledTimes(2);
  57. expect(reset).toHaveBeenCalledTimes(1);
  58. });
  59. it("wraps the existing service package with the local reload-aware service module", () => {
  60. const packageJson = JSON.parse(
  61. readFileSync(new URL("../package.json", import.meta.url), "utf8"),
  62. ) as { devDependencies?: Record<string, string> };
  63. const serviceSource = readFileSync(
  64. new URL("./e2e/helpers/e2e-electron-service.ts", import.meta.url),
  65. "utf8",
  66. );
  67. const wdioSource = readFileSync(new URL("../wdio.conf.ts", import.meta.url), "utf8");
  68. expect(packageJson.devDependencies?.["wdio-electron-service"]).toBe("^9.2.1");
  69. expect(packageJson.devDependencies?.["@wdio/electron-types"]).toBe("^9.2.1");
  70. expect(serviceSource).toContain(
  71. "export const launcher: typeof ElectronLauncherService = ElectronLauncherService",
  72. );
  73. expect(serviceSource).toContain("async onReload()");
  74. expect(serviceSource).toContain("registerE2EElectronServiceBridgeReload(");
  75. expect(wdioSource).toContain("E2E_ELECTRON_SERVICE_MODULE_PATH");
  76. });
  77. it("cancels the ContextId timeout after the default execution context is found", () => {
  78. const rootPackageJson = JSON.parse(
  79. readFileSync(new URL("../../../package.json", import.meta.url), "utf8"),
  80. ) as {
  81. pnpm?: { patchedDependencies?: Record<string, string> };
  82. };
  83. const electronServiceSource = readFileSync(
  84. new URL(
  85. "../../../node_modules/wdio-electron-service/dist/esm/index.js",
  86. import.meta.url,
  87. ),
  88. "utf8",
  89. );
  90. const contextIdHandler = electronServiceSource.slice(
  91. electronServiceSource.indexOf(
  92. "_ElectronCdpBridge_getContextIdHandler = function",
  93. ),
  94. electronServiceSource.indexOf("function getInitializeScript()"),
  95. );
  96. // Bug 根因:上游 9.2.1 的 ContextId Promise 成功 resolve 后没有取消 timeout,
  97. // 定时器仍会在健康 case 运行期间打印误导性的 Timeout exceeded error。
  98. expect(
  99. rootPackageJson.pnpm?.patchedDependencies?.[
  100. "wdio-electron-service@9.2.1"
  101. ],
  102. ).toBe("patches/wdio-electron-service@9.2.1.patch");
  103. expect(contextIdHandler).toContain("clearTimeout(timeout)");
  104. expect(contextIdHandler).toContain(
  105. "this.off('Runtime.executionContextCreated', onExecutionContextCreated)",
  106. );
  107. });
  108. it("routes a browser reload through the bridge callback registered by the service", async () => {
  109. const browser = {} as WebdriverIO.Browser;
  110. const reload = vi.fn(async () => undefined);
  111. registerE2EElectronServiceBridgeReload(browser, reload);
  112. await reloadE2EElectronServiceBridge(browser);
  113. expect(reload).toHaveBeenCalledOnce();
  114. });
  115. });