e2eInfrastructureLifecycle.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { readFileSync } from "node:fs";
  2. import { describe, expect, it, vi } from "vitest";
  3. import { createE2ERunnerFinalizationGuard } from "./e2e/helpers/e2e-runner-finalization-guard.js";
  4. import { runMeasuredE2ELifecyclePhase } from "./e2e/helpers/e2e-lifecycle-measurement.js";
  5. describe("desktop E2E infrastructure lifecycle", () => {
  6. it("records a failed lifecycle phase before rethrowing the original error", async () => {
  7. const record = vi.fn();
  8. const failure = new Error("beforeSession setup failed");
  9. await expect(
  10. runMeasuredE2ELifecyclePhase(
  11. "before-session",
  12. { cid: "0-52", specs: ["./test/e2e/example.test.ts"] },
  13. record,
  14. async () => {
  15. throw failure;
  16. },
  17. ),
  18. ).rejects.toBe(failure);
  19. expect(record).toHaveBeenCalledWith(
  20. expect.objectContaining({
  21. cid: "0-52",
  22. error: "beforeSession setup failed",
  23. phase: "before-session",
  24. status: "failed",
  25. }),
  26. );
  27. });
  28. it("records falsy thrown values as failures", async () => {
  29. const record = vi.fn();
  30. await expect(
  31. runMeasuredE2ELifecyclePhase("prepare", { specs: [] }, record, async () => {
  32. throw "";
  33. }),
  34. ).rejects.toBe("");
  35. expect(record).toHaveBeenCalledWith(
  36. expect.objectContaining({ error: "", status: "failed" }),
  37. );
  38. });
  39. it("keeps a referenced finalization timer until it is disarmed", () => {
  40. vi.useFakeTimers();
  41. try {
  42. const onTimeout = vi.fn();
  43. const guard = createE2ERunnerFinalizationGuard({ onTimeout, timeoutMs: 5_000 });
  44. guard.arm();
  45. expect(guard.isArmed()).toBe(true);
  46. guard.disarm();
  47. vi.advanceTimersByTime(5_000);
  48. expect(onTimeout).not.toHaveBeenCalled();
  49. expect(guard.isArmed()).toBe(false);
  50. } finally {
  51. vi.useRealTimers();
  52. }
  53. });
  54. it("arms after the last worker and disarms before normal onComplete", () => {
  55. const source = readFileSync(new URL("../wdio.conf.ts", import.meta.url), "utf8");
  56. const workerEnd = source.slice(
  57. source.indexOf(" onWorkerEnd("),
  58. source.indexOf(" async afterTest(", source.indexOf(" onWorkerEnd(")),
  59. );
  60. const onComplete = source.slice(source.indexOf(" async onComplete("));
  61. const forcedFinalization = source.slice(
  62. source.indexOf("function forceFinalizeE2ERunnerFromGuard()"),
  63. source.indexOf("async function resetE2EHome("),
  64. );
  65. expect(workerEnd).toContain("e2eRunnerFinalizationGuard.arm()");
  66. expect(workerEnd.indexOf("e2eRunnerFinalizationGuard.arm()")).toBeLessThan(
  67. workerEnd.indexOf("if (!workerLifecycle)"),
  68. );
  69. expect(workerEnd).toContain('status: exitCode === 0 ? "passed" : "failed"');
  70. expect(forcedFinalization).toContain("armE2ERunnerExitWatchdog(1)");
  71. expect(onComplete).toContain("e2eRunnerFinalizationGuard.disarm()");
  72. });
  73. });