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).
Run contract tests through the same serializer production uses
A test that rebuilds the serialization options by hand instead of calling the shared helper is blind to the one thing every endpoint actually goes through. It keeps passing while the real wire format changes, which is the exact failure it exists to prevent.
Bad example
| 1 | // production: serialize() uses { excludeExtraneousValues: true, exposeUnsetFields: false } |
| 2 | const output = plainToInstance(ResponseDto, row, { |
| 3 | excludeExtraneousValues: true, // second option missing |
| 4 | }); |
| 5 | expect(output.avatar).toBe(EXPECTED_URL); |
Explanation (EN)
Both option sets happen to agree for this fixture, so the test passes today and would keep passing if the shared serializer changed.
Objašnjenje (HR)
Oba skupa opcija slucajno se slazu za ovaj primjer, pa test danas prolazi i nastavio bi prolaziti i da se dijeljeni serijalizator promijeni.
Good example
| 1 | import { serialize } from "@/shared/util/serialize"; |
| 2 |
|
| 3 | const output = serialize(ResponseDto, { rows: [row] }) as ResponseDto; |
| 4 | expect(output.rows[0].avatar).toBe(EXPECTED_URL); |
Explanation (EN)
The test exercises the real path, so any change to the shared serializer shows up as a failure here.
Objašnjenje (HR)
Test prolazi kroz stvarni put, pa se svaka promjena dijeljenog serijalizatora ovdje pojavi kao pad testa.
Notes (EN)
Ask what change should make this test red. If a plausible change to the production path leaves it green, the test is checking a copy rather than the thing.
Bilješke (HR)
Pitaj se koja bi promjena trebala oboriti ovaj test. Ako uvjerljiva promjena produkcijskog puta ostavi test zelenim, test provjerava kopiju, a ne stvarnu stvar.
Exceptions / Tradeoffs (EN)
If calling the real helper needs an ugly cast you truly want to avoid, mirror every option and add a comment naming the file they must track.
Iznimke / Tradeoffi (HR)
Ako poziv stvarnog pomocnika trazi ruzan cast koji stvarno zelis izbjeci, preslikaj sve opcije i dodaj komentar s imenom datoteke koju moraju pratiti.