Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
backend ruleP2stack specificStack: TypeScript / Jest
testingmockingjest
Use jest.fn() for plain mocks and spyOn only when observing or restoring
If a test only needs to stub a return value, jest.fn() is enough. Reach for jest.spyOn when you also need to assert how the original was called or restore it afterward.
PR: hegnar-user-ws · org-mining-hist-2026-06Created: Jun 18, 2026
Bad example
Old codetypescript
| 1 | jest.spyOn(configService, 'get').mockReturnValue('test'); // not observing/restoring |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | configService.get = jest.fn().mockReturnValue('test'); |
Explanation (EN)
Objašnjenje (HR)