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).
Run tests that touch browser globals in a browser/DOM environment
Prefer a real browser/jsdom test over stubbing window in a Node test for code that uses browser globals.
Bad example
| 1 | // foo.test.ts (Node environment) |
| 2 | beforeEach(() => { |
| 3 | vi.stubGlobal('window', { dataLayer: [] }); |
| 4 | }); |
| 5 |
|
| 6 | it('uses window.dataLayer', () => { |
| 7 | track(); |
| 8 | expect(window.dataLayer).toHaveLength(1); |
| 9 | }); |
Explanation (EN)
Stubbing a fake window in Node diverges from the real environment; the stub can hide bugs that only a real window/DOM would surface.
Objašnjenje (HR)
Stubanje laznog window-a u Node-u odstupa od stvarnog okruzenja; stub moze sakriti greske koje bi otkrilo samo pravo window/DOM okruzenje.
Good example
| 1 | // foo.browser.test.tsx (browser environment) |
| 2 | beforeEach(() => { |
| 3 | window.dataLayer = []; |
| 4 | }); |
| 5 |
|
| 6 | it('uses window.dataLayer', () => { |
| 7 | track(); |
| 8 | expect(window.dataLayer).toHaveLength(1); |
| 9 | }); |
Explanation (EN)
Running in a real browser/DOM env means window exists for real, so you reset state instead of fabricating the global.
Objašnjenje (HR)
Pokretanje u stvarnom browser/DOM okruzenju znaci da window stvarno postoji, pa resetiras stanje umjesto da izmisljas global.