fix: add missing vi.mock() calls to prevent vitest worker shutdown flakiness (#28459)

* fix: add missing vi.mock() calls to prevent vitest worker shutdown flakiness

Add vi.mock() calls for modules that trigger background network requests
or database connections during import. These transitive imports can cause
the vitest worker RPC to shut down while pending fetch/network operations
are still in flight, resulting in flaky test failures with:
  Error: [vitest-worker]: Closing rpc while "fetch" was pending

The primary modules mocked are:
- @calcom/app-store/delegationCredential (triggers credential lookups)
- @calcom/prisma (triggers database initialization)
- @calcom/features/calendars/lib/CalendarManager (triggers calendar API calls)
- @calcom/features/auth/lib/verifyEmail (triggers email service)
- @calcom/lib/domainManager/organization (triggers domain lookups)

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix: remove conflicting empty prisma mocks from files with prismock/prismaMock setups

- Remove vi.mock('@calcom/prisma', () => ({ default: {}, prisma: {} })) from 28 files
  that already have prismock/prismaMock test doubles. Vitest hoists all vi.mock() calls
  and the last one wins, so these empty mocks were overriding the functional test doubles.
- Fix CalendarSubscriptionService.test.ts to reuse the shared mock from
  __mocks__/delegationCredential instead of creating a new unconfigured vi.fn()
- Remove DelegationCredentialRepository.test.ts empty prisma mock (different pattern)
- Remove vi.mock from inside beforeEach in intentToCreateOrg.handler.test.ts

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix: add comprehensive delegationCredential mock exports to prevent CI test failures

The vi.mock blocks for @calcom/app-store/delegationCredential were missing
exports that the code under test transitively imports (e.g.
enrichUsersWithDelegationCredentials, enrichUserWithDelegationCredentialsIncludeServiceAccountKey,
buildAllCredentials, getFirstDelegationConferencingCredentialAppLocation).

Added all exports with passthrough implementations so the booking flow
works correctly without triggering real network requests.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix: correct credential mock return shapes to match real module API

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix: revert unintended yarn.lock changes

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
RomitandDevin AI authored and GitHub committed 2026-03-17 09:55:37 +05:30
1 parent 353f71bdc6
commit 4291a59b2c
70 files changed
+561 -271

No files matched your search

+6 -2
View File
@@ -1,9 +1,8 @@
import { describe, expect, it, vi } from "vitest";
import dayjs from "@calcom/dayjs";
import { getCheckBookingLimitsService } from "@calcom/features/di/containers/BookingLimits";
import type { IntervalLimit } from "@calcom/lib/intervalLimits/intervalLimitSchema";
import { validateIntervalLimitOrder } from "@calcom/lib/intervalLimits/validateIntervalLimitOrder";
import { describe, expect, it, vi } from "vitest";
const mockCountBookingsByEventTypeAndDateRange = vi.fn();
vi.mock("@calcom/features/bookings/repositories/BookingRepository", () => ({
@@ -14,6 +13,11 @@ vi.mock("@calcom/features/bookings/repositories/BookingRepository", () => ({
}),
}));
vi.mock("@calcom/prisma", () => ({
default: {},
prisma: {},
}));
type Mockdata = {
id: number;
startDate: Date;
@@ -1,8 +1,7 @@
import { describe, expect, it, vi } from "vitest";
import dayjs from "@calcom/dayjs";
import { checkDurationLimit, checkDurationLimits } from "@calcom/features/bookings/lib/checkDurationLimits";
import { validateIntervalLimitOrder } from "@calcom/lib/intervalLimits/validateIntervalLimitOrder";
import { describe, expect, it, vi } from "vitest";
const mockGetTotalBookingDuration = vi.fn();
vi.mock("@calcom/features/bookings/repositories/BookingRepository", () => ({
@@ -13,6 +12,11 @@ vi.mock("@calcom/features/bookings/repositories/BookingRepository", () => ({
}),
}));
vi.mock("@calcom/prisma", () => ({
default: {},
prisma: {},
}));
type MockData = {
id: number;
startDate: Date;
+48 -6
View File
@@ -2,19 +2,61 @@ import {
createBookingScenario,
getOrganizer,
getScenarioData,
TestData,
mockSuccessfulVideoMeetingCreation,
TestData,
} from "@calcom/testing/lib/bookingScenario/bookingScenario";
import { describe, it, beforeEach, vi, expect } from "vitest";
import { makeUserActor } from "@calcom/features/booking-audit/lib/makeActor";
import { getBookingEventHandlerService } from "@calcom/features/bookings/di/BookingEventHandlerService.container";
import * as handleConfirmationModule from "@calcom/features/bookings/lib/handleConfirmation";
import { distributedTracing } from "@calcom/lib/tracing/factory";
import { BookingStatus } from "@calcom/prisma/enums";
import { confirmHandler } from "@calcom/trpc/server/routers/viewer/bookings/confirm.handler";
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
import { makeUserActor } from "@calcom/features/booking-audit/lib/makeActor";
import { getBookingEventHandlerService } from "@calcom/features/bookings/di/BookingEventHandlerService.container";
import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("@calcom/app-store/delegationCredential", () => ({
enrichHostsWithDelegationCredentials: vi.fn().mockImplementation(({ hosts }) => hosts),
enrichUsersWithDelegationCredentials: vi.fn().mockImplementation(({ users }) => users),
enrichUserWithDelegationCredentialsIncludeServiceAccountKey: vi.fn().mockImplementation(({ user }) => user),
enrichUserWithDelegationCredentials: vi.fn().mockImplementation(({ user }) => user),
enrichUserWithDelegationConferencingCredentialsWithoutOrgId: vi.fn().mockImplementation(({ user }) => user),
getUsersCredentialsIncludeServiceAccountKey: vi.fn().mockResolvedValue([]),
getUsersCredentials: vi.fn().mockResolvedValue([]),
getCredentialForSelectedCalendar: vi.fn(),
getAllDelegationCredentialsForUserIncludeServiceAccountKey: vi.fn().mockResolvedValue([]),
getAllDelegationCredentialsForUser: vi.fn().mockResolvedValue([]),
getAllDelegatedCalendarCredentialsForUser: vi.fn().mockResolvedValue([]),
getAllDelegationCredentialsForUserByAppType: vi.fn().mockResolvedValue([]),
getAllDelegationCredentialsForUserByAppSlug: vi.fn().mockResolvedValue([]),
buildAllCredentials: vi.fn().mockImplementation(({ existingCredentials }) => existingCredentials || []),
getDelegationCredentialOrFindRegularCredential: vi.fn(),
getDelegationCredentialOrRegularCredential: vi.fn(),
getFirstDelegationConferencingCredential: vi.fn(),
getFirstDelegationConferencingCredentialAppLocation: vi.fn(),
findUniqueDelegationCalendarCredential: vi.fn(),
assertSuccessfullyConfiguredInWorkspace: vi.fn(),
}));
vi.mock("@calcom/features/calendars/lib/CalendarManager", () => ({
createEvent: vi.fn(),
updateEvent: vi.fn(),
deleteEvent: vi.fn(),
getBusyCalendarTimes: vi.fn(),
}));
vi.mock("@calcom/features/auth/lib/verifyEmail", () => ({
checkIfEmailIsBlockedInWatchlist: vi.fn(),
isEmailVerified: vi.fn(),
}));
vi.mock("@calcom/lib/domainManager/organization", () => ({
getOrgDomainConfigFromHostname: vi.fn(),
subdomainSuffix: vi.fn(),
}));
vi.mock("@calcom/emails/organization-email-service", () => ({
OrganizationEmailService: vi.fn(),
}));
vi.mock("@calcom/features/bookings/di/BookingEventHandlerService.container", () => {
const onBookingAccepted = vi.fn().mockResolvedValue(undefined);
@@ -57,7 +57,6 @@ vi.mock("@calcom/i18n/server", () => {
},
};
});
describe("handleChildrenEventTypes", () => {
describe("Shortcircuits", () => {
it("Returns message 'No managed event type'", async () => {