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).
Verify a test assertion can actually fail before relying on it
Before adding an assertion meant to catch a specific bug, check that a broken implementation would actually make it fail — an assertion that's structurally always true tests nothing.
Bad example
| 1 | const html = renderJsonLd([{ name: '</script>' }]); |
| 2 | expect(html).not.toContain('</script><'); |
Explanation (EN)
Even a naive `JSON.stringify` without any escaping would produce `</script>"}</script>` (note the closing quote/brace between the two tags), so this exact substring would never appear regardless of whether escaping works — the assertion can't catch the bug it's meant to catch.
Objašnjenje (HR)
Cak bi i naivan `JSON.stringify` bez ikakvog escapinga proizveo `</script>"}</script>` (primijeti zatvarajuci navodnik/zagradu izmedu dva taga), pa se ovaj tocan podniz nikad ne bi pojavio bez obzira radi li escaping ili ne — tvrdnja ne moze uhvatiti gresku koju treba uhvatiti.
Good example
| 1 | const html = renderJsonLd([{ name: '</script>' }]); |
| 2 | expect(html.match(/<\/script>/g)).toHaveLength(1); |
Explanation (EN)
Counting the closing-script-tag occurrences directly tests the property that matters (exactly one real closing tag, no injected one), and would fail if escaping were removed.
Objašnjenje (HR)
Brojanje pojavljivanja zatvarajuceg script taga izravno testira svojstvo koje je bitno (tocno jedan pravi zatvarajuci tag, bez ubacenog), i palo bi kad bi se escaping uklonio.