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).
fullstack ruleP2universalStack: typescript
testingmockingreadability
Keep each test mock narrow and single-purpose
Avoid one mega-mock with many flags handling unrelated scenarios; split it into focused mocks and only require the inputs each test actually uses.
PR: hegnar-forum-web · org-mining-hist-2026-06Created: Jun 20, 2026
Bad example
Old codets
| 1 | const mockFetch = ({ status, ok, data, error, returnHtml }) => { |
| 2 | global.fetch = jest.fn(() => { |
| 3 | if (error) throw new Error('Error'); |
| 4 | if (returnHtml) return Promise.resolve(new Response('<html>...')); |
| 5 | return Promise.resolve({ json: () => Promise.resolve(data), status, ok }); |
| 6 | }); |
| 7 | }; |
Explanation (EN)
Objašnjenje (HR)
Good example
New codets
| 1 | const mockFetchWithError = () => { throw new Error('Error'); }; |
| 2 | const mockFetchWithHtml = () => Promise.resolve(new Response('<html>...', { status: 200 })); |
| 3 | const mockFetchJson = (data, status, ok) => () => Promise.resolve({ json: () => Promise.resolve(data), status, ok }); |
| 4 | // in each test: global.fetch = jest.fn(mockFetchWithError); |
Explanation (EN)
Objašnjenje (HR)