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 ruleP2universalStack: Jest
testingmocksdry
Define a shared mock return once instead of per test case
When every test stubs the same return value, set it directly on the mock declaration rather than repeating the stub inside each it block.
PR: vinify-backend · org-mining-hist-2026-06Created: Jun 19, 2026
Bad example
Old codetypescript
| 1 | const repo = { findByPk: jest.fn() }; |
| 2 | it('a', () => { repo.findByPk.mockReturnValue({ userId: 10 }); /* ... */ }); |
| 3 | it('b', () => { repo.findByPk.mockReturnValue({ userId: 10 }); /* ... */ }); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | const repo = { findByPk: jest.fn(() => ({ userId: 10 })) }; |
| 2 | it('a', () => { /* ... */ }); |
| 3 | it('b', () => { /* ... */ }); |
Explanation (EN)
Objašnjenje (HR)