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).
Don't use runtime validation libraries inside tests; assert instead
In tests, the only thing that should throw is an assertion. Narrow shapes with assertion-based helpers, not zod/valibot parse calls.
Bad example
| 1 | import * as v from 'valibot'; |
| 2 |
|
| 3 | const BodySchema = v.array(OpSchema); |
| 4 |
|
| 5 | test('sends the expected payload', async () => { |
| 6 | const ops = v.parse(BodySchema, await request.json()); // throws a valibot error, not an assertion |
| 7 | expect(ops[0].id).toBe('123'); |
| 8 | }); |
Explanation (EN)
A `parse` failure throws a library error that masks what the test meant to verify, and pulls a runtime-validation dependency into test code where it doesn't belong.
Objašnjenje (HR)
Neuspjeh `parse` baca gresku biblioteke koja prikriva ono sto je test htio provjeriti i uvlaci ovisnost o runtime validaciji u testni kod gdje joj nije mjesto.
Good example
| 1 | function expectUpdateOp(op: GqlOp): asserts op is UpdateOp { |
| 2 | expect(op).toMatchObject({ |
| 3 | operationName: 'UpdateTemplate', |
| 4 | variables: { template: { id: expect.any(String) } }, |
| 5 | }); |
| 6 | } |
| 7 |
|
| 8 | test('sends the expected payload', async () => { |
| 9 | const op = (await request.json())[0]; |
| 10 | expectUpdateOp(op); |
| 11 | expect(op.variables.template.id).toBe('123'); |
| 12 | }); |
Explanation (EN)
A custom assertion helper narrows the type AND fails through the matcher API, so a mismatch reads as a normal assertion with a useful diff.
Objašnjenje (HR)
Vlastiti assertion helper suzava tip I pada kroz matcher API, pa se neslaganje cita kao obican assertion s korisnim diffom.
Exceptions / Tradeoffs (EN)
Fine to import the same validation lib the implementation uses when the test is explicitly verifying that schema's behaviour.
Iznimke / Tradeoffi (HR)
U redu je uvesti istu validacijsku biblioteku koju koristi implementacija kada test izricito provjerava ponasanje te sheme.