How AI code goes wrong

Lesson 23 Hollow tests

6 min read · JavaScript · Quiz

asserting on mocks, blind snapshots, skipped tests, tests rewritten to pass

What you'll learn

  • Spot tests that can never fail: mocks, skips and always-true assertions
  • Prove a test is hollow by breaking the code on purpose
  • Refuse a test that was rewritten to match a bug

The AI says "I added tests and they all pass." Green only means nothing failed. It does not mean anything was checked.

The idea

A test is only worth something if it can fail. Ask one question of every test: if the code were wrong, would this go red? Hollow tests answer no. The usual kinds: a test that asserts on a mock (a fake it set up itself), an assertion that can never fail (|| true), a skipped test, a blind snapshot recorded from whatever the code returned, and a failing test the AI rewrote to pass.

The proof is quick: break the code on purpose and run the tests. If they stay green, they are decoration. In the snippet the function totalWithTax forgets to add the tax, and the suite still says green.

cart.test.js
1import test from "node:test";2import assert from "node:assert/strict";3import { totalWithTax } from "./cart.js";45test("adds 20% tax", (t) => {6  const calc = t.mock.fn(() => 120);7  assert.equal(calc(100), 120);8});910test("rounds to whole cents", { skip: "flaky" }, () => {11  assert.equal(totalWithTax(10.1), 12.12);12});1314test("never goes negative", () => {15  assert.ok(totalWithTax(-5) >= 0 || true);16});1718test("matches snapshot", (t) => {19  t.assert.snapshot(totalWithTax(100));20});
  1. Line 6 A mock of the very function under test. The assertion checks the fake the test just built, so totalWithTax never runs.
  2. Line 10 Skipped. It never runs, so it can't fail. Ask why it was skipped, and who turns it back on.
  3. Line 15 || true makes the condition true whatever the total is. This line can never fail.
  4. Line 19 A snapshot written from the first run records whatever the code returned, bug included. Open the snapshot file and check the value by hand.

Take away

  • Green means nothing failed, not that something was checked.
  • Ask of every test: if the code were wrong, would this go red?
  • Break the code on purpose. A test that stays green is decoration.

Quiz: test yourself

7 questions: a quick quiz, then a review drill. Pick an answer to see why it's right.