e2eRendererBridgeRootHook.test.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import Mocha, { Test } from "mocha";
  2. import { describe, expect, it } from "vitest";
  3. import { createE2ERendererBridgeRootHooks } from "./e2e/helpers/e2e-renderer-bridge-root-hook.js";
  4. class SilentReporter {
  5. constructor(_runner: Mocha.Runner) {}
  6. }
  7. async function runMochaWithRendererPreflight(
  8. runPreflight: () => Promise<void>,
  9. readBeforeSessionError?: () => unknown,
  10. ): Promise<{ events: string[]; failureCount: number; hookError: Error | null }> {
  11. const events: string[] = [];
  12. const mocha = new Mocha({
  13. reporter: SilentReporter,
  14. rootHooks: createE2ERendererBridgeRootHooks(async () => {
  15. events.push("preflight");
  16. await runPreflight();
  17. }, readBeforeSessionError),
  18. });
  19. mocha.suite.addTest(
  20. new Test("product case", () => {
  21. events.push("test-body");
  22. }),
  23. );
  24. let hookError: Error | null = null;
  25. const failureCount = await new Promise<number>((resolve) => {
  26. const runner = mocha.run(resolve);
  27. runner.on("fail", (_runnable, error: Error) => {
  28. hookError = error;
  29. });
  30. });
  31. return { events, failureCount, hookError };
  32. }
  33. describe("desktop e2e renderer bridge root hook", () => {
  34. it("runs renderer preflight before the product test body", async () => {
  35. const result = await runMochaWithRendererPreflight(async () => {});
  36. expect(result).toEqual({
  37. events: ["preflight", "test-body"],
  38. failureCount: 0,
  39. hookError: null,
  40. });
  41. });
  42. it("reports preflight rejection as a root-hook failure and skips the test body", async () => {
  43. const result = await runMochaWithRendererPreflight(async () => {
  44. throw new Error("e2e-bridge-preflight infra failure: forced");
  45. });
  46. expect(result.events).toEqual(["preflight"]);
  47. expect(result.failureCount).toBe(1);
  48. expect(result.hookError?.message).toBe("e2e-bridge-preflight infra failure: forced");
  49. });
  50. it("reports beforeSession setup failure before preflight and skips the product test", async () => {
  51. const result = await runMochaWithRendererPreflight(
  52. async () => {
  53. throw new Error("preflight must not run");
  54. },
  55. () => new Error("process exit barrier failed"),
  56. );
  57. expect(result.events).toEqual([]);
  58. expect(result.failureCount).toBe(1);
  59. expect(result.hookError?.message).toBe(
  60. "e2e-before-session infra failure: process exit barrier failed",
  61. );
  62. });
  63. it("treats a falsy non-null beforeSession rejection as a failure", async () => {
  64. const result = await runMochaWithRendererPreflight(async () => {}, () => "");
  65. expect(result.events).toEqual([]);
  66. expect(result.failureCount).toBe(1);
  67. expect(result.hookError?.message).toBe("e2e-before-session infra failure: ");
  68. });
  69. });