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).
Stub globals with stubGlobal instead of manually assigning and tearing them down
Set global values in tests via the runner's stubGlobal helper, which auto-restores, rather than assigning to the global object in beforeEach and writing matching cleanup.
Bad example
| 1 | beforeEach(() => { |
| 2 | window.Session = { isAuthenticated: true }; |
| 3 | }); |
| 4 | // easy to forget the matching afterEach, leaking state into other tests |
| 5 | afterEach(() => { |
| 6 | delete window.Session; |
| 7 | }); |
Explanation (EN)
Manually mutating a global requires a hand-written teardown to undo it. Forgetting the cleanup leaks state into later tests and causes order-dependent flakiness.
Objašnjenje (HR)
Rucno mijenjanje globalne vrijednosti zahtijeva rucno napisan teardown koji to ponistava. Zaboravljeno ciscenje propusta stanje u kasnije testove i uzrokuje nestabilnost ovisnu o redoslijedu.
Good example
| 1 | beforeEach(() => { |
| 2 | vi.stubGlobal('Session', { isAuthenticated: true }); |
| 3 | }); |
| 4 | // with restoreMocks/unstubAllGlobals configured, no manual teardown needed |
Explanation (EN)
`vi.stubGlobal` records the original value and restores it automatically (with unstubAllGlobals / restoreMocks), so there is no teardown to forget and tests stay isolated.
Objašnjenje (HR)
`vi.stubGlobal` biljezi izvornu vrijednost i automatski je vraca (uz unstubAllGlobals / restoreMocks), pa nema teardowna koji bi se zaboravio i testovi ostaju izolirani.
Notes (EN)
Ensure unstubAllGlobals (or restoreMocks/unstubGlobals: true) is configured so stubs are reset between tests.
Bilješke (HR)
Pobrini se da je unstubAllGlobals (ili restoreMocks/unstubGlobals: true) konfiguriran kako bi se stubovi resetirali izmedu testova.