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).
Use vi.mocked / jest.mocked instead of `as Mock` casts
Wrap mocked functions with the framework's typed mock helper rather than casting to Mock, preserving signatures and avoiding lint-suppression comments.
Bad example
| 1 | import type { Mock } from 'vitest'; |
| 2 |
|
| 3 | // oxlint-disable-next-line typescript/no-unsafe-type-assertion |
| 4 | const mockedFetchUser = fetchUser as Mock; |
| 5 | mockedFetchUser.mockResolvedValue(defaultUser); |
Explanation (EN)
Casting to Mock is an unsafe assertion that throws away the original signature, requires a lint-suppression comment, and lets you call mock methods with arguments the real function would reject.
Objašnjenje (HR)
Cast u Mock je nesigurna tvrdnja koja odbacuje originalni potpis, zahtijeva komentar za guseanje lintera i dopusta pozivanje mock metoda s argumentima koje bi prava funkcija odbila.
Good example
| 1 | const mockedFetchUser = vi.mocked(fetchUser); |
| 2 | mockedFetchUser.mockResolvedValue(defaultUser); |
Explanation (EN)
vi.mocked keeps the mock typed to the real function signature, so mockResolvedValue / arguments are type-checked and no lint suppression is needed.
Objašnjenje (HR)
vi.mocked drzi mock tipiziran prema potpisu prave funkcije, pa su mockResolvedValue i argumenti tipski provjereni i nije potrebno guseti linter.
Notes (EN)
The Jest equivalent is jest.mocked. Both are preferable to manual `as unknown as Mock` chains.
Bilješke (HR)
Jest ekvivalent je jest.mocked. Oba su bolja od rucnih `as unknown as Mock` lanaca.