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).
Co-locate Test Files With Source, Except When the Framework Treats the Directory as Routable
Keep `*.test.ts` next to the file it tests, unless every file in that directory is auto-registered as a route or entry point by the framework.
Bad example
| 1 | // __tests__/utils/formatName.test.ts |
| 2 | import { formatName } from 'utils/static/formatName'; |
| 3 |
|
| 4 | describe('formatName', () => { |
| 5 | it('joins names', () => { |
| 6 | expect(formatName('A', 'B')).toBe('A B'); |
| 7 | }); |
| 8 | }); |
Explanation (EN)
Puts the test in a separate mirrored `__tests__` directory purely by convention, even for a plain utility module where co-locating the test next to its source would cause no problems, making the source harder to navigate.
Objašnjenje (HR)
Test stavlja u odvojeni, zrcaljeni `__tests__` direktorij isključivo iz navike, čak i za obični util modul gdje bi test mogao stajati uz sam source bez ikakvih problema, što otežava snalaženje u kodu.
Good example
| 1 | // utils/static/formatName.ts |
| 2 | // utils/static/formatName.test.ts <- co-located next to its source |
Explanation (EN)
Co-locates the test file next to its source by default for easy navigation, but keeps tests in a separate mirrored directory specifically when the framework auto-registers every file in that folder as a route or entry point (e.g. Next.js `pages/api`), since a co-located `*.test.ts` there would register a phantom endpoint.
Objašnjenje (HR)
Test file se prema zadanom pravilu smješta uz svoj source radi lakše navigacije, no testovi ostaju u odvojenom zrcaljenom direktoriju baš kad framework svaki file u tom folderu automatski registrira kao rutu ili entry point (npr. Next.js `pages/api`), jer bi kolocirani `*.test.ts` ondje registrirao lažni endpoint.
Exceptions / Tradeoffs (EN)
Next.js `pages/api` (and any directory without a `pageExtensions` filter excluding test files) auto-routes every `.ts` file, so tests for API routes must live in a separate `__tests__` tree unless a `pageExtensions` override excludes test files.
Iznimke / Tradeoffi (HR)
Next.js `pages/api` (i svaki direktorij bez `pageExtensions` filtera koji isključuje test fileove) automatski pretvara svaki `.ts` file u rutu, pa testovi za API rute moraju biti u odvojenom `__tests__` stablu, osim ako `pageExtensions` override ne isključi test fileove.