Skip to main content

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:

PatternLooks likeWhy it is false
No expectationCode runs, no expectAlways passes
Expected value copied from the codeexpect(formatDuration(63000)).toBe(formatDuration(63000))Compares the code with itself
Too weakexpect(result).toBeTruthy()"1m 3s" is truthy too
Mocking the thing under testThe test replaces formatDuration with a fakeTests the fake
Swallowed errorstry { ... } catch {} around the expectationFailures 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:

  1. Does the name describe a behaviour a user or developer cares about?
  2. Is there a precise expectation, with a value from the requirements, not from the code?
  3. Which change to the code would make it fail? Have you seen it fail?
  4. Does it depend on time, order, network or data it does not control?
  5. 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:

  1. 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.)
  2. 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.
  3. Check what changed. git diff and git log show recent changes. The bug is usually in the most recent one.
  4. Decide who is right. Compare expected and received with the requirement, not with your gut. Sometimes the test is outdated.
  5. 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

  1. 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?
  2. Ask a colleague (or the AI) to break src/lib/runs.ts without telling you how. Find the cause using only the test output, git diff and the steps above.
  3. 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.