e2e-lifecycle-measurement.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import type {
  2. E2ELifecycleEvent,
  3. E2ELifecyclePhase,
  4. } from "../reporting/e2e-reporting-types.js";
  5. export async function runMeasuredE2ELifecyclePhase<T>(
  6. phase: E2ELifecyclePhase,
  7. context: { cid?: string; specs?: string[] },
  8. record: (event: E2ELifecycleEvent) => void,
  9. run: () => Promise<T> | T,
  10. ): Promise<T> {
  11. const startedAt = new Date();
  12. let failed = false;
  13. let failure: unknown = null;
  14. try {
  15. return await run();
  16. } catch (error) {
  17. failed = true;
  18. failure = error;
  19. throw error;
  20. } finally {
  21. const completedAt = new Date();
  22. record({
  23. cid: context.cid,
  24. completedAt: completedAt.toISOString(),
  25. durationMs: completedAt.getTime() - startedAt.getTime(),
  26. error: failed ? formatErrorMessage(failure) : undefined,
  27. phase,
  28. specs: context.specs ?? [],
  29. startedAt: startedAt.toISOString(),
  30. status: failed ? "failed" : "passed",
  31. });
  32. }
  33. }
  34. function formatErrorMessage(error: unknown) {
  35. return error instanceof Error ? error.message : String(error);
  36. }