33## 🎯 Kluczowe ustawienia
44
55### ` environment: "jsdom" `
6+
67** Co to robi:** Symuluje środowisko przeglądarki (DOM, window, document)
78** Dlaczego:** Twoje komponenty React potrzebują DOM
89** Alternatywa:** ` happy-dom ` (szybszy, ale mniej kompatybilny) - zostań przy jsdom
910
1011### ` globals: true `
12+
1113** Co to robi:** Możesz pisać ` describe ` , ` it ` , ` expect ` bez importów
1214** Przykład:**
15+
1316``` typescript
1417// Bez globals:
15- import { describe , it , expect } from ' vitest' ;
18+ import { describe , it , expect } from " vitest" ;
1619
1720// Z globals (prostsze!):
18- describe (' MyComponent' , () => {
19- it (' should render' , () => {
21+ describe (" MyComponent" , () => {
22+ it (" should render" , () => {
2023 expect (true ).toBe (true );
2124 });
2225});
2326```
2427
2528### ` setupFiles: ["./src/test/setup-tests.ts"] `
29+
2630** Co to robi:** Uruchamia ten plik przed wszystkimi testami
2731** Co jest w środku:**
32+
2833- ` @testing-library/jest-dom ` (matchery jak ` toBeInTheDocument() ` )
2934- Mocki Next.js (` useRouter ` , ` usePathname ` , itp.)
3035- Mocki next-intl, next-themes
3136- MSW (Mock Service Worker) setup
3237
3338### ` css: true `
39+
3440** Co to robi:** Nie failuje gdy importujesz CSS w komponentach
3541** Przykład:**
42+
3643``` typescript
3744// Bez css: true → ERROR
38- import ' ./Button.css' ;
45+ import " ./Button.css" ;
3946
4047// Z css: true → OK ✅
41- import ' ./Button.css' ;
48+ import " ./Button.css" ;
4249```
4350
4451## 📊 Coverage (Pokrycie kodu)
4552
4653### ` reporter: ["text", "html"] `
54+
4755** Co to robi:**
56+
4857- ` text ` - pokazuje wyniki w terminalu
4958- ` html ` - generuje stronę HTML w ` coverage/index.html `
5059
5160** Usunięte:** ` json ` i ` lcov ` (niepotrzebne bez CI/CD)
5261
5362### ` thresholds: 60 `
63+
5464** Co to robi:** Minimalny % pokrycia kodu
5565** Zmienione z 70% na 60%** - łatwiej na początek
5666
5767** 4 typy pokrycia:**
68+
5869- ** lines:** % linii kodu które zostały uruchomione
5970- ** functions:** % funkcji które zostały wywołane
6071- ** branches:** % ścieżek (if/else) które zostały przetestowane
6172- ** statements:** % instrukcji które zostały wykonane
6273
6374** Co się stanie jak spadnie poniżej 60%?**
75+
6476``` bash
6577npm run test:coverage
6678# ERROR: Coverage threshold not met!
6779```
6880
6981### ` exclude ` (w coverage)
82+
7083** Co to robi:** Te pliki nie liczą się do pokrycia
7184** Co wykluczamy:**
85+
7286- ` src/test/ ` - same testy
7387- ` **/*.config.* ` - pliki konfiguracyjne (vitest.config.ts, itp.)
7488- ` src/db/database.types.ts ` - auto-generowane przez Supabase
@@ -77,19 +91,22 @@ npm run test:coverage
7791## 🎨 Aliasy ścieżek
7892
7993### ` alias: { "@": "./src" } `
94+
8095** Co to robi:** Możesz pisać ` @/components ` zamiast ` ../../../components `
8196** Przykład:**
97+
8298``` typescript
8399// Zamiast:
84- import { Button } from ' ../../../components/ui/Button' ;
100+ import { Button } from " ../../../components/ui/Button" ;
85101
86102// Piszesz:
87- import { Button } from ' @/components/ui/Button' ;
103+ import { Button } from " @/components/ui/Button" ;
88104```
89105
90106## 🚀 Jak używać
91107
92108### Podstawowe komendy
109+
93110``` bash
94111# Watch mode (rekomendowane podczas developmentu)
95112npm run test
@@ -106,6 +123,7 @@ npm run test:coverage
106123```
107124
108125### Pierwszy test
126+
109127``` typescript
110128// src/components/Button.test.tsx
111129import { render , screen } from ' @testing-library/react' ;
@@ -120,6 +138,7 @@ describe('Button', () => {
120138```
121139
122140### Uruchom:
141+
123142``` bash
124143npm run test
125144# Vitest automatycznie znajdzie *.test.tsx
@@ -128,22 +147,28 @@ npm run test
128147## 💡 Tipsy dla początkujących
129148
130149### 1. Używaj Watch Mode
150+
131151``` bash
132152npm run test
133153```
154+
134155Vitest automatycznie uruchomi testy gdy zapiszesz plik!
135156
136157### 2. Używaj UI Mode do debugowania
158+
137159``` bash
138160npm run test:ui
139161```
162+
140163Zobaczysz GUI z:
164+
141165- Listą wszystkich testów
142166- Wynikami w czasie rzeczywistym
143167- Stack traces
144168- Console logi
145169
146170### 3. Filtruj testy podczas developmentu
171+
147172``` bash
148173# Tylko testy z "Button" w nazwie
149174npm run test -- Button
@@ -153,9 +178,11 @@ npm run test -- src/components/Button.test.tsx
153178```
154179
155180### 4. Nie martw się o coverage na początku
181+
156182Zacznij od pisania testów, coverage przyjdzie z czasem.
157183
158184Kiedy sprawdzać coverage:
185+
159186- Przed mergem do main
160187- Co jakiś czas, żeby zobaczyć progress
161188
@@ -168,6 +195,7 @@ npm run test:coverage
168195## 🎓 Dobre praktyki
169196
170197### 1. Jeden plik testowy na komponent
198+
171199```
172200src/
173201 components/
@@ -176,38 +204,42 @@ src/
176204```
177205
178206### 2. Grupuj testy w describe
207+
179208``` typescript
180- describe (' Button' , () => {
181- describe (' when disabled' , () => {
182- it (' should not call onClick' , () => {
209+ describe (" Button" , () => {
210+ describe (" when disabled" , () => {
211+ it (" should not call onClick" , () => {
183212 // test
184213 });
185214 });
186215
187- describe (' when enabled' , () => {
188- it (' should call onClick' , () => {
216+ describe (" when enabled" , () => {
217+ it (" should call onClick" , () => {
189218 // test
190219 });
191220 });
192221});
193222```
194223
195224### 3. Używaj opisowych nazw testów
225+
196226``` typescript
197227// ❌ Źle
198- it (' works' , () => {});
228+ it (" works" , () => {});
199229
200230// ✅ Dobrze
201- it (' should call onClick when button is clicked' , () => {});
231+ it (" should call onClick when button is clicked" , () => {});
202232```
203233
204234### 4. Najpierw funkcjonalność, potem coverage
235+
205236Nie pisz testów tylko po to żeby mieć 100% coverage.
206237Pisz testy które testują ** zachowanie** Twojej aplikacji.
207238
208239## ⚙️ Kiedy zmienić ustawienia
209240
210241### Masz już stabilne testy i chcesz wyższych standardów?
242+
211243``` typescript
212244thresholds : {
213245 lines : 80 ,
@@ -218,15 +250,19 @@ thresholds: {
218250```
219251
220252### Potrzebujesz LCOV dla CI/CD?
253+
221254``` typescript
222255reporter : [" text" , " html" , " lcov" ],
223256```
257+
224258LCOV używa się do integracji z narzędziami jak Codecov.
225259
226260### Chcesz testować bez DOM (czyste funkcje)?
261+
227262``` typescript
228263environment : " node" , // Zamiast "jsdom"
229264```
265+
230266Szybsze, ale nie zadziała dla komponentów React!
231267
232268## ❓ FAQ
@@ -268,4 +304,3 @@ A: Nie! 60-80% to dobry cel. 100% często oznacza testowanie implementacji zamia
268304### Wszystko inne jest OK! ✅
269305
270306Config był już dobry, tylko lekko zoptymalizowany dla początkującego.
271-
0 commit comments