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).
Check for an auto-reset mock config before manually clearing mocks in each test
Before adding a beforeEach/afterEach that manually calls .mockClear()/.mockReset() on spies, check whether the test runner is already configured to do it automatically (e.g. Vitest's clearMocks/resetMocks/restoreMocks) — manual clearing on top of that is redundant.
Bad example
| 1 | // vitest.config.ts already has clearMocks: true |
| 2 |
|
| 3 | beforeEach(() => { |
| 4 | mySpy.mockClear(); |
| 5 | }); |
Explanation (EN)
The config already clears every mock between tests, so this manual call does nothing and just adds boilerplate that future readers have to reason about.
Objašnjenje (HR)
Konfiguracija već čisti svaki mock između testova, pa ovaj ručni poziv ne radi ništa i samo dodaje kod koji budući čitatelji moraju analizirati.
Good example
| 1 | // vitest.config.ts: clearMocks: true — no manual mockClear() needed |
| 2 |
|
| 3 | it('calls the handler', () => { ... }); |
Explanation (EN)
Relying on the runner's built-in mock lifecycle keeps individual test files free of boilerplate that duplicates global config.
Objašnjenje (HR)
Oslanjanje na ugrađeni lifecycle mockova test runnera drži pojedinačne test fileove čistima od koda koji duplicira globalnu konfiguraciju.