100% Test Coverage and the App Still Had 2 Real Bugs
Everyone says check your test coverage. So I gave an AI agent a messy shopping cart API in Express, asked it to write tests, and measured the coverage. It hit 100%. The app still had two real bugs. Here is what happened. The app A small Express cart API, 26 lines. You can add items, get a total with two coupon codes, and remove items. It looks fine at a glance. Nothing is validated except the…
In an article about test coverage, an AI agent was given a simple Express shopping cart API and tasked with writing tests. Astonishingly, the AI achieved 100% line coverage. However, this did not guarantee the absence of bugs. The API, comprised of 26 lines of code, allowed users to add items, calculate totals with coupons, and remove items. It had basic validation for item names, but no other checks.
The reporter then conducted "probing tests" to uncover hidden issues. First, they tested negative quantities, which the API accepted without error and responded with a 201 status code. They also tested sending non-numeric quantities, which resulted in the total returning as "NaN" (not a number). These two tests exposed real bugs in the system, despite the 100% line coverage. This demonstrates that coverage only measures which lines of code were executed, not their correctness.
To address these issues, the reporter added a single line of validation: if the quantity was not a finite number or was zero or less, a 400 error should be returned. Running the tests again, all nine tests passed, and the coverage remained at 100%. The reporter emphasized that coverage is simply a minimum check for code execution, not a measure of correct outputs. The tests that reveal real bugs focus on invalid inputs and unexpected behaviors.
In conclusion, the reporter advised that developers should write tests for the "what should never be allowed" scenarios at each endpoint before trusting the green checkmark indicating coverage. This precautionary measure ensures the app is less prone to breaking due to unnoticed bugs. The full experiment, including code and tests, can be found in a video available at https://youtu.be/JqhsD936gD0.
The report is part of a series called "Ship Stack," which presents real experiments on actual backends with concrete numbers and avoids vague explanations.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to