Skip to content

Feat/vitest testing library msw#253

Closed
D0dii wants to merge 6 commits intomainfrom
feat/vitest-testing-library-msw
Closed

Feat/vitest testing library msw#253
D0dii wants to merge 6 commits intomainfrom
feat/vitest-testing-library-msw

Conversation

@D0dii
Copy link
Copy Markdown
Member

@D0dii D0dii commented Jul 24, 2025

Adding tests setup

  • vitest
  • react-testing-library
  • msw

Important

Introduces Vitest, React Testing Library, and MSW for testing with new test files, mock handlers, and configuration updates.

  • Testing Setup:
    • Introduces vitest, react-testing-library, and msw for testing.
    • Adds vitest.config.mts for Vitest configuration with jsdom environment and setup files.
  • Tests:
    • Adds plans-edit.test.tsx to test error handling in CreateNewPlanPage.
    • Adds plans.test.tsx to test error handling in PlanItem.
  • Mocks:
    • Adds handlers.ts and server.ts for MSW mock server setup.
    • Mock handlers simulate API errors for departments, registrations, and course loading.
  • Scripts:
    • Updates package.json scripts to include vitest commands for testing.
  • Misc:
    • Adds error toast in app-sidebar.tsx for registration fetch failures.
    • Adds setup.ts for test environment setup with global mocks and server lifecycle management.

This description was created by Ellipsis for 463a6dd. You can customize this summary. It will automatically update as commits are pushed.

Comment thread frontend/__tests__/tests.test.tsx Outdated
Comment thread frontend/__tests__/tests.test.tsx Outdated
@D0dii
Copy link
Copy Markdown
Member Author

D0dii commented Jul 29, 2025

I know it isn't the prettiest code but for now I'm tired and don't have energy for it

Copy link
Copy Markdown
Member

@Rei-x Rei-x left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spoczko testy dodi, raczej nie mam jakichś dużych uwag, są w większości bardzo poprawne i testują co mają testować - może brakuje mi jakichś testów logiki biznesowej albo happy patha, w większości przetestowałeś ekrany błędów

Comment on lines +21 to +28
vi.mock("@/hooks/use-share", () => ({
useShare: () => ({
isDialogOpen: false,
openDialog: vi.fn(),
closeDialog: vi.fn(),
setIsDialogOpen: vi.fn(),
}),
}));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nie trzeba chyba tego mockować? mogłeś to jakoś ograć kodem, staraj się mockować jak najmniej

beforeEach(() => {
localStorage.setItem(
"plansIds-v2",
JSON.stringify([{ id: "a5e9b042-9151-4719-9f1d-8ff6fb216900" }]),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wydzieliłbym to id do jakiejś zmiennej, żeby było wiadomo co go używa

localStorage.clear();
});

it("Should display error for faculties without backend", async () => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raczej bym ustawił i tak jakiś handler w msw, ale taki co zwróci 500, zamiast żadnego - jesteś wtedy explicit co testujesz

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

teraz zauważyłem, że są gdzieś w setupie - nie jestem tego fanem, bo (tak jak mi przed chwilą) ciężko zrozumieć co się właściwie dzieje - najlepiej ustawiaj request handlery w teście

Comment on lines +96 to +97
const btns = screen.getAllByRole("button");
await user.click(btns[0]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oj oj, to jest bardzo wrażliwy locator, ktoś doda drugi przycisk i nagle ten test pada


globalThis.ResizeObserver = ResizeObserver;

const takeRecordsMock = () => [];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

może jestem ślepy, ale gdzie to jest używane?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kurde nie pamiętam, ale coś mi sie wywalało bez obserwera

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

w sensie nie observer, tylko takeRecordsMock

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

o oki, już widzę

import { HttpResponse, http } from "msw";

export const handlers = [
http.get("http://localhost:3333/api/departments", () => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raczej bym używał zmiennej z env'a zamiast hardkodować localhosta


afterEach(() => {
localStorage.clear();
vi.clearAllMocks();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

możesz to wrzucić do configu vitesta - cleanMocks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ja akurat jestem fanem kolokacji testów czyli ten test bym wrzucił w @/app/plans/edit/[id]/page.client.test.tsx

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm okej w sumie spoko pomysł, ale nie lepiej jeszcze bardziej do: @/app/plans/edit/[id]/tests/page.client.test.tsx?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jak wolisz, ja zwykle nie robię dodatkowych folderów - i też nie spotkałem się jeszcze z takim podejściem jak ty opisałeś

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zazwyczaj albo ludzie robią pliki koło siebie albo moję kompletnie osobny folder tests koło src

Copy link
Copy Markdown
Member Author

@D0dii D0dii Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okej, po prostu to wciskanie testu obok stron mnie trochę gryzie, a co np. jeśliby zrobić feature based architekturę(wiem, że w planerze tego nie mamy) i mieć np. features/create-plan/tests to chyba by fajnie wyglądało

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noo jak lubisz, raczej ludzie robią pliki koło sieibe

@qamarq qamarq closed this Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants