Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Don't redefine mock handlers already provided by the default set
Use per-test mock overrides only to add or change behavior; rely on the shared default handlers otherwise.
Bad example
| 1 | // defaultHandlers already mocks GET /api/search-history -> [] |
| 2 | test('shows nav link', async ({ worker }) => { |
| 3 | worker.use( |
| 4 | http.get('https://example.com/api/search-history', () => HttpResponse.json([])), |
| 5 | ); |
| 6 | // ...assertions |
| 7 | }); |
Explanation (EN)
The handler duplicates one already registered in the default set, adding noise and a second place to keep the same mock in sync.
Objašnjenje (HR)
Handler duplicira onaj koji je vec registriran u zadanom skupu, dodajuci sum i drugo mjesto na kojem treba odrzavati isti mock sinkroniziranim.
Good example
| 1 | // defaultHandlers already mocks GET /api/search-history -> [] |
| 2 | test('shows nav link', async () => { |
| 3 | // no worker.use(): the default handler is enough |
| 4 | // ...assertions |
| 5 | }); |
| 6 |
|
| 7 | test('handles empty results', async ({ worker }) => { |
| 8 | worker.use( |
| 9 | http.get('https://example.com/api/search-history', () => HttpResponse.json([{ id: 1 }])), |
| 10 | ); // override only when behavior must differ |
| 11 | }); |
Explanation (EN)
Tests lean on the shared defaults and reach for worker.use() only when they genuinely need different behavior, keeping each test focused on its delta.
Objašnjenje (HR)
Testovi se oslanjaju na zajednicke zadane vrijednosti i posezu za worker.use() samo kada stvarno trebaju drugacije ponasanje, drzeci svaki test usredotocen na svoju razliku.
Exceptions / Tradeoffs (EN)
If no default handler exists for the endpoint a test hits, define it (ideally promote it into the shared defaults rather than per-test).
Iznimke / Tradeoffi (HR)
Ako ne postoji zadani handler za endpoint koji test poziva, definiraj ga (po mogucnosti ga promakni u zajednicke zadane vrijednosti umjesto po testu).