Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Cover non-default values, failure paths, and state transitions in tests
Don't test only the happy path; add cases for non-default inputs, dependency failures, and field changes that trigger recomputation.
Bad example
| 1 | describe('createTransaction', () => { |
| 2 | it('creates a transaction', async () => { |
| 3 | const result = await service.createTransaction(baseDto); // base currency, rate 1, no failures |
| 4 | expect(result.priceInBase).toBe(baseDto.price); |
| 5 | }); |
| 6 | }); |
Explanation (EN)
Only the trivial path (default currency, exchange rate 1, no dependency failure) is exercised, so conversion math and error handling are never verified.
Objašnjenje (HR)
Testira se samo trivijalan put (zadana valuta, tecaj 1, bez kvara ovisnosti), pa se matematika konverzije i obrada gresaka nikad ne provjeravaju.
Good example
| 1 | describe('createTransaction', () => { |
| 2 | it('computes priceInBase and total for a non-base currency (rate 10.5)', async () => { |
| 3 | rateService.fetch.mockResolvedValue(10.5); |
| 4 | const result = await service.createTransaction(usdDto); |
| 5 | expect(result.priceInBase).toBe(usdDto.price * 10.5); |
| 6 | }); |
| 7 |
|
| 8 | it('rejects when the exchange-rate fetch fails', async () => { |
| 9 | rateService.fetch.mockRejectedValue(new Error('upstream down')); |
| 10 | await expect(service.createTransaction(usdDto)).rejects.toThrow(); |
| 11 | }); |
| 12 |
|
| 13 | it('re-fetches the rate when currency changes on update', async () => { |
| 14 | await service.updateTransaction(id, { currency: 'USD' }); |
| 15 | expect(rateService.fetch).toHaveBeenCalledWith('USD', expect.any(String)); |
| 16 | }); |
| 17 | }); |
Explanation (EN)
These cases verify the conversion math with a real rate, the failure path of the dependency, and the re-fetch triggered by a state transition — the exact behaviors the feature adds.
Objašnjenje (HR)
Ovi slucajevi provjeravaju matematiku konverzije sa stvarnim tecajem, put kvara ovisnosti i ponovno dohvacanje pokrenuto promjenom stanja — upravo ponasanja koja znacajka uvodi.