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).
Extract repeated test fixtures into a shared constants module
When the same default mock/fixture object is duplicated across multiple test files, move it to a shared test-constants module instead of redeclaring it.
Bad example
| 1 | // user-card.test.tsx |
| 2 | const defaultUser = { |
| 3 | id: 0, |
| 4 | name: '', |
| 5 | email: '', |
| 6 | isAdmin: false, |
| 7 | }; |
| 8 |
|
| 9 | // user-menu.test.tsx |
| 10 | const defaultUser = { |
| 11 | id: 0, |
| 12 | name: '', |
| 13 | email: '', |
| 14 | isAdmin: false, |
| 15 | }; |
Explanation (EN)
The same fixture object is copy-pasted into several test files. When the underlying shape changes, every copy must be updated by hand, and copies drift out of sync.
Objašnjenje (HR)
Isti fixture objekt kopiran je u vise test datoteka. Kada se promijeni oblik podatka, svaku kopiju treba rucno azurirati i kopije se s vremenom razidu.
Good example
| 1 | // test/fixtures.ts |
| 2 | export const defaultUser = { |
| 3 | id: 0, |
| 4 | name: '', |
| 5 | email: '', |
| 6 | isAdmin: false, |
| 7 | }; |
| 8 |
|
| 9 | // user-card.test.tsx |
| 10 | import { defaultUser } from 'test/fixtures'; |
| 11 |
|
| 12 | // user-menu.test.tsx |
| 13 | import { defaultUser } from 'test/fixtures'; |
Explanation (EN)
The fixture lives in one shared module and is imported wherever needed, so there is a single source of truth to update.
Objašnjenje (HR)
Fixture zivi u jednom dijeljenom modulu i uvozi se gdje god treba, pa postoji jedan izvor istine za azuriranje.
Notes (EN)
Shared fixtures pair well with a typed shape (see the rule on typing shared test fixtures) so all consumers benefit from the same type.
Bilješke (HR)
Dijeljeni fixtureovi dobro idu uz tipizirani oblik (vidi pravilo o tipiziranju dijeljenih test fixtureova) kako bi svi potrosaci dobili isti tip.
Exceptions / Tradeoffs (EN)
If a fixture is genuinely used in only one test file, keep it local; only extract once it is duplicated or clearly will be.
Iznimke / Tradeoffi (HR)
Ako se fixture stvarno koristi u samo jednoj test datoteci, ostavi ga lokalnim; izdvoji tek kad se duplicira ili je jasno da hoce.