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 tests for every new code path a change introduces
When a change broadens behavior to handle new cases (new file types, branches, inputs), add tests that exercise those new cases before merging.
Bad example
| 1 | // Before: only HTML was rewritten, and only HTML was tested. |
| 2 | // PR widens the glob to also rewrite JS and CSS files... |
| 3 | const options = { |
| 4 | files: [join(BUILD_DIR, '*'), join(BUILD_DIR, 'static', '*')], |
| 5 | from: new RegExp(BASE_URL_PLACEHOLDER, 'g'), |
| 6 | }; |
| 7 | // ...but no test asserts JS/CSS files actually get rewritten. |
Explanation (EN)
The new JS/CSS rewriting path ships with zero coverage. A future refactor of the glob or regex could silently stop rewriting those files and no test would catch it.
Objašnjenje (HR)
Novi put za prepisivanje JS/CSS datoteka isporucuje se bez ikakve pokrivenosti. Buduci refaktor glob uzorka ili regexa mogao bi tiho prestati prepisivati te datoteke, a nijedan test to ne bi uhvatio.
Good example
| 1 | test('rewrites the base URL placeholder in JS files', async () => { |
| 2 | await run(); |
| 3 | await expect(readFile(join(BUILD_DIR, 'static', 'chunk.js'), 'utf-8')) |
| 4 | .resolves.toContain('https://cdn.example.com/'); |
| 5 | }); |
| 6 |
|
| 7 | test('rewrites the base URL placeholder in CSS files', async () => { |
| 8 | await run(); |
| 9 | await expect(readFile(join(BUILD_DIR, 'static', 'styles.css'), 'utf-8')) |
| 10 | .resolves.toContain('url(https://cdn.example.com/static/bg.png)'); |
| 11 | }); |
Explanation (EN)
Each newly supported file type gets an explicit test, so the new behavior is verified now and protected against regressions later.
Objašnjenje (HR)
Svaki novopodrzani tip datoteke dobiva eksplicitan test, pa je novo ponasanje sada provjereno i zasticeno od buducih regresija.