Files
calendar/packages/testing/src/setupVitest.ts
T
Keith WilliamsGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
7a6f4b2594 fix: add global CRM services mock to prevent test flakes (#26716)
* fix: mock CRM services to prevent test flakes in handlePaymentSuccess.test.ts

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

* fix: add global CRM services mock to prevent test flakes

The test flakes were caused by the Salesforce CRM service importing @urql/core,
which triggers fetch calls during module loading. These fetch calls remain
pending when the Vitest worker shuts down, causing the error:
'[vitest-worker]: Closing rpc while fetch was pending'

This fix adds a global mock for CrmServiceMap in setupVitest.ts, similar to
how payment services are already mocked. This prevents the Salesforce module
from being loaded during test execution.

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>
2026-01-11 18:18:05 +05:30

198 lines
5.0 KiB
TypeScript

import matchers from "@testing-library/jest-dom/matchers";
import ResizeObserver from "resize-observer-polyfill";
import { vi, expect } from "vitest";
import createFetchMock from "vitest-fetch-mock";
import type { CalendarService } from "@calcom/types/Calendar";
global.ResizeObserver = ResizeObserver;
const fetchMocker = createFetchMock(vi);
// sets globalThis.fetch and globalThis.fetchMock to our mocked version
fetchMocker.enableMocks();
expect.extend(matchers);
class MockExchangeCalendarService implements CalendarService {
constructor() {}
async createEvent() {
return {
uid: "mock",
id: "mock",
password: "",
type: "",
url: "",
additionalInfo: {},
};
}
async updateEvent() {
return {
uid: "mock",
id: "mock",
password: "",
type: "",
url: "",
additionalInfo: {},
};
}
async deleteEvent() {}
async getAvailability() {
return [];
}
async listCalendars() {
return [];
}
}
vi.mock("@calcom/exchangecalendar/lib/CalendarService", () => ({
default: MockExchangeCalendarService,
}));
vi.mock("@calcom/exchange2013calendar/lib/CalendarService", () => ({
default: MockExchangeCalendarService,
}));
vi.mock("@calcom/exchange2016calendar/lib/CalendarService", () => ({
default: MockExchangeCalendarService,
}));
const MOCK_PAYMENT_UID = "MOCK_PAYMENT_UID";
class MockPaymentService {
constructor(credentials?: any) {
this.credentials = credentials;
}
private credentials: any;
async create(
payment: any,
bookingId: number,
userId: number,
username: string | null,
bookerName: string | null,
paymentOption: any,
bookerEmail: string,
bookerPhoneNumber?: string | null,
selectedEventTypeTitle?: string,
eventTitle?: string
) {
const { default: prismaMock } = await import("@calcom/testing/lib/__mocks__/prisma");
const externalId = "mock_payment_external_id";
const paymentCreateData = {
uid: MOCK_PAYMENT_UID,
appId: null,
bookingId,
fee: 10,
success: false,
refunded: false,
data: {},
externalId,
paymentOption,
amount: payment.amount,
currency: payment.currency,
};
const createdPayment = await prismaMock.payment.create({
data: paymentCreateData,
});
return createdPayment;
}
async collectCard() {
return { success: true };
}
async chargeCard() {
return { success: true };
}
async refund() {
return { success: true };
}
async deletePayment() {
return { success: true };
}
async afterPayment(event: any, booking: any, paymentData: any) {
const { sendAwaitingPaymentEmailAndSMS } = await import("@calcom/emails/email-manager");
await sendAwaitingPaymentEmailAndSMS({
...event,
paymentInfo: {
link: "http://mock-payment.example.com/",
paymentOption: paymentData.paymentOption || "ON_BOOKING",
amount: paymentData.amount,
currency: paymentData.currency,
},
});
return { success: true };
}
}
vi.mock("@calcom/app-store/stripepayment/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/paypal/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/alby/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/hitpay/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/btcpayserver/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/mock-payment-app/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/payment.services.generated", () => ({
PaymentServiceMap: {
stripepayment: Promise.resolve({ PaymentService: MockPaymentService }),
paypal: Promise.resolve({ PaymentService: MockPaymentService }),
alby: Promise.resolve({ PaymentService: MockPaymentService }),
hitpay: Promise.resolve({ PaymentService: MockPaymentService }),
btcpayserver: Promise.resolve({ PaymentService: MockPaymentService }),
"mock-payment-app": Promise.resolve({ PaymentService: MockPaymentService }),
},
}));
class MockCrmService {
constructor() {}
async createEvent() {
return [];
}
async updateEvent() {
return [];
}
async deleteEvent() {}
async getContacts() {
return [];
}
async createContacts() {
return [];
}
}
vi.mock("@calcom/app-store/crm.apps.generated", () => ({
CrmServiceMap: {
closecom: Promise.resolve({ default: MockCrmService }),
hubspot: Promise.resolve({ default: MockCrmService }),
"pipedrive-crm": Promise.resolve({ default: MockCrmService }),
salesforce: Promise.resolve({ default: MockCrmService }),
"zoho-bigin": Promise.resolve({ default: MockCrmService }),
zohocrm: Promise.resolve({ default: MockCrmService }),
},
}));
if (!process.env.INTEGRATION_TESTS) {
vi.mock("@calcom/app-store/salesforce/lib/graphql/documents/queries", () => ({
GetAccountRecordsForRRSkip: {},
}));
}