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).
Keep edge-case inputs consistent across sibling tests
When one test uses whitespace input to verify trimming, use the same kind of input in parallel tests of the same fallback logic.
Bad example
| 1 | it('trims the name before using it', () => { |
| 2 | const result = buildLabel(createItem({ name: ' Widget ' })); |
| 3 | expect(result.text).toBe('Widget'); |
| 4 | }); |
| 5 |
|
| 6 | it('falls back to the short name when the name is blank', () => { |
| 7 | // empty string, not whitespace — doesn't confirm trimming happens on this path too |
| 8 | const result = buildLabel(createItem({ name: '', shortName: 'Widget' })); |
| 9 | expect(result.text).toBe('Widget'); |
| 10 | }); |
Explanation (EN)
The first test proves trimming works, but the fallback test uses an empty string instead of whitespace, so it never confirms the fallback path also trims — a whitespace-only name could slip through untrimmed and the suite wouldn't notice.
Objašnjenje (HR)
Prvi test dokazuje da trimanje radi, ali test za fallback koristi prazan string umjesto razmaka, pa nikad ne provjeri triman li se i taj put — naziv koji sadrži samo razmake mogao bi proći netriman, a test suite to ne bi primijetio.
Good example
| 1 | it('trims the name before using it', () => { |
| 2 | const result = buildLabel(createItem({ name: ' Widget ' })); |
| 3 | expect(result.text).toBe('Widget'); |
| 4 | }); |
| 5 |
|
| 6 | it('falls back to the short name when the name is blank', () => { |
| 7 | const result = buildLabel(createItem({ name: ' ', shortName: 'Widget' })); |
| 8 | expect(result.text).toBe('Widget'); |
| 9 | }); |
Explanation (EN)
Using a whitespace-only string in the fallback test too means both tests confirm the same trimming behavior along their respective code paths, catching a regression either one could otherwise miss.
Objašnjenje (HR)
Korištenjem stringa koji sadrži samo razmake i u fallback testu, oba testa provjeravaju isto ponašanje trimanja na svojim putevima kroz kod, pa se hvata regresija koju bi svaki od njih zasebno mogao propustiti.