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 and assert on the global directly, not via a local alias
Avoid a top-level test variable that just mirrors a global; set up and assert on the global itself.
Bad example
| 1 | let dataLayer: Array<Record<string, unknown>>; |
| 2 |
|
| 3 | beforeEach(() => { |
| 4 | dataLayer = []; |
| 5 | vi.stubGlobal('window', { dataLayer }); |
| 6 | }); |
| 7 |
|
| 8 | it('pushes an event', () => { |
| 9 | track(); |
| 10 | expect(dataLayer).toHaveLength(1); |
| 11 | }); |
Explanation (EN)
The dataLayer alias adds indirection and can drift from window.dataLayer; the test reads less like the real code path.
Objašnjenje (HR)
Alias dataLayer dodaje indirekciju i moze se razici od window.dataLayer; test manje nalikuje stvarnom toku koda.
Good example
| 1 | beforeEach(() => { |
| 2 | vi.stubGlobal('window', { dataLayer: [] }); |
| 3 | }); |
| 4 |
|
| 5 | it('pushes an event', () => { |
| 6 | track(); |
| 7 | expect(window.dataLayer).toHaveLength(1); |
| 8 | }); |
Explanation (EN)
Stubbing and asserting on window.dataLayer keeps the test aligned with the code under test and removes the redundant variable.
Objašnjenje (HR)
Stubanje i provjera na window.dataLayer drzi test uskladen s kodom koji se testira i uklanja suvisnu varijablu.