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).
Scope test-only build config under the test block, not top-level
Place test-only options (e.g. dep pre-bundling for the test runner) inside the test config block so the production build does not act on them.
Bad example
| 1 | import { defineConfig } from 'vite'; |
| 2 |
|
| 3 | export default defineConfig({ |
| 4 | resolve: { |
| 5 | tsconfigPaths: true, |
| 6 | }, |
| 7 | // Top-level: vite build will also try to use this, even though it's test-only. |
| 8 | optimizeDeps: { |
| 9 | include: ['msw', 'msw/browser', 'vitest-browser-react'], |
| 10 | }, |
| 11 | test: { |
| 12 | browser: { |
| 13 | enabled: true, |
| 14 | }, |
| 15 | }, |
| 16 | }); |
Explanation (EN)
optimizeDeps is declared at the top level, so the production build (vite build) also consumes it even though these are test-only dependencies. The intent is unclear and the build does extra, unintended work.
Objašnjenje (HR)
optimizeDeps je deklariran na najvišoj razini pa ga produkcijski build (vite build) takoder koristi, iako su to ovisnosti samo za testove. Namjera je nejasna, a build radi dodatni, nezeljeni posao.
Good example
| 1 | import { defineConfig } from 'vite'; |
| 2 |
|
| 3 | export default defineConfig({ |
| 4 | resolve: { |
| 5 | tsconfigPaths: true, |
| 6 | }, |
| 7 | test: { |
| 8 | browser: { |
| 9 | enabled: true, |
| 10 | }, |
| 11 | // Test-only: scoped under `test`, so `vite build` never touches it. |
| 12 | optimizeDeps: { |
| 13 | include: ['msw', 'msw/browser', 'vitest-browser-react'], |
| 14 | }, |
| 15 | }, |
| 16 | }); |
Explanation (EN)
The same option is nested under the test config, so only the test runner uses it. The production build is unaffected and the intent (test-only) is explicit from the structure.
Objašnjenje (HR)
Ista opcija je ugnijezdena unutar test konfiguracije pa je koristi samo test runner. Produkcijski build nije pogoden, a namjera (samo za testove) je jasna iz same strukture.
Notes (EN)
Applies to any tool that merges a shared config with a separate test section (Vite/Vitest, Jest projects, etc.). When the runner supports a test-scoped equivalent of a build option, prefer the scoped one.
Bilješke (HR)
Vrijedi za svaki alat koji spaja zajednicku konfiguraciju s odvojenim test dijelom (Vite/Vitest, Jest projekti, itd.). Kad runner podrzava test-scoped ekvivalent build opcije, koristi onu unutar test bloka.
Exceptions / Tradeoffs (EN)
If an option genuinely needs to apply to both the build and the tests, keeping it top-level is correct.
Iznimke / Tradeoffi (HR)
Ako opcija stvarno mora vrijediti i za build i za testove, ispravno je drzati je na najvisoj razini.