fix: use Proxy-based CalendarServiceMap mock to prevent test flakes (#28787)

* fix: add vi.mock calls to prevent InstantBookingCreateService test flake

Add two vi.mock() calls ported from cal repo to prevent
'Closing rpc while fetch was pending' errors:

1. Mock @calcom/app-store/calendar.services.generated with a Proxy
   to prevent real calendar modules (feishu, lark) from triggering
   async getAppAccessToken fetch calls during test worker shutdown.

2. Mock @calcom/features/ee/organizations/di/OrganizationRepository.container
   to prevent deep transitive import chain from triggering a Vitest
   module-resolution RPC still in flight at shutdown.

Also removes the narrow feishucalendar mock which is now covered by
the broader calendar.services.generated Proxy mock.

* fix: update bookingScenario to use Proxy-based CalendarServiceMap

Replace the plain-object CalendarServiceMap mock with a Proxy-based
implementation that catches ALL calendar service accesses. This prevents
real calendar modules (feishu, lark, etc.) from being imported during
tests, which was causing 'Closing rpc while fetch was pending' errors
when the Vitest worker shut down.

Also update mockCalendar to use a shared mock map directly instead of
dynamically importing calendar.services.generated, avoiding loading
real calendar service modules during test execution.
This commit is contained in:
Sahitya Chandra authored and GitHub committed 2026-04-08 13:17:30 +05:30
1 parent 77eb4c80bf
commit dcbb417aec
2 files changed
+192 -166

No files matched your search

@@ -1,29 +1,47 @@
import prismock from "@calcom/testing/lib/__mocks__/prisma";
import {
createBookingScenario,
getScenarioData,
getGoogleCalendarCredential,
TestData,
getOrganizer,
mockSuccessfulVideoMeetingCreation,
getScenarioData,
mockCalendarToHaveNoBusySlots,
mockNoTranslations,
mockSuccessfulVideoMeetingCreation,
TestData,
} from "@calcom/testing/lib/bookingScenario/bookingScenario";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { BookingStatus } from "@calcom/prisma/enums";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { getInstantBookingCreateService } from "../../di/InstantBookingCreateService.container";
import type { CreateInstantBookingData } from "../dto/types";
vi.mock("@calcom/features/notifications/sendNotification", () => ({
sendNotification: vi.fn(),
// Mock calendar services map to prevent real calendar service modules (feishu, lark, etc.) from being
// imported. Their top-level imports trigger async fetch calls (getAppAccessToken) that cause
// "Closing rpc while fetch was pending" errors when the test worker shuts down.
// This vi.mock must be in the test file itself (not just in bookingScenario.ts) to guarantee
// Vitest hoists it before any transitive imports resolve the real module.
vi.mock("@calcom/app-store/calendar.services.generated", () => ({
CalendarServiceMap: new Proxy(
{},
{
get(_target: Record<string, unknown>, prop: string) {
if (typeof prop === "symbol") return undefined;
return Promise.resolve({ default: vi.fn() });
},
}
),
}));
vi.mock("@calcom/app-store/feishucalendar/lib/CalendarService", () => ({
default: class MockFeishuCalendarService {},
// Mock OrganizationRepository container to prevent a deep transitive import chain
// (InstantBookingCreateService → getBookingFields → workflows/types → routing-forms →
// webhooks DI → eventTypes → OrganizationRepository.container) from triggering a Vitest
// module-resolution RPC that is still in flight when the worker shuts down, causing
// "Closing rpc while fetch was pending" errors.
vi.mock("@calcom/features/ee/organizations/di/OrganizationRepository.container", () => ({
getOrganizationRepository: vi.fn().mockReturnValue({}),
}));
vi.mock("@calcom/features/notifications/sendNotification", () => ({
sendNotification: vi.fn(),
}));
vi.mock("@calcom/features/conferencing/lib/videoClient", () => ({
@@ -257,9 +275,7 @@ describe("handleInstantMeeting", () => {
const callArgs = onBookingCreatedSpy.mock.calls[0][0];
expect(callArgs.payload.booking.uid).toBe(result.bookingUid);
expect(callArgs.payload.config.isDryRun).toBe(false);
expect(callArgs.actor).toEqual(
expect.objectContaining({ identifiedBy: expect.any(String) })
);
expect(callArgs.actor).toEqual(expect.objectContaining({ identifiedBy: expect.any(String) }));
expect(callArgs.auditData).toEqual(
expect.objectContaining({
startTime: expect.any(Number),