Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Share repeated test fixtures and setup helpers at the nearest common scope
Reuse duplicated fixture data or equivalent mock setup through a shared local helper. Extract a separate module when multiple files need it, and keep mutable test state isolated.
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)
When repeated mock setup has the same intent, reuse the existing helper rather than reimplementing it in another describe block. Move it to the nearest common scope. Shared fixtures should have a source-derived type. A helper factory should create fresh mutable data and mocks per invocation so reuse does not introduce cross-test state.
Bilješke (HR)
Kad ponovljeno postavljanje mockova ima istu namjeru, upotrijebi postojeći helper umjesto nove implementacije u drugom describe bloku. Premjesti ga u najbliži zajednički doseg. Dijeljeni fixture treba imati tip izveden iz izvora. Tvornička funkcija treba pri svakom pozivu stvoriti nove promjenjive podatke i mockove kako ponovno korištenje ne bi povezalo stanje testova.
Exceptions / Tradeoffs (EN)
Keep fixtures and helpers local when only one file needs them. Do not force one helper onto genuinely different scenarios or share a mutable mock instance across isolated tests. Small repetitions without a common concept may remain inline.
Iznimke / Tradeoffi (HR)
Ostavi fixtureove i helpere lokalno kad ih treba samo jedna datoteka. Nemoj nametnuti jedan helper različitim scenarijima ni dijeliti promjenjivu mock instancu među izoliranim testovima. Male ponovljene dijelove bez zajedničkog koncepta možeš ostaviti na mjestu.