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).
Replace an assertion that cannot fail on its own with one for an uncovered branch
An assertion implied by an exact-match assertion earlier in the same test can never be the thing that goes red, so it costs lines and runtime while adding no signal. Spend those lines on the branches that currently have no coverage at all.
Bad example
| 1 | it("serves the frozen url", () => { |
| 2 | expect(buildUrl()).toBe(EXPECTED); |
| 3 | }); |
| 4 |
|
| 5 | it("does not resize or re-encode", () => { |
| 6 | expect(buildUrl()).not.toContain("webp"); // implied by toBe above |
| 7 | expect(buildUrl()).not.toContain("width="); |
| 8 | }); |
| 9 | // untested: the empty-avatar and out-of-range-rank branches |
Explanation (EN)
Anything that fails these two already failed the exact match, and the helper is executed twice more for nothing.
Objašnjenje (HR)
Sve sto obori ova dva provjeravanja vec je oborilo tocno podudaranje, a pomocna funkcija se bez razloga izvodi jos dva puta.
Good example
| 1 | it("serves the frozen url", () => { |
| 2 | expect(buildUrl()).toBe(EXPECTED); |
| 3 | }); |
| 4 |
|
| 5 | it("falls back to the placeholder when there is no avatar", () => { |
| 6 | expect(buildUrl({ avatar: "" })).toContain("empty-placeholder"); |
| 7 | }); |
| 8 |
|
| 9 | it("uses the default placeholder outside the ranked positions", () => { |
| 10 | expect(buildUrl({ rank: 4 })).toContain("[default]"); |
| 11 | }); |
Explanation (EN)
The same number of lines now covers the two real branches of the helper instead of restating one assertion.
Objašnjenje (HR)
Isti broj linija sada pokriva dvije stvarne grane pomocne funkcije umjesto da ponavlja jednu tvrdnju.
Notes (EN)
Before adding an assertion, ask which failure it catches that the ones above it do not. If there is none, it is documentation, and a comment is cheaper.
Bilješke (HR)
Prije dodavanja tvrdnje pitaj se koji kvar hvata, a da ga tvrdnje iznad ne hvataju. Ako takvog nema, to je dokumentacija, a komentar je jeftiniji.