fix: improve BookingReferenceRepository integration test isolation (#26707)
The integration tests were flaky because they relied on a shared booking created in beforeAll, which could cause race conditions when tests run in parallel or when cleanup from previous runs interfered. Changes: - Use seed user (member0-acme@example.com) when available, fallback to creating a test user for local development - Create a fresh booking in beforeEach instead of sharing one across tests - Track created bookings and clean them up properly in afterEach - Fix import organization to satisfy linter Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
9d9a85ddd9
commit
539214b164
1 file changed
+76
-55
+76
-55
@@ -1,82 +1,103 @@
|
||||
import { describe, it, expect, beforeAll, afterAll, afterEach } from "vitest";
|
||||
|
||||
import prisma from "@calcom/prisma";
|
||||
import type { Booking, Credential, User } from "@calcom/prisma/client";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import type { Booking, Credential } from "@calcom/prisma/client";
|
||||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { BookingReferenceRepository } from "./BookingReferenceRepository";
|
||||
|
||||
const testRunId = `${Date.now()}-${Math.random().toString(36).substring(2, 8)}`;
|
||||
|
||||
let testUserId: number;
|
||||
let testCredential: Credential;
|
||||
let testBooking: Booking;
|
||||
const createdBookingReferenceIds: number[] = [];
|
||||
const createdBookingIds: number[] = [];
|
||||
|
||||
async function clearTestBookingReferences() {
|
||||
if (createdBookingReferenceIds.length > 0) {
|
||||
await prisma.bookingReference.deleteMany({
|
||||
where: {
|
||||
id: {
|
||||
in: createdBookingReferenceIds,
|
||||
},
|
||||
},
|
||||
});
|
||||
createdBookingReferenceIds.length = 0;
|
||||
}
|
||||
}
|
||||
|
||||
async function clearTestBookings() {
|
||||
if (createdBookingIds.length > 0) {
|
||||
await prisma.bookingReference.deleteMany({
|
||||
where: {
|
||||
bookingId: {
|
||||
in: createdBookingIds,
|
||||
},
|
||||
},
|
||||
});
|
||||
await prisma.booking.deleteMany({
|
||||
where: {
|
||||
id: {
|
||||
in: createdBookingIds,
|
||||
},
|
||||
},
|
||||
});
|
||||
createdBookingIds.length = 0;
|
||||
}
|
||||
}
|
||||
|
||||
describe("BookingReferenceRepository Integration Tests", () => {
|
||||
let testUser: User;
|
||||
let testCredential: Credential;
|
||||
let testBooking: Booking;
|
||||
const createdBookingReferenceIds: number[] = [];
|
||||
|
||||
beforeAll(async () => {
|
||||
testUser = await prisma.user.create({
|
||||
data: {
|
||||
email: `bookingreference-test-${testRunId}@example.com`,
|
||||
username: `bookingreference-test-${testRunId}`,
|
||||
},
|
||||
let testUser = await prisma.user.findFirst({
|
||||
where: { email: "member0-acme@example.com" },
|
||||
});
|
||||
|
||||
testCredential = await prisma.credential.create({
|
||||
data: {
|
||||
type: "google_calendar",
|
||||
key: {},
|
||||
userId: testUser.id,
|
||||
},
|
||||
if (!testUser) {
|
||||
testUser = await prisma.user.create({
|
||||
data: {
|
||||
email: `bookingreference-test-${testRunId}@example.com`,
|
||||
username: `bookingreference-test-${testRunId}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
testUserId = testUser.id;
|
||||
|
||||
let credential = await prisma.credential.findFirst({
|
||||
where: { userId: testUserId, type: "google_calendar" },
|
||||
});
|
||||
|
||||
if (!credential) {
|
||||
credential = await prisma.credential.create({
|
||||
data: {
|
||||
type: "google_calendar",
|
||||
key: {},
|
||||
userId: testUserId,
|
||||
},
|
||||
});
|
||||
}
|
||||
testCredential = credential;
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
testBooking = await prisma.booking.create({
|
||||
data: {
|
||||
uid: `test-booking-uid-${testRunId}`,
|
||||
uid: `test-booking-uid-${testRunId}-${Date.now()}`,
|
||||
title: "Test Booking",
|
||||
startTime: new Date(),
|
||||
endTime: new Date(),
|
||||
userId: testUser.id,
|
||||
userId: testUserId,
|
||||
},
|
||||
});
|
||||
createdBookingIds.push(testBooking.id);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
if (createdBookingReferenceIds.length > 0) {
|
||||
await prisma.bookingReference.deleteMany({
|
||||
where: {
|
||||
id: {
|
||||
in: createdBookingReferenceIds,
|
||||
},
|
||||
},
|
||||
});
|
||||
createdBookingReferenceIds.splice(0, createdBookingReferenceIds.length);
|
||||
}
|
||||
await clearTestBookingReferences();
|
||||
await clearTestBookings();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await prisma.bookingReference.deleteMany({
|
||||
where: {
|
||||
bookingId: testBooking.id,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.booking.deleteMany({
|
||||
where: {
|
||||
id: testBooking.id,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.credential.deleteMany({
|
||||
where: {
|
||||
id: testCredential.id,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.user.deleteMany({
|
||||
where: {
|
||||
id: testUser.id,
|
||||
},
|
||||
});
|
||||
await clearTestBookingReferences();
|
||||
await clearTestBookings();
|
||||
});
|
||||
|
||||
describe("replaceBookingReferences", () => {
|
||||
|
||||
Reference in new issue
Block a user