Skip to content
Antoine Le LouëtIndependent senior developer (Home)
Back to the Journal

Advice2 min read

All tests pass. The bug is still there.

An assertion that is too weak misses a rendering defect; a regression test detects it before correction and after reintroduction.

Expected text preserves angle brackets as characters; the initial assertion checks only a word, while the new one checks escaping.
An assertion can pass while missing the very defect the reader wants to fix.

A green test can answer the wrong question

The lab receives text and places it in an HTML paragraph. The expected rule is to preserve the entered text even when it contains angle brackets. The first function directly interpolates that input. An input of ‘<b>Texte</b>’ therefore becomes markup instead of remaining literal text.

JavaScript · deliberately defective version
const render = text => `<p>${text}</p>`;
assert.match(render('Texte'), /Texte/);

The initial assertion passes. It proves that the word ‘Texte’ is present in this case, not that the content is treated as text. A separate reproduction displays the generated string with angle brackets: <p><b>Texte</b></p>. This teaching example is attributed to no tool or client.

Make a test fail on the expected behaviour

JavaScript · regression assertion
assert.equal(render('<b>Texte</b>'),
  '<p>&lt;b&gt;Texte&lt;/b&gt;</p>');

This assertion fails on the initial function. It expresses the rule before changing code. The lab runs it inside an assertion expecting that failure, preserving a reproducible overall command while showing the red state.

If the business rule were to accept a subset of markup, this test would be incorrect. Validating the need precedes choosing the assertion. A test result must always be read alongside the rule it checks.

Two diffs to keep

Diff · implementation change
- const render = text => `<p>${text}</p>`;
+ const render = text => `<p>${text.replaceAll('&','&amp;')
+   .replaceAll('<','&lt;').replaceAll('>','&gt;')}</p>`;
Diff · verification change
- assert.match(render('Texte'), /Texte/);
+ assert.equal(render('<b>Texte</b>'),
+   '<p>&lt;b&gt;Texte&lt;/b&gt;</p>');

Reintroduce the defect to check the test

Under Node.js 24.13.0, the test fails on the defective state, passes after correction and fails again when the initial function is reintroduced. The script preserves these four observations. The fix concerns an HTML text context; it is not universal escaping for attributes, URLs or scripts.

The regression test fails on the defective function, passes after correction, then fails when the initial function is reintroduced. The same case must detect the defect’s return.
Reading of the check performed with the lab’s new assertion. The initial weak test is separate: this cycle concerns the regression test checking literal text.

The test boundary also matters

A correct function can be bypassed by a caller, or its output changed by final rendering. The lab’s string check does not prove integration across an entire application. Add a browser flow when that risk belongs to the real change.

When reviewing a fix, ask which defect would make the new test fail. Reintroducing that specific defect provides more useful evidence than a count of added tests. Other possible defects still need examining according to the need’s scope.

Sources and useful links

Share on LinkedIn (new tab)

Keep reading
All articles in this category