Why Your AI Keeps Editing Your Tests (and Which Tests to Let It Touch)

When an AI fix turns a test red, one way out is to edit the test until it is green. Which tests it can safely edit depends on what they check. A test of what a user sees (the total on the screen, the line on the invoice) still fails when a fix breaks something else. A test of how the code works inside can pass while the app is broken, and the AI will happily rewrite it to match. Write the first kind, and guard the second.
This post runs the same fix against both kinds of tests, in a scratch repo you can copy. Your AI Fixed One Bug and Broke Two covers the fix loop itself; this post is about the tests, and what happens when the AI is allowed to edit them. Every output below is from a real run (vitest 5.1.2, Node 22).
What is the difference between a behavior and an implementation detail?
A behavior is something a person can see or get: “the cart shows $54.50”, “the invoice shows the same amount due”. An implementation detail is how the code produces it: which helper is called, what its arguments are, what it is named.
Google’s testing guidance says to test behaviors, not methods: a single method can show many behaviors, and one behavior can span several methods. Kent C. Dodds names the two costs of getting it wrong in Testing Implementation Details. Tests tied to internals break when you refactor (red, app fine) and may stay green when you break the app. The second one is the dangerous one.
A small store with two screens
The shared function orderTotal prices an order. Two screens call it, the cart and the invoice. The rules in plain English: SAVE10 takes 10% off, and shipping is $5.00, free once the items cost $50.00 or more after the coupon. Money is in whole cents, so the numbers are exact.
Save this as shop.js in a scratch folder. It has the shipping bug from the tests post: shipping looks at the price before the coupon.
const sum = (items) => items.reduce((s, i) => s + i.priceCents * i.qty, 0);const dollars = (cents) => (cents / 100).toFixed(2);
export function orderTotal({ subtotalCents, coupon }) { const discount = coupon === 'SAVE10' ? Math.round(subtotalCents * 0.1) : 0; const shipping = subtotalCents >= 5000 ? 0 : 500; return subtotalCents - discount + shipping;}
export function renderCart(items, coupon) { return `Total: $${dollars(orderTotal({ subtotalCents: sum(items), coupon }))}`;}
export function renderInvoice(items, coupon) { return `Amount due: $${dollars(orderTotal({ subtotalCents: sum(items), coupon }))}`;}Two test files. The first checks what each screen shows (behavior.test.js):
import { it, expect } from 'vitest';import { renderCart, renderInvoice } from './shop.js';
const items = [{ priceCents: 3000, qty: 1 }, { priceCents: 2500, qty: 1 }]; // $55.00const boundary = [{ priceCents: 5556, qty: 1 }]; // $55.56 less 10% rounds to exactly $50.00
it('cart: $55.00 with SAVE10 is $49.50 of items, so shipping applies: Total $54.50', () => { expect(renderCart(items, 'SAVE10')).toBe('Total: $54.50');});it('invoice: the same order shows the same amount due, $54.50', () => { expect(renderInvoice(items, 'SAVE10')).toBe('Amount due: $54.50');});it('cart: $55.00 with no coupon ships free: Total $55.00', () => { expect(renderCart(items, '')).toBe('Total: $55.00');});it('invoice: $55.00 with no coupon: Amount due $55.00', () => { expect(renderInvoice(items, '')).toBe('Amount due: $55.00');});it('a basket that is exactly $50.00 after the coupon ships free', () => { expect(renderCart(boundary, 'SAVE10')).toBe('Total: $50.00');});The second tests the helper on its own (internals.test.js), the way tests are often written when someone says “add tests for this function”:
import { it, expect } from 'vitest';import { orderTotal } from './shop.js';
it('orderTotal applies 10% for SAVE10', () => { expect(orderTotal({ subtotalCents: 10000, coupon: 'SAVE10' })).toBe(9000);});it('orderTotal adds 500 shipping under 5000', () => { expect(orderTotal({ subtotalCents: 2000, coupon: '' })).toBe(2500);});Set up and run:
npm init -ynpm pkg set type=module scripts.test="vitest run"npm i -D vitestnpm testStep 1: red, for the right reason
× cart: $55.00 with SAVE10 ... Total $54.50× invoice: the same order shows the same amount due, $54.50✓ cart: $55.00 with no coupon ships free✓ invoice: $55.00 with no coupon✓ a basket that is exactly $50.00 after the coupon ships free✓ internals.test.js (2 tests)AssertionError: expected 'Total: $49.50' to be 'Total: $54.50'Tests 2 failed | 5 passed (7)The behavior tests catch the bug on both screens. The internals tests are green, because each checks a number the helper gets right in isolation. Already, one suite sees the bug and the other does not.
Step 2: the proper fix
Compute shipping from the discounted price:
export function orderTotal({ subtotalCents, coupon }) { const discount = coupon === 'SAVE10' ? Math.round(subtotalCents * 0.1) : 0; const items = subtotalCents - discount; const shipping = items >= 5000 ? 0 : 500; return items + shipping;}Tests 7 passed (7). Both screens correct, all green. The new boundary test also guards the line itself: changing >= 5000 to > 5000 makes it fail (run in the same scratch folder).
Step 3: the “tidy up” that breaks the other screen
Now the realistic part. Say you ask the AI to fix the cart and it also “simplifies” the shared function: the coupon is applied in the cart code, and orderTotal only prices what it is given. It updates renderCart to match. It forgets renderInvoice calls the same function.
export function orderTotal({ subtotalCents }) { const shipping = subtotalCents >= 5000 ? 0 : 500; return subtotalCents + shipping;}
export function renderCart(items, coupon) { const subtotalCents = sum(items); const discounted = coupon === 'SAVE10' ? subtotalCents - Math.round(subtotalCents * 0.1) : subtotalCents; return `Total: $${dollars(orderTotal({ subtotalCents: discounted }))}`;}
export function renderInvoice(items, coupon) { return `Amount due: $${dollars(orderTotal({ subtotalCents: sum(items) }))}`; // coupon silently dropped}The cart looks right in the preview, so you would not notice. Run the tests:
✓ cart: $55.00 with SAVE10 ... Total $54.50× invoice: the same order shows the same amount due, $54.50✓ cart: $55.00 with no coupon ships free✓ invoice: $55.00 with no coupon✓ a basket that is exactly $50.00 after the coupon ships free× orderTotal applies 10% for SAVE10✓ orderTotal adds 500 shipping under 5000AssertionError: expected 'Amount due: $55.00' to be 'Amount due: $54.50'AssertionError: expected 10000 to be 9000Tests 2 failed | 5 passed (7)The invoice test is red, with a message that points at the customer-visible problem: the invoice charges $55.00 where the cart said $54.50. That is the behavior test earning its place.
The internals test is red as well, but for a useless reason: the helper no longer applies coupons, because the AI moved the job. That test is not telling you the app is broken. It is telling you the code moved. And look at what it invites the AI to do next.
Step 4: the green that lies
The tempting move, for you or the AI, is the easy one: rewrite the internals test to match its new function:
it('orderTotal adds 500 shipping under 5000', () => { expect(orderTotal({ subtotalCents: 2000 })).toBe(2500);});it('orderTotal ships free at 5000 or more', () => { expect(orderTotal({ subtotalCents: 5000 })).toBe(5000);});Run that file alone: Tests 2 passed (2). If this were your only suite, you would ship, and every invoice with a coupon would overcharge. Run the behavior suite and the invoice test is still red: expected 'Amount due: $55.00' to be 'Amount due: $54.50'. Revert the tidy-up and all seven pass again.
That is the whole lesson. When the AI is allowed to edit the tests, tests that describe how the code works are the ones it will edit, because they are the ones that break when the code moves. Tests that describe what the user sees do not need to change when the code moves, so a failure in them means a real change in what the user gets.
How do I get behavior tests from an AI?
Write tests for what a user sees or receives, not for how the code works inside. Call the functions or screens the way the app does, and check the visible output: the text on screen, the invoice line, the saved record. Do not test private helpers directly and do not assert that a helper was called. Name each test as a full sentence with a concrete example number. When a change makes an existing test fail, do not edit the test: tell me which behavior changed and ask me.
Then add this check yourself. After any change to a shared function (anything several screens use), ask: “list every place that calls this and show me a test for each.” You can see the callers yourself with grep -rn "orderTotal(" .; if one has no behavior test, that is your gap.
How do I stop the AI editing tests without telling me?
Saying “do not edit the tests” in the prompt is a request. A commit guard is a rule. This commit-msg hook refuses any commit that touches a *.test.js file unless the message says [test-change]:
#!/bin/shif git diff --cached --name-only | grep -q '\.test\.js$' && ! grep -q '\[test-change\]' "$1"; then echo "Blocked: a test file changed. Add [test-change] to the commit message if that is intended." exit 1fiSave it as .githooks/commit-msg, then run chmod +x .githooks/commit-msg and git config core.hooksPath .githooks. In a scratch repo, a commit that changed a test file without the tag was refused, the same commit with [test-change] went through, and a code-only commit was untouched. It only stops commits made through git on your machine, so keep the habit of reading git diff --stat too.
Gotchas
- A behavior is not always a screen. In your app it may be an API response, a row saved in the database, or an email sent. The rule is the same: check what leaves the building.
- Do not ban helper tests. A pure function with tricky rules (taxes, dates) deserves its own tests. The trouble starts when helper tests are the only tests.
The one check an engineer does by hand
Break a shared function on purpose, such as dropping the coupon, and read which tests go red. If nothing user-facing fails, you have a gap. It is the sabotage check from tests for people who do not write code, aimed at the function with the most callers.
This is the missing piece in the bug-fix loop: the alarm that rings when a fix in one place changes another.
The Get AI Native ebook goes deeper on this. It is free, and the expected launch is around Christmas. Join the waitlist on the homepage.