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).
Pass import() to vi.mock instead of a bare module string
Mock modules with vi.mock(import('path/to/module')) rather than vi.mock('path/to/module'). The import() form is type-checked: the module path is validated by TypeScript, and the factory/mocked result is typed to the real module's shape, so typos, moved files, and drifted mock shapes fail at compile time instead of silently.
Bad example
| 1 | vi.mock('utils/tracking/editPortfolio'); |
Explanation (EN)
A bare string path is not type-checked: a typo or a later file move is not caught, and a factory's return shape is untyped.
Objašnjenje (HR)
Good example
| 1 | vi.mock(import('utils/tracking/editPortfolio')); |
Explanation (EN)
The import() argument makes TypeScript validate the module path and type the mocked module's shape (including any factory you provide).
Objašnjenje (HR)
Notes (EN)
Supported since Vitest 2.1; applies to vi.mock and vi.doMock. Especially valuable after renames/moves, where stale string paths would otherwise keep passing silently.
Exceptions / Tradeoffs (EN)
Dynamic or computed module paths cannot use the import() form.