Judging AI-Written Tests and Investigating Failures
Why This Matters
You now have tests at every level of the pyramid. Two skills decide whether they help or hinder you: spotting tests that look good but prove nothing, and finding out quickly why a test is red.
Five false-green patterns
These are the most common ways an AI-written (or human-written) test passes while checking nothing:
| Pattern | Looks like | Why it is false |
|---|---|---|
| No expectation | Code runs, no expect | Always passes |
| Expected value copied from the code | expect(formatDuration(63000)).toBe(formatDuration(63000)) | Compares the code with itself |
| Too weak | expect(result).toBeTruthy() | "1m 3s" is truthy too |
| Mocking the thing under test | The test replaces formatDuration with a fake | Tests the fake |
| Swallowed errors | try { ... } catch {} around the expectation | Failures disappear |
Your best tool against all five is the one from lesson 8, A Unit Test: Generate, Then Judge: break the code and check that the test fails.
Review checklist
Use this for every test you accept:
- Does the name describe a behaviour a user or developer cares about?
- Is there a precise expectation, with a value from the requirements, not from the code?
- Which change to the code would make it fail? Have you seen it fail?
- Does it depend on time, order, network or data it does not control?
- Is it at the lowest level of the pyramid that can catch this problem?
Investigating a failing test
When a test is red, the question is always: is the code wrong, the test wrong, or the environment? Work through it in this order:
- Read the failure message. Which test, expected what, received what, which line? (Look back at the real output in lesson 8, A Unit Test: Generate, Then Judge: it answers all four.)
- Reproduce it. Run only that test:
npx vitest run tests/lib/runs.test.ts. Does it fail every time? If not, it may be flaky: think of time, order or network. - Check what changed.
git diffandgit logshow recent changes. The bug is usually in the most recent one. - Decide who is right. Compare expected and received with the requirement, not with your gut. Sometimes the test is outdated.
- Fix one thing, run again, and keep the test that would have caught it.
AI as an investigator
AI is good at the first three steps, if you give it the facts:
This test fails. Here is the full output and the git diff of the last change.
Explain the most likely cause. Do not change any code yet.
Tell me whether you think the code, the test or the environment is wrong, and why.
“Do not change any code yet” matters: an assistant that is told only to “fix the failing test” may simply change the expected value until it is green. That turns a caught bug into a false green.
Exercise
- Ask your AI assistant to write bad tests for
formatDuration: one of each false-green pattern. Can you spot each one, and prove it with a code change? - Ask a colleague (or the AI) to break
src/lib/runs.tswithout telling you how. Find the cause using only the test output,git diffand the steps above. - Write down one rule for your team about how AI may and may not fix failing tests.
Key Takeaways
- Learn the five false-green patterns; breaking the code exposes them all.
- Review every test with the five-question checklist.
- For a red test, decide: code, test or environment? Read, reproduce, diff, decide, fix.
- Let AI investigate, but never let it “fix” a test by changing the expectation unchecked.