Skip to content

Commit dcbb417

Browse files
fix: use Proxy-based CalendarServiceMap mock to prevent test flakes (calcom#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.
1 parent 77eb4c8 commit dcbb417

File tree

2 files changed

+192
-166
lines changed

2 files changed

+192
-166
lines changed

packages/features/bookings/lib/service/InstantBookingCreateService.test.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,47 @@
11
import prismock from "@calcom/testing/lib/__mocks__/prisma";
2-
32
import {
43
createBookingScenario,
5-
getScenarioData,
64
getGoogleCalendarCredential,
7-
TestData,
85
getOrganizer,
9-
mockSuccessfulVideoMeetingCreation,
6+
getScenarioData,
107
mockCalendarToHaveNoBusySlots,
118
mockNoTranslations,
9+
mockSuccessfulVideoMeetingCreation,
10+
TestData,
1211
} from "@calcom/testing/lib/bookingScenario/bookingScenario";
13-
14-
import { describe, it, expect, vi, beforeEach } from "vitest";
15-
1612
import { BookingStatus } from "@calcom/prisma/enums";
17-
13+
import { beforeEach, describe, expect, it, vi } from "vitest";
1814
import { getInstantBookingCreateService } from "../../di/InstantBookingCreateService.container";
1915
import type { CreateInstantBookingData } from "../dto/types";
2016

21-
vi.mock("@calcom/features/notifications/sendNotification", () => ({
22-
sendNotification: vi.fn(),
17+
// Mock calendar services map to prevent real calendar service modules (feishu, lark, etc.) from being
18+
// imported. Their top-level imports trigger async fetch calls (getAppAccessToken) that cause
19+
// "Closing rpc while fetch was pending" errors when the test worker shuts down.
20+
// This vi.mock must be in the test file itself (not just in bookingScenario.ts) to guarantee
21+
// Vitest hoists it before any transitive imports resolve the real module.
22+
vi.mock("@calcom/app-store/calendar.services.generated", () => ({
23+
CalendarServiceMap: new Proxy(
24+
{},
25+
{
26+
get(_target: Record<string, unknown>, prop: string) {
27+
if (typeof prop === "symbol") return undefined;
28+
return Promise.resolve({ default: vi.fn() });
29+
},
30+
}
31+
),
32+
}));
33+
34+
// Mock OrganizationRepository container to prevent a deep transitive import chain
35+
// (InstantBookingCreateService → getBookingFields → workflows/types → routing-forms →
36+
// webhooks DI → eventTypes → OrganizationRepository.container) from triggering a Vitest
37+
// module-resolution RPC that is still in flight when the worker shuts down, causing
38+
// "Closing rpc while fetch was pending" errors.
39+
vi.mock("@calcom/features/ee/organizations/di/OrganizationRepository.container", () => ({
40+
getOrganizationRepository: vi.fn().mockReturnValue({}),
2341
}));
2442

25-
vi.mock("@calcom/app-store/feishucalendar/lib/CalendarService", () => ({
26-
default: class MockFeishuCalendarService {},
43+
vi.mock("@calcom/features/notifications/sendNotification", () => ({
44+
sendNotification: vi.fn(),
2745
}));
2846

2947
vi.mock("@calcom/features/conferencing/lib/videoClient", () => ({
@@ -257,9 +275,7 @@ describe("handleInstantMeeting", () => {
257275
const callArgs = onBookingCreatedSpy.mock.calls[0][0];
258276
expect(callArgs.payload.booking.uid).toBe(result.bookingUid);
259277
expect(callArgs.payload.config.isDryRun).toBe(false);
260-
expect(callArgs.actor).toEqual(
261-
expect.objectContaining({ identifiedBy: expect.any(String) })
262-
);
278+
expect(callArgs.actor).toEqual(expect.objectContaining({ identifiedBy: expect.any(String) }));
263279
expect(callArgs.auditData).toEqual(
264280
expect.objectContaining({
265281
startTime: expect.any(Number),

0 commit comments

Comments
 (0)