Skip to content

Commit

Permalink
refactor: Moving from jest to vitest (calcom#9035)
Browse files Browse the repository at this point in the history
* Moving to vitest

* Rearranging test

* Fixing prettier linting

* Reverting launch.json

* Adjustments

* Merged with main and regenerated lockfile

* Fixing tests for API

* Yarn updated, docs is gone

---------

Co-authored-by: Peer Richelsen <[email protected]>
Co-authored-by: zomars <[email protected]>
Co-authored-by: Alex van Andel <[email protected]>
  • Loading branch information
4 people authored May 24, 2023
1 parent ae471ce commit 734382b
Show file tree
Hide file tree
Showing 37 changed files with 1,443 additions and 2,092 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public

*.lock
*.log
*.test.ts

.gitignore
.npmignore
Expand Down
1 change: 1 addition & 0 deletions apps/web/.prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
public/embed
*.test.ts
2 changes: 0 additions & 2 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@
"@types/stripe": "^8.0.417",
"@types/uuid": "8.3.1",
"autoprefixer": "^10.4.12",
"babel-jest": "^28.1.0",
"copy-webpack-plugin": "^11.0.0",
"detect-port": "^1.3.0",
"env-cmd": "^10.1.0",
Expand All @@ -165,7 +164,6 @@
"msw": "^0.42.3",
"postcss": "^8.4.18",
"tailwindcss": "^3.3.1",
"ts-jest": "^28.0.8",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
},
Expand Down
2 changes: 1 addition & 1 deletion apps/web/pages/[user].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default function User(props: inferSSRProps<typeof getServerSideProps> & E
delete query.user; // So it doesn't display in the Link (and make tests fail)
const nameOrUsername = user.name || user.username || "";

/*
/*
const telemetry = useTelemetry();
useEffect(() => {
if (top !== window) {
Expand Down
5 changes: 3 additions & 2 deletions apps/web/test/lib/checkBookingLimits.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { describe, expect, it } from "vitest";
import prismaMock from "../../../../tests/libs/__mocks__/prisma";

import dayjs from "@calcom/dayjs";
import { validateIntervalLimitOrder } from "@calcom/lib";
import { checkBookingLimits, checkBookingLimit } from "@calcom/lib/server";
import type { IntervalLimit } from "@calcom/types/Calendar";

import { prismaMock } from "../../../../tests/config/singleton";

type Mockdata = {
id: number;
startDate: Date;
Expand Down
4 changes: 3 additions & 1 deletion apps/web/test/lib/checkDurationLimits.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { describe, expect, it } from "vitest";
import prismaMock from "../../../../tests/libs/__mocks__/prisma";

import dayjs from "@calcom/dayjs";
import { validateIntervalLimitOrder } from "@calcom/lib";
import { checkDurationLimit, checkDurationLimits } from "@calcom/lib/server";

import { prismaMock } from "../../../../tests/config/singleton";

type MockData = {
id: number;
Expand Down
42 changes: 21 additions & 21 deletions apps/web/test/lib/getAggregateWorkingHours.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, it } from "@jest/globals";
import MockDate from "mockdate";
import { expect, it } from "vitest";

import { getAggregateWorkingHours } from "@calcom/core/getAggregateWorkingHours";

Expand Down Expand Up @@ -28,35 +28,35 @@ const HAWAII_AND_NEWYORK_TEAM = [
/* TODO: Make this test more "professional" */
it("Sydney and Shiraz can live in harmony 🙏", async () => {
expect(getAggregateWorkingHours(HAWAII_AND_NEWYORK_TEAM, "COLLECTIVE")).toMatchInlineSnapshot(`
Array [
Object {
"days": Array [
[
{
"days": [
3,
4,
5,
],
"endTime": 360,
"startTime": 780,
},
Object {
"days": Array [
{
"days": [
6,
],
"endTime": 180,
"startTime": 0,
"userId": 2,
},
Object {
"days": Array [
{
"days": [
2,
3,
4,
],
"endTime": 1260,
"startTime": 780,
},
Object {
"days": Array [
{
"days": [
5,
],
"endTime": 1260,
Expand All @@ -66,9 +66,9 @@ it("Sydney and Shiraz can live in harmony 🙏", async () => {
`);

expect(getAggregateWorkingHours(HAWAII_AND_NEWYORK_TEAM, "ROUND_ROBIN")).toMatchInlineSnapshot(`
Array [
Object {
"days": Array [
[
{
"days": [
1,
2,
3,
Expand All @@ -79,8 +79,8 @@ it("Sydney and Shiraz can live in harmony 🙏", async () => {
"startTime": 780,
"userId": 1,
},
Object {
"days": Array [
{
"days": [
3,
4,
5,
Expand All @@ -89,16 +89,16 @@ it("Sydney and Shiraz can live in harmony 🙏", async () => {
"startTime": 0,
"userId": 1,
},
Object {
"days": Array [
{
"days": [
6,
],
"endTime": 180,
"startTime": 0,
"userId": 2,
},
Object {
"days": Array [
{
"days": [
2,
3,
4,
Expand All @@ -107,8 +107,8 @@ it("Sydney and Shiraz can live in harmony 🙏", async () => {
"startTime": 780,
"userId": 3,
},
Object {
"days": Array [
{
"days": [
5,
],
"endTime": 1439,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/test/lib/getAvailabilityFromSchedule.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, it } from "@jest/globals";
import type { Availability } from "@prisma/client";
import MockDate from "mockdate";
import { expect, it } from "vitest";

import dayjs from "@calcom/dayjs";
import { getAvailabilityFromSchedule } from "@calcom/lib/availability";
Expand Down
73 changes: 32 additions & 41 deletions apps/web/test/lib/getSchedule.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/**
* !: Stops the `jose` dependency from bundling the browser version and breaking tests
* @jest-environment node
*/
import type {
EventType as PrismaEventType,
User as PrismaUser,
Booking as PrismaBooking,
App as PrismaApp,
} from "@prisma/client";

import CalendarManagerMock from "../../../../tests/libs/__mocks__/CalendarManager";
import prismaMock from "../../../../tests/libs/__mocks__/prisma";

import { diff } from "jest-diff";
import { v4 as uuidv4 } from "uuid";
import { describe, expect, vi, beforeEach, afterEach, test } from "vitest";

import logger from "@calcom/lib/logger";
import prisma from "@calcom/prisma";
Expand All @@ -18,14 +19,13 @@ import type { BookingStatus } from "@calcom/prisma/enums";
import type { Slot } from "@calcom/trpc/server/routers/viewer/slots/types";
import { getSchedule } from "@calcom/trpc/server/routers/viewer/slots/util";

import { prismaMock, CalendarManagerMock } from "../../../../tests/config/singleton";

// TODO: Mock properly
prismaMock.eventType.findUnique.mockResolvedValue(null);
prismaMock.user.findMany.mockResolvedValue([]);

jest.mock("@calcom/lib/constants", () => ({
vi.mock("@calcom/lib/constants", () => ({
IS_PRODUCTION: true,
WEBAPP_URL: "http://localhost:3000"
}));

declare global {
Expand Down Expand Up @@ -233,6 +233,13 @@ describe("getSchedule", () => {
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const { dateString: plus2DateString } = getDate({ dateIncrement: 2 });

CalendarManagerMock.getBusyCalendarTimes.mockResolvedValue([
{
start: `${plus2DateString}T04:45:00.000Z`,
end: `${plus2DateString}T23:00:00.000Z`,
},
]);

const scenarioData = {
hosts: [],
eventTypes: [
Expand Down Expand Up @@ -261,12 +268,6 @@ describe("getSchedule", () => {
// An event with one accepted booking
createBookingScenario(scenarioData);

addBusyTimesInGoogleCalendar([
{
start: `${plus2DateString}T04:45:00.000Z`,
end: `${plus2DateString}T23:00:00.000Z`,
},
]);
const scheduleForDayWithAGoogleCalendarBooking = await getSchedule({
eventTypeId: 1,
eventTypeSlug: "",
Expand Down Expand Up @@ -492,7 +493,7 @@ describe("getSchedule", () => {

// FIXME: Fix minimumBookingNotice is respected test
test.skip("minimumBookingNotice is respected", async () => {
jest.useFakeTimers().setSystemTime(
vi.useFakeTimers().setSystemTime(
(() => {
const today = new Date();
// Beginning of the day in current timezone of the system
Expand Down Expand Up @@ -571,13 +572,20 @@ describe("getSchedule", () => {
dateString: todayDateString,
}
);
jest.useRealTimers();
vi.useRealTimers();
});

test("afterBuffer and beforeBuffer tests - Non Cal Busy Time", async () => {
const { dateString: plus2DateString } = getDate({ dateIncrement: 2 });
const { dateString: plus3DateString } = getDate({ dateIncrement: 3 });

CalendarManagerMock.getBusyCalendarTimes.mockResolvedValue([
{
start: `${plus3DateString}T04:00:00.000Z`,
end: `${plus3DateString}T05:59:59.000Z`,
},
]);

const scenarioData = {
eventTypes: [
{
Expand Down Expand Up @@ -607,13 +615,6 @@ describe("getSchedule", () => {

createBookingScenario(scenarioData);

addBusyTimesInGoogleCalendar([
{
start: `${plus3DateString}T04:00:00.000Z`,
end: `${plus3DateString}T05:59:59.000Z`,
},
]);

const scheduleForEventOnADayWithNonCalBooking = await getSchedule({
eventTypeId: 1,
eventTypeSlug: "",
Expand Down Expand Up @@ -641,6 +642,13 @@ describe("getSchedule", () => {
const { dateString: plus2DateString } = getDate({ dateIncrement: 2 });
const { dateString: plus3DateString } = getDate({ dateIncrement: 3 });

CalendarManagerMock.getBusyCalendarTimes.mockResolvedValue([
{
start: `${plus3DateString}T04:00:00.000Z`,
end: `${plus3DateString}T05:59:59.000Z`,
},
]);

const scenarioData = {
eventTypes: [
{
Expand Down Expand Up @@ -679,13 +687,6 @@ describe("getSchedule", () => {

createBookingScenario(scenarioData);

addBusyTimesInGoogleCalendar([
{
start: `${plus3DateString}T04:00:00.000Z`,
end: `${plus3DateString}T05:59:59.000Z`,
},
]);

const scheduleForEventOnADayWithCalBooking = await getSchedule({
eventTypeId: 1,
eventTypeSlug: "",
Expand All @@ -712,6 +713,8 @@ describe("getSchedule", () => {
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
const { dateString: plus2DateString } = getDate({ dateIncrement: 2 });

CalendarManagerMock.getBusyCalendarTimes.mockResolvedValue([]);

const scenarioData = {
eventTypes: [
{
Expand Down Expand Up @@ -1380,15 +1383,3 @@ const getDate = (param: { dateIncrement?: number; monthIncrement?: number; yearI
dateString: `${year}-${month}-${date}`,
};
};

/**
* TODO: Improve this to validate the arguments passed to getBusyCalendarTimes if they are valid or not.
*/
function addBusyTimesInGoogleCalendar(
busy: {
start: string;
end: string;
}[]
) {
CalendarManagerMock.getBusyCalendarTimes.mockResolvedValue(busy);
}
16 changes: 8 additions & 8 deletions apps/web/test/lib/getTimezone.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, it } from "@jest/globals";
import { expect, it } from "vitest";

import { filterByCities, addCitiesToDropdown, handleOptionLabel } from "@calcom/lib/timezone";

Expand Down Expand Up @@ -34,25 +34,25 @@ const option = {
};

it("should return empty array for an empty string", () => {
expect(filterByCities("", cityData)).toMatchInlineSnapshot(`Array []`);
expect(filterByCities("", cityData)).toMatchInlineSnapshot(`[]`);
});

it("should filter cities for a valid city name", () => {
expect(filterByCities("San Francisco", cityData)).toMatchInlineSnapshot(`
Array [
Object {
[
{
"city": "San Francisco",
"timezone": "America/Argentina/Cordoba",
},
Object {
{
"city": "San Francisco de Macoris",
"timezone": "America/Santo_Domingo",
},
Object {
{
"city": "San Francisco Gotera",
"timezone": "America/El_Salvador",
},
Object {
{
"city": "San Francisco",
"timezone": "America/Los_Angeles",
},
Expand All @@ -62,7 +62,7 @@ it("should filter cities for a valid city name", () => {

it("should return appropriate timezone(s) for a given city name array", () => {
expect(addCitiesToDropdown(cityData)).toMatchInlineSnapshot(`
Object {
{
"America/Argentina/Cordoba": "San Francisco",
"America/El_Salvador": "San Francisco Gotera",
"America/Los_Angeles": "San Francisco",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/test/lib/getWorkingHours.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, it } from "@jest/globals";
import MockDate from "mockdate";
import { expect, it } from "vitest";

import dayjs from "@calcom/dayjs";
import { getWorkingHours } from "@calcom/lib/availability";
Expand Down
Loading

0 comments on commit 734382b

Please sign in to comment.