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).
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)
Only the happy path is tested. The new branch — the actual reason this code changed — has no coverage, so a regression in it (like guarding only the leaf property instead of the whole chain) would ship undetected, as happened in this PR.
Objašnjenje (HR)
Testira se samo "happy path" scenarij. Nova grana — pravi razlog zbog kojeg se kôd mijenjao — nema pokrivenost testovima, pa bi regresija u njoj (npr. provjera samo krajnjeg svojstva umjesto cijelog lanca) prošla neopaženo, što se i dogodilo u ovom PR-u.
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.