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-only formatting in the test file, not the shared serializer
When a security-sensitive serialization function's output is hard to read in snapshots, add a pretty-printing wrapper in the test file — don't change the shared function's behavior between test and production.
Bad example
| 1 | export const safeJsonLd = import.meta.env.MODE === 'test' |
| 2 | ? (obj) => prettierFormat(safeJsonLdPure(obj)) |
| 3 | : safeJsonLdPure; |
Explanation (EN)
Branching the shared escaping/serialization function on test mode means the exact bytes exercised in tests are no longer the exact bytes shipped to production — a regression in the escaping logic could hide behind the test-only path.
Objašnjenje (HR)
Grananje dijeljene funkcije za escaping/serijalizaciju ovisno o test modu znaci da tocni bajtovi koji se testiraju vise nisu tocno oni koji se salju u produkciju — regresija u escaping logici moze se sakriti iza test-only putanje.
Good example
| 1 | // in the test file only |
| 2 | const prettyPrintJsonLd = (html: string) => prettierFormat(html); |
| 3 | expect(prettyPrintJsonLd(renderJsonLd(nodes))).toMatchSnapshot(); |
Explanation (EN)
The production `safeJsonLd`/`renderJsonLd` stays a single, unbranched implementation. Readability for snapshots is a test concern, so the formatting wrapper lives in the test file and never ships.
Objašnjenje (HR)
Produkcijski `safeJsonLd`/`renderJsonLd` ostaje jedna implementacija bez grananja. Citljivost za snapshotove je briga testa, pa formatirajuci wrapper zivi u test datoteci i nikad se ne salje u produkciju.