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).
Don't manually clear the DOM when the test runner auto-cleans it
Skip manual document.body resets in afterEach when the test environment already cleans the DOM between tests.
Bad example
| 1 | describe('Widget', () => { |
| 2 | afterEach(() => { |
| 3 | Widget.list.length = 0; |
| 4 | document.body.innerHTML = ''; // framework already resets the DOM |
| 5 | }); |
| 6 |
|
| 7 | it('renders', () => { |
| 8 | setupWidget(); |
| 9 | // ... |
| 10 | }); |
| 11 | }); |
Explanation (EN)
Manually wiping document.body duplicates work the test runner already does between tests, adding noise and implying cleanup that the environment handles for you.
Objašnjenje (HR)
Rucno brisanje document.body duplicira posao koji test runner ionako radi izmedu testova, dodaje sum i sugerira ciscenje koje okruzenje vec obavlja umjesto tebe.
Good example
| 1 | describe('Widget', () => { |
| 2 | afterEach(() => { |
| 3 | Widget.list = []; // only reset state the runner does NOT clean |
| 4 | }); |
| 5 |
|
| 6 | it('renders', () => { |
| 7 | setupWidget(); |
| 8 | // ... |
| 9 | }); |
| 10 | }); |
Explanation (EN)
Only reset state the framework cannot reach (module-level singletons, static registries). Let the runner handle DOM teardown so cleanup code stays minimal and intentional.
Objašnjenje (HR)
Resetiraj samo stanje do kojeg framework ne moze doci (singletoni na razini modula, staticki registri). Pusti runner da odradi rusenje DOM-a kako bi kod za ciscenje ostao minimalan i namjeran.
Notes (EN)
Verify the assumption: Vitest browser mode and @testing-library/react (with auto-cleanup) reset the DOM between tests. Plain happy-dom/jsdom without cleanup configured may not.
Bilješke (HR)
Provjeri pretpostavku: Vitest browser mode i @testing-library/react (s automatskim ciscenjem) resetiraju DOM izmedu testova. Obicni happy-dom/jsdom bez konfiguriranog cleanupa mozda nece.
Exceptions / Tradeoffs (EN)
If you opt out of auto-cleanup or share a single DOM across tests deliberately, manual teardown is justified.
Iznimke / Tradeoffi (HR)
Ako se iskljucis iz automatskog ciscenja ili namjerno dijelis jedan DOM izmedu testova, rucno rusenje je opravdano.