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 ruleP1universalStack: general
testingmockingunit-tests
Mock dependencies in unit tests instead of exercising nested implementations
A unit test should mock the collaborators a function calls and assert on its own contract, not invoke and depend on the behavior of methods-within-methods.
PR: hegnar-ws · org-mining-hist-2026-06Created: Jun 18, 2026
Bad example
Old codetypescript
| 1 | // test for purgeSingleImageCache that actually runs the real purgeSingleFile |
| 2 | const res = await purgeSingleImageCache({ url }); |
| 3 | expect(res.success).toBe(true); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | vi.spyOn(mod, 'purgeSingleFile').mockResolvedValue({ success: true, existed: true }); |
| 2 | const res = await purgeSingleImageCache(url); |
| 3 | expect(res).toEqual({ success: true, data: { /* ... */ } }); |
Explanation (EN)
Objašnjenje (HR)