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 reset/re-import modules per test unless you actually mock modules
Use a static import for the unit under test; reserve vi.resetModules() + dynamic import for cases that truly re-mock module dependencies, not mere global stubbing.
Bad example
| 1 | describe('parsePrice', () => { |
| 2 | beforeEach(() => { |
| 3 | vi.resetModules(); |
| 4 | vi.stubGlobal('navigator', { language: 'en' }); |
| 5 | }); |
| 6 |
|
| 7 | it('formats value', async () => { |
| 8 | const { parsePrice } = await import('./price'); |
| 9 | expect(parsePrice('1,5')).toBe(1.5); |
| 10 | }); |
| 11 | }); |
Explanation (EN)
vi.resetModules() and per-test dynamic import only matter when you re-mock module dependencies between tests. Stubbing a global is unrelated, so this adds async boilerplate and re-evaluation cost for no benefit.
Objašnjenje (HR)
vi.resetModules() i dinamicki import po testu imaju smisla samo kad izmedu testova ponovno mockas modulske ovisnosti. Stubanje globala nema veze s tim, pa ovo dodaje async boilerplate i trosak ponovne evaluacije bez koristi.
Good example
| 1 | import { parsePrice } from './price'; |
| 2 |
|
| 3 | describe('parsePrice', () => { |
| 4 | beforeEach(() => { |
| 5 | vi.stubGlobal('navigator', { language: 'en' }); |
| 6 | }); |
| 7 |
|
| 8 | it('formats value', () => { |
| 9 | expect(parsePrice('1,5')).toBe(1.5); |
| 10 | }); |
| 11 | }); |
Explanation (EN)
A static import keeps the test synchronous and clear. Global stubs work without resetting the module registry, so the dynamic import and resetModules are dropped.
Objašnjenje (HR)
Staticki import cini test sinkronim i jasnim. Globalni stubovi rade bez resetiranja registra modula, pa se dinamicki import i resetModules uklanjaju.
Exceptions / Tradeoffs (EN)
Keep vi.resetModules() + dynamic import when a test must observe different vi.mock()/vi.doMock() module replacements or re-run top-level module side effects in isolation.
Iznimke / Tradeoffi (HR)
Zadrzi vi.resetModules() + dinamicki import kad test mora vidjeti razlicite vi.mock()/vi.doMock() zamjene modula ili izolirano ponovno pokrenuti nuspojave na razini modula.