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: node
testingenvironmentmockingtestability
Read the current environment through a helper so tests can mock it
Expose a getCurrentEnv() helper that returns production/development/test and mock that in tests, instead of reading and mutating process.env.NODE_ENV directly in test setup.
PR: hegnar-journalist-boost · org-mining-2026-06Created: Jun 17, 2026
Bad example
Old codetypescript
| 1 | process.env.NODE_ENV = 'production'; |
| 2 | // ...run test... |
| 3 | process.env.NODE_ENV = 'test'; // brittle, easy to leak between tests |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | vi.spyOn(env, 'getCurrentEnv').mockReturnValue('production'); |
| 2 | // code under test calls getCurrentEnv() instead of reading process.env directly |
Explanation (EN)
Objašnjenje (HR)