Topic 1 of 5 · 4 min read
Tests that pass for the wrong reason
Goal: Explain why an AI-written test would still pass if the code under test were broken.
A test is only worth keeping if it would fail when the code is wrong; an AI-written test that cannot fail is decoration with a green tick. Assistants such as Claude, ChatGPT, Copilot and Cursor are very good at producing tests that look right and run green on the first try. That is the problem. Green is the easy part. Your job is to ask what the test would do against broken code.
Check yourself · 1 of 3
An AI wrote a test that sets a mock to return 42, calls the mock, and asserts it got 42. What is wrong with it?
Why it matters
A suite that cannot fail gives you false safety. You merge, the dashboard is green, and the bug ships. Models are trained to produce code that compiles and passes, so a test written after the code often just describes the code.
Four patterns to look for
- The tautology. The expected value is computed with the same logic as the code, or the test asserts something that is always true.
- The assertion on the mock. The test sets up a mock to return a value, calls the code, and asserts the mock returned that value. Nothing real ran.
- The rubber-stamped snapshot. The snapshot was generated from the current output, so it stores whatever the code does today, bugs included.
- The copied implementation. The test repeats the same formula, loop or regex as the code. If the formula is wrong, both are wrong in the same way.
How to check
Read each assertion and ask one question: if I broke the line this test claims to cover, would it fail? Then do it: change the code on purpose, run the test, and watch.
Before and after
// Weak: the expected value reuses the code's own formula
test("total with tax", () => {
const items = [{ price: 100 }, { price: 50 }];
expect(orderTotal(items, 0.2)).toBe(sumPrices(items) * (1 + 0.2));
});
If orderTotal applies tax twice, sumPrices is still reused and the test may agree with the bug. The fixed test states the answer a human worked out:
test("total with 20% tax on 150", () => {
expect(orderTotal([{ price: 100 }, { price: 50 }], 0.2)).toBe(180);
});
What goes wrong
- Trusting the coverage number. A line can be executed without being checked. Coverage says the code ran, not that anything verified it.
- Mocking the thing under test. If the function you are testing is itself replaced, the test checks the replacement.
- Regenerating snapshots to make a failure go away. The snapshot then records the new behaviour, right or wrong.
- Asserting only "does not throw". Many generated tests end with
expect(result).toBeDefined(), which almost any output satisfies.
Hard-coded expected values from the spec are the cheapest defence. They are boring, and boring is what you want from a test.
Examples
The assertion on the mock
const repo = { find: vi.fn().mockResolvedValue({ id: 1, name: "Ana" }) };
const user = await repo.find(1);
expect(user.name).toBe("Ana");
The test calls the mock directly and checks what the mock was told to say. The real UserService is never involved. Call the service with the mocked repo and assert on what the service returns.
The always-true assertion
def test_parse_price():
result = parse_price("$4.50")
assert result is not None
Any output except None passes, including 450 or 0.45. Assert the exact value: assert result == Decimal("4.50").
Key points
- Ask of every assertion: if the line it covers were broken, would this test fail?
- A test that computes its expected value with the code's own logic will agree with the code's bugs.
- Asserting on a value a mock was told to return checks the mock, not your code.
- A snapshot generated from current output stores today's behaviour, including its bugs.
- Coverage shows that code ran, not that anything checked the result.
Common mistakes
- Treating a green run on the first attempt as proof that the test is good.
- Computing the expected value in the test with the same helper or formula the code uses.
- Accepting snapshots that were generated from the code under review.
- Reading the number of tests or the coverage figure instead of the assertions.
Check yourself · 2 of 3
A test computes the expected discount as price * 0.9, the same line the function uses. Why is that weak?
Check yourself · 3 of 3
A snapshot test was just generated by running the new code once. What does it prove?
Practice items, not counted towards your mastery.
Start here
- The Practical Test Pyramid (martinfowler.com) (opens in a new tab)ArticleFree
Where unit, integration and end-to-end tests belong, and why test behaviour rather than implementation.
- Mocks Aren't Stubs (martinfowler.com) (opens in a new tab)ArticleFree
Defines mocks, stubs, fakes and spies, and the difference between state and behaviour verification.
- Stryker mutation testing (opens in a new tab)ToolFree
Mutation testing for JavaScript, TypeScript, C# and Scala: the tool that checks whether tests notice changed code.