Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Add test coverage for new conditional branches, including the exact edge-case shapes they fix
A new branch added to fix a bug should ship with tests for the specific data shapes (missing key vs. empty object) that caused the bug.
Bad example
| 1 | function getImageUrl(asset: Asset) { |
| 2 | if (!asset.renditions?.highRes?.uri) { |
| 3 | return fallbackUrl(asset); |
| 4 | } |
| 5 | return asset.renditions.highRes.uri; |
| 6 | } |
| 7 |
|
| 8 | // tests/getImageUrl.test.ts |
| 9 | it('returns the highRes uri', () => { |
| 10 | expect(getImageUrl(assetWithHighRes)).toBe(assetWithHighRes.renditions.highRes.uri); |
| 11 | }); |
| 12 | // no test for a missing/empty `renditions` |
Explanation (EN)
Happy-path tests do not protect the new guard or fallback from regression.
Objašnjenje (HR)
Test uspješnog puta ne štiti novu zaštitnu provjeru ili fallback od regresije.
Good example
| 1 | it('falls back when renditions is missing entirely', () => { |
| 2 | expect(getImageUrl({ ...baseAsset, renditions: undefined })).toBe(fallbackUrl(baseAsset)); |
| 3 | }); |
| 4 |
|
| 5 | it('falls back when renditions is an empty object', () => { |
| 6 | expect(getImageUrl({ ...baseAsset, renditions: {} })).toBe(fallbackUrl(baseAsset)); |
| 7 | }); |
| 8 |
|
| 9 | it('returns the highRes uri when present', () => { |
| 10 | expect(getImageUrl(baseAssetWithHighRes)).toBe(baseAssetWithHighRes.renditions.highRes.uri); |
| 11 | }); |
Explanation (EN)
Testing both the missing-key and empty-object shapes (not just the one mentioned in the bug report) covers the actual variety of malformed data seen in production and would have caught the incomplete guard before it shipped.
Objašnjenje (HR)
Testiranjem i slučaja nedostajućeg ključa i praznog objekta (ne samo onog spomenutog u prijavi bug-a) pokriva se stvarna raznolikost neispravnih podataka viđenih u produkciji, što bi otkrilo nepotpunu provjeru prije nego što je isporučena.
Notes (EN)
For a bug-fix branch, reproduce the triggering input and cover relevant distinctions such as absent parents, empty objects and valid data.
Exceptions / Tradeoffs (EN)
A behaviorally equivalent existing test can be extended; avoid implementation-mirroring assertions that do not reproduce the bug.