Skip to main content

A Unit Test: Generate, Then Judge

Why This Matters

This is the core loop of the whole course. You let AI write a test, and then you prove whether the test is worth anything by breaking the code on purpose. A test that stays green when the code is wrong is worse than no test: it gives false confidence.

Step 1: generate

Give the AI your test list from lesson 7, Test Cases Before Code and the code:

Write Vitest unit tests for formatDuration in src/lib/runs.ts.
Use exactly these cases: <paste your table>.
Use one `it` per behaviour, with a name that describes the behaviour.
Do not change src/lib/runs.ts.

Read what you get. Check three things before running anything:

  1. Does every case from your list appear, with the expected value you wrote down, not a value the AI calculated from the code?
  2. Do the names describe behaviour (“shows minutes and zero-padded seconds”) rather than implementation (“test padStart”)?
  3. Is there an expect in every test? A test without an expectation always passes.

Step 2: run

npm test

Real Vitest output in the terminal: 7 tests in tests/lib/runs.test.ts, all passed

Green. But green only means the test and the code agree. It does not mean either of them is right.

Step 3: break it on purpose

Open src/lib/runs.ts and make one small, realistic mistake. Change:

return `${minutes}m ${String(seconds).padStart(2, '0')}s`

to:

return `${minutes}m ${String(seconds).padStart(1, '0')}s`

Run the tests again:

Real Vitest output after breaking the code: the test “shows minutes and zero-padded seconds from one minute” fails with expected ‘1m 3s’ to be ‘1m 03s’; 1 failed, 6 passed

This is what a good test looks like when it catches a bug:

  • It fails, so the bug cannot slip through.
  • Its name tells you which behaviour broke, without reading code.
  • The message shows expected versus received: "1m 03s" versus "1m 3s".
  • It points to the line: tests/lib/runs.test.ts:16.

Now put the code back:

git restore src/lib/runs.ts
npm test   # green again

Make it a habit

For every test you accept, from an AI or a colleague, ask: “Which change to the code would make this test fail?” If you cannot think of one, the test probably checks nothing. Tools can automate this idea; it is called mutation testing, and TestRun Board has it (npm run mutation). You meet it again in the Agentic Engineering course.

Exercise

  1. Generate tests for formatDuration with the prompt above and review them with the three checks.
  2. Break the code in three different ways: the padStart change, changing 10_000 to 1_000, and removing the ms < 0 check. Which tests catch each change? Which change is not caught?
  3. For a change that is not caught, write the missing test case in plain words, then ask the AI to add exactly that test. Restore the code with git restore.

Key Takeaways

  • Generate, review, run, break, restore: that is the loop.
  • Check that expected values come from your list, not from the code.
  • A good failing test names the behaviour, shows expected versus received and points to the line.
  • Ask of every test: which change would make it fail?