From 51199c18822af778f85cc9dade004b383289c9d1 Mon Sep 17 00:00:00 2001 From: Dhiren-Mhatre Date: Wed, 25 Dec 2024 01:19:54 +0530 Subject: [PATCH 1/2] test: migrate errorHandler tests from Jest to Vitest --- .env.example | 31 ------------------- jest.config.js | 3 +- src/components/AddOn/AddOn.spec.tsx | 1 - .../OrganizationDashboard.spec.tsx | 10 ++++-- ...Handler.test.tsx => errorHandler.spec.tsx} | 7 +++-- 5 files changed, 12 insertions(+), 40 deletions(-) delete mode 100644 .env.example rename src/utils/{errorHandler.test.tsx => errorHandler.spec.tsx} (96%) diff --git a/.env.example b/.env.example deleted file mode 100644 index de7cff92ec..0000000000 --- a/.env.example +++ /dev/null @@ -1,31 +0,0 @@ -# 👋 Welcome, we're glad you're setting up an installation of Talawa-admin. Copy this -# file to .env or set the variables in your local environment manually. - - -# Custom port number for the talawa-admin development server to run on. Default is 4321. - -PORT=4321 - -# Run Talawa-api locally in your system, and put its url into the same. - -REACT_APP_TALAWA_URL= - -# Do you want to setup and use "I'm not a robot" Checkbox (Google Recaptcha)? -# If no, leave blank, else write yes -# Example: REACT_APP_USE_RECAPTCHA=yes - -REACT_APP_USE_RECAPTCHA= - -# If you are using Google Recaptcha, i.e., REACT_APP_USE_RECAPTCHA=yes, read the following steps -# Get the google recaptcha site key from google recaptcha admin or https://www.google.com/recaptcha/admin/create -# from here for reCAPTCHA v2 and "I'm not a robot" Checkbox, and paste the key here. -# Note: In domains, fill localhost - -REACT_APP_RECAPTCHA_SITE_KEY= - -# has to be inserted in the env file to use plugins and other websocket based features. -REACT_APP_BACKEND_WEBSOCKET_URL=ws://localhost:4000/graphql/ - -# If you want to logs Compiletime and Runtime error , warning and info write YES or if u want to -# keep the console clean leave it blank -ALLOW_LOGS= diff --git a/jest.config.js b/jest.config.js index 75e0cc5b4d..425ad2aa76 100644 --- a/jest.config.js +++ b/jest.config.js @@ -10,8 +10,7 @@ export default { '!**/index.{js,ts}', '!**/*.d.ts', '!src/test/**', - '!vitest.config.ts', - ], + '!vitest.config.ts',], // setupFiles: ['react-app-polyfill/jsdom'], setupFiles: ['whatwg-fetch'], setupFilesAfterEnv: ['/src/setupTests.ts'], diff --git a/src/components/AddOn/AddOn.spec.tsx b/src/components/AddOn/AddOn.spec.tsx index f0173c53fe..a2e45157e4 100644 --- a/src/components/AddOn/AddOn.spec.tsx +++ b/src/components/AddOn/AddOn.spec.tsx @@ -13,7 +13,6 @@ import { I18nextProvider } from 'react-i18next'; import '@testing-library/jest-dom'; import { describe, test, expect, vi } from 'vitest'; import { store } from 'state/store'; - import AddOn from './AddOn'; import i18nForTest from 'utils/i18nForTest'; import { StaticMockLink } from 'utils/StaticMockLink'; diff --git a/src/screens/OrganizationDashboard/OrganizationDashboard.spec.tsx b/src/screens/OrganizationDashboard/OrganizationDashboard.spec.tsx index b7b4e05a37..6fd27ce429 100644 --- a/src/screens/OrganizationDashboard/OrganizationDashboard.spec.tsx +++ b/src/screens/OrganizationDashboard/OrganizationDashboard.spec.tsx @@ -147,6 +147,11 @@ describe('Testing Organization Dashboard Screen', () => { vi.mocked(useParams).mockReturnValue({ orgId: 'orgId' }); renderOrganizationDashboard(link1); + // First wait for the dashboard to fully load + await waitFor(() => { + expect(screen.getByText(t.upcomingEvents)).toBeInTheDocument(); + }); + // Dashboard cards const membersBtn = await screen.findByText(t.members); expect(membersBtn).toBeInTheDocument(); @@ -155,9 +160,8 @@ describe('Testing Organization Dashboard Screen', () => { expect(screen.getByText(t.events)).toBeInTheDocument(); expect(screen.getByText(t.blockedUsers)).toBeInTheDocument(); - // Upcoming events - expect(screen.getByText(t.upcomingEvents)).toBeInTheDocument(); - expect(screen.getByText('Event 1')).toBeInTheDocument(); + // Upcoming events - Use a more flexible matcher + expect(screen.getByText(/Event 1/i, { exact: false })).toBeInTheDocument(); // Latest posts expect(screen.getByText(t.latestPosts)).toBeInTheDocument(); diff --git a/src/utils/errorHandler.test.tsx b/src/utils/errorHandler.spec.tsx similarity index 96% rename from src/utils/errorHandler.test.tsx rename to src/utils/errorHandler.spec.tsx index f229e8d5fa..96c35e2a7f 100644 --- a/src/utils/errorHandler.test.tsx +++ b/src/utils/errorHandler.spec.tsx @@ -2,10 +2,11 @@ type TFunction = (key: string, options?: Record) => string; import { errorHandler } from './errorHandler'; import { toast } from 'react-toastify'; +import { describe, it, expect, vi } from 'vitest'; -jest.mock('react-toastify', () => ({ +vi.mock('react-toastify', () => ({ toast: { - error: jest.fn(), + error: vi.fn(), }, })); @@ -22,7 +23,7 @@ describe('Test if errorHandler is working properly', () => { }; beforeEach(() => { - jest.clearAllMocks(); + vi.clearAllMocks(); }); it('should call toast.error with the correct message if error message is "Failed to fetch"', async () => { From de80a7ae6a13628dbb2d8f074fea76a184674ce6 Mon Sep 17 00:00:00 2001 From: Dhiren-Mhatre Date: Wed, 25 Dec 2024 02:02:59 +0530 Subject: [PATCH 2/2] solved linting error --- jest.config.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jest.config.js b/jest.config.js index 425ad2aa76..75e0cc5b4d 100644 --- a/jest.config.js +++ b/jest.config.js @@ -10,7 +10,8 @@ export default { '!**/index.{js,ts}', '!**/*.d.ts', '!src/test/**', - '!vitest.config.ts',], + '!vitest.config.ts', + ], // setupFiles: ['react-app-polyfill/jsdom'], setupFiles: ['whatwg-fetch'], setupFilesAfterEnv: ['/src/setupTests.ts'],