Scheduling infrastructure for absolutely everyone.
fix: add cleanup and mock embed-iframe to prevent test teardown leak (#26377)
Keith Williams64dcf5d3f6b2
diff --git a/apps/web/components/apps/routing-forms/TestFormDialog.test.tsx b/apps/web/components/apps/routing-forms/TestFormDialog.test.tsxindex 5f2b366b83e..1dbeec8d5ba 100644--- a/apps/web/components/apps/routing-forms/TestFormDialog.test.tsx+++ b/apps/web/components/apps/routing-forms/TestFormDialog.test.tsx@@ -1,6 +1,6 @@-import { render, screen, fireEvent } from "@testing-library/react";+import { render, screen, fireEvent, cleanup } from "@testing-library/react";import type { Mock } from "vitest";-import { vi } from "vitest";+import { vi, beforeEach, afterEach, describe, expect, it } from "vitest";import { findMatchingRoute } from "@calcom/app-store/routing-forms/lib/processRoute";@@ -194,6 +194,15 @@ describe("TestFormDialog", () => {beforeEach(() => {resetFindTeamMembersMatchingAttributeLogicResponse();vi.clearAllMocks();+ vi.useFakeTimers();+ });++ afterEach(() => {+ // Flush any pending timers (like Radix FocusScope setTimeout) before cleanup+ // to prevent them from firing after jsdom teardown+ vi.runOnlyPendingTimers();+ vi.useRealTimers();+ cleanup();});it("renders the dialog when open", () => {diff --git a/apps/web/components/booking/__tests__/CancelBooking.cancellationFee.test.tsx b/apps/web/components/booking/__tests__/CancelBooking.cancellationFee.test.tsxindex d61226b6837..ce37d66d776 100644--- a/apps/web/components/booking/__tests__/CancelBooking.cancellationFee.test.tsx+++ b/apps/web/components/booking/__tests__/CancelBooking.cancellationFee.test.tsx@@ -1,15 +1,40 @@-import { render, screen } from "@testing-library/react";-import * as React from "react";-import { describe, expect, it, vi, beforeAll } from "vitest";+import { render, screen, cleanup } from "@testing-library/react";+import { describe, expect, it, vi, beforeAll, afterAll, afterEach } from "vitest";import * as shouldChargeModule from "@calcom/features/bookings/lib/payment/shouldChargeNoShowCancellationFee";import CancelBooking from "../CancelBooking";+// Mock the embed-iframe module to prevent it from scheduling timers/RAF that can cause+// teardown issues when jsdom environment is destroyed+vi.mock("@calcom/embed-core/embed-iframe", () => ({+ sdkActionManager: null,+}));++// Store original scrollIntoView to restore later+const originalScrollIntoView = Element.prototype.scrollIntoView;+beforeAll(() => {+ // jsdom doesn't implement scrollIntoView, so we need to mock itElement.prototype.scrollIntoView = vi.fn();});+afterAll(() => {+ // Restore scrollIntoView to avoid polluting other tests in the same worker+ if (originalScrollIntoView) {+ Element.prototype.scrollIntoView = originalScrollIntoView;+ } else {+ // If it was originally undefined, delete it+ delete (Element.prototype as { scrollIntoView?: unknown }).scrollIntoView;+ }+ // Clean up module mocks to avoid polluting other tests+ vi.unmock("@calcom/embed-core/embed-iframe");+});++afterEach(() => {+ cleanup();+});+vi.mock("@calcom/trpc/react", () => ({trpc: {viewer: {diff --git a/packages/trpc/server/routers/viewer/bookings/confirm.handler.test.ts b/packages/trpc/server/routers/viewer/bookings/confirm.handler.test.tsindex f7da466cfe7..62a9444f3f1 100644--- a/packages/trpc/server/routers/viewer/bookings/confirm.handler.test.ts+++ b/packages/trpc/server/routers/viewer/bookings/confirm.handler.test.ts@@ -1,13 +1,11 @@/* eslint-disable @typescript-eslint/ban-ts-comment */// @ts-nocheck// TODO: Bring this test back with the correct setup (no illegal imports)+// NOTE: All imports except vitest are deferred to inside the skipped describe blocks+// to prevent module loading side effects during test collection (which can cause+// "Closing rpc while fetch was pending" errors from Salesforce GraphQL module imports)import { describe, beforeEach, vi, expect, test } from "vitest";-import { BookingStatus } from "@calcom/prisma/enums";--import type { TrpcSessionUser } from "../../../types";-import { confirmHandler } from "./confirm.handler";-//eslint-disable-next-line playwright/no-skipped-testdescribe.skip("confirmHandler", () => {beforeEach(() => {diff --git a/packages/trpc/server/routers/viewer/bookings/editLocation.handler.test.ts b/packages/trpc/server/routers/viewer/bookings/editLocation.handler.test.tsindex d0b53781c92..d33680ce06c 100644--- a/packages/trpc/server/routers/viewer/bookings/editLocation.handler.test.ts+++ b/packages/trpc/server/routers/viewer/bookings/editLocation.handler.test.ts@@ -1,24 +1,11 @@/* eslint-disable @typescript-eslint/ban-ts-comment */// @ts-nocheck// TODO: Bring this test back with the correct setup (no illegal imports)+// NOTE: All imports except vitest are deferred to inside the skipped describe blocks+// to prevent module loading side effects during test collection (which can cause+// "Closing rpc while fetch was pending" errors from watchlist module imports)import { describe, expect, test, vi, beforeEach } from "vitest";-import { prisma } from "@calcom/prisma";-import { BookingStatus } from "@calcom/prisma/enums";--import {- editLocationHandler,- getLocationForOrganizerDefaultConferencingAppInEvtFormat,- SystemError,- UserError,-} from "./editLocation.handler";--vi.mock("@calcom/prisma", () => {- return {- prisma: vi.fn(),- };-});-describe.skip("getLocationForOrganizerDefaultConferencingAppInEvtFormat", () => {const mockTranslate = vi.fn((key: string) => key);