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: javascript
testingmockingassertionsdeterminism
Don't add optional chaining in tests; mock dependencies and assert on known shapes
Tests should control their inputs and outcomes, so avoid defensive optional chaining; mock third-party calls and assert against the expected, deterministic shape.
PR: hegnar-web · org-mining-hist-2026-06Created: Jun 19, 2026
Bad example
Old codetypescript
| 1 | expect(result?.children?.[0]?.textContent).toBe('hi'); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | // dependency mocked -> shape is guaranteed |
| 2 | expect(result.children[0].textContent).toBe('hi'); |
Explanation (EN)
Objašnjenje (HR)