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).
Pull real exports through importOriginal instead of hardcoding copies in a partial mock
In a partial module mock, get the exports you don't want to override from importOriginal so they stay in sync with the source instead of being duplicated.
Bad example
| 1 | vi.mock(import('./consts'), async () => { |
| 2 | return { |
| 3 | BUILD_DIR: await makeTempDir(), |
| 4 | // duplicated by hand from the real module: |
| 5 | BASE_URL_PLACEHOLDER: '/__BASE_URL__', |
| 6 | }; |
| 7 | }); |
Explanation (EN)
The hardcoded `BASE_URL_PLACEHOLDER` copy silently drifts out of sync if the real constant ever changes, and the test keeps passing against a stale value.
Objašnjenje (HR)
Tvrdo kodirana kopija `BASE_URL_PLACEHOLDER` tiho izade iz sinkronizacije ako se prava konstanta promijeni, a test i dalje prolazi protiv zastarjele vrijednosti.
Good example
| 1 | vi.mock(import('./consts'), async (importOriginal) => { |
| 2 | const { BASE_URL_PLACEHOLDER } = await importOriginal(); |
| 3 | return { |
| 4 | BUILD_DIR: await makeTempDir(), // only this one is overridden |
| 5 | BASE_URL_PLACEHOLDER, // always the real value |
| 6 | }; |
| 7 | }); |
Explanation (EN)
`importOriginal()` returns the real module, so the values you don't intend to override are always the source-of-truth values and never drift.
Objašnjenje (HR)
`importOriginal()` vraca pravi modul, pa su vrijednosti koje ne namjeravas nadjacati uvijek izvorne vrijednosti i nikad ne odlutaju.