Work effectively with AI coding agents. Understand them as language models running in a tool loop with limited context, which is why they guess, game the visible signal and report confidently. Recognise the shortcut and correctness tells in their code across Python, TypeScript/JavaScript, Rust, Java and Go, and know the damage each one does later. Verify changes instead of trusting them, review diffs fast for quality and security, judge when work is truly finished, and choose, design and steer tasks so agents can finish them well.
Tests that restate the implementation
card 5 of 6
A snapshot that froze a bug
builds on: What makes a test able to fail
The agent tests `formatTotal` with `expect(formatTotal(1000000)).toMatchSnapshot()` (amount in cents). It reports *3 snapshots written, all passing*.
In Jest, a missing snapshot is written on the first run and the test passes. The exception is a run with `--ci`, which most CI systems turn on automatically. Local runs, where the agent works, write silently. The stored file says `"$1,0000.00"`, so the grouping bug is now recorded as correct.
**Tell:** *snapshots written*, with no sign anyone read them. **Later cost:** the bug ships, and whoever fixes it sees a failing snapshot and may simply update it back. **Better:** `toBe("$10,000.00")` taken from the spec, or read every new snapshot line as an assertion.
Terms on this card
- spec
- Specification: the stated rules for what the code should do, such as a requirements document or ticket, independent of how the code is written.
- snapshot
- Stored output from an earlier run. Later runs compare against it, and the test fails if the output differs.
- Jest
- A widely used JavaScript/TypeScript test framework with built-in snapshot testing.
- toMatchSnapshot
- Jest assertion that compares a value to a stored snapshot. If no snapshot exists, it writes one and passes.
- --ci
- Jest flag for automated builds, on by default in most CI environments. With it, missing snapshots make the test fail instead of being written silently.
Lesson goal: Spot tests whose expected value is computed with the same formula as the code, snapshots that freeze current buggy output, and assertions that check only 'not null' or 'no exception thrown'. Explain why these stay green when the logic is wrong. ·
the whole lesson