Dashboard

AI-Built App Passes Tests but Breaks for Users

When an AI-built app passes tests but breaks for users, the suite is testing the code's assumptions. Four gaps AI test generation leaves, and the fix.

Carlo Zuercher
Carlo Zuercher
Staff Engineer, Platform
9 October 20261 min read

A green test suite and a broken product is not a paradox. When an AI-built app passes tests but breaks for users, it is usually because the same model wrote both the code and the tests, in the same session, from the same mental model. The tests encode the assumptions the implementation already makes. Anything the model did not think of is absent from both, and absence is invisible.

Four gaps account for most of it. Each has a specific check.

Gap 1: the tests assert what the code does

Ask a model to add tests to code it just wrote and it reads the implementation, works out the behaviour, and asserts that behaviour. If the function returns null for a missing record when it should throw, the test asserts null. The suite is now a faithful description of a bug.

This is not laziness. It is the only thing it can do without a specification, and you did not give it one.

The check. Pick your three most important functions. For each, write down what it should do, in a sentence, without looking at the code. Then read the test. If the test matches the code and not your sentence, the test is worthless and so is the coverage number attached to it.

The fix. Write the assertions before the implementation, or at least write the expected behaviour in prose and have the model generate tests from that text rather than from the code. Using AI to write tests that catch bugs covers the prompting order that produces tests worth having.

Gap 2: the mock covers the thing that breaks

Generated tests mock external dependencies aggressively, which is reasonable for speed and disastrous for coverage of the dependency boundary, because that boundary is where production failures concentrate.

javascript
// Generated test: the payment provider always behaves
jest.mock('./payments', () => ({
  charge: jest.fn().mockResolvedValue({ status: 'succeeded', id: 'ch_1' })
}))

Every test passes. In production the provider returns a requires_action status for 3D Secure, times out under load, or returns a duplicate charge on retry, and none of those paths has ever executed.

The check. Search your test files for mock definitions and list what each one always returns. Anything that only ever returns success is hiding a code path.

The fix. For each mock, add at least one test where it fails in the way that dependency actually fails: a timeout, a rate limit, a malformed response, a partial success. You are not testing the dependency, you are testing that your code does something sensible when it misbehaves.

Gap 3: nothing runs concurrently

Generated test suites run one operation at a time against a clean database. Real users click twice, submit a form while the first submission is in flight, and open two tabs.

The classic result is a double booking or a duplicate order, from code that reads, checks and writes without a constraint or a transaction guarding the gap between the read and the write.

The check. Find every place the code reads a value, decides something from it, and then writes. Ask whether two requests arriving in the same hundred milliseconds would both pass the check. If yes, and the consequence is a duplicate record or a negative balance, that is a production bug with a green test beside it.

The fix. Push the invariant into the database where it belongs, with a unique constraint or a transaction at the right isolation level, rather than into application logic a second concurrent request will not see. Then write the test that fires two requests at once and asserts exactly one succeeds.

Gap 4: the empty state never existed

Models generate test fixtures that are plausible and populated: a user with three orders, a project with tasks, a list with items. The states that break interfaces are the ones with nothing in them, or far too much.

State

What generated tests usually cover

What breaks in production

Zero records

Rarely

Layout collapse, divide by zero in an average, "undefined" rendered

One record

Sometimes

Pluralisation, a chart with a single point

Very long strings

No

Overflow, truncated database writes, broken layout

Thousands of records

No

Timeout, unpaginated query, browser freeze

Unicode and emoji in names

No

Encoding errors, byte-length truncation mid-character

The check. Load each main screen with an account that has no data at all. Most AI-built apps have at least one screen that has never been seen in that state, because the developer seeded data on day one and never cleared it.

The fix. Add the empty case to every list view test, and seed one deliberately hostile fixture: a 500-character name, an apostrophe, a non-Latin script. Prompting AI to generate edge cases for testing is good at producing these once you ask for them specifically, which is the part that does not happen by default.

Why coverage says an AI-built app passes tests but breaks

Line coverage measures which lines executed, not whether the assertions meant anything. A suite that calls every function and asserts the values the implementation happens to return can report 90 percent and detect nothing. Coverage is a floor, not evidence.

A more useful signal: break something deliberately. Change a comparison operator, flip a boolean default, return an empty array where the code returns data. Run the suite. If it stays green, you now know what your tests are worth.

Where tests start failing after an agent touches them rather than after you touch the code, the cause is usually different and is covered in why an AI coding agent keeps rewriting your tests. For failures that only appear once the app is live, what to do when an AI-built app breaks in production deals with the response, and staging and production for an AI-built app with the environment that catches them earlier. The broader practice of mutating code to grade a test suite has a name, mutation testing, and the Google Testing Blog's post on it is a reasonable starting point.

FAQ

Why do AI-generated tests pass when the app is broken?

Because the model usually writes the tests from the implementation rather than from a specification, so the assertions describe the behaviour that exists, including the incorrect parts of it.

Is it better to write tests before asking AI to write the code?

Generally yes. Giving the model the expected behaviour first means it has a target other than its own output, and the tests then serve as a specification rather than a description.

How much test coverage should an AI-built app have?

The number matters less than what the tests assert. A suite covering 60 percent of lines with meaningful assertions about failure paths is worth more than 95 percent of lines asserting that functions return what they return.

What is the fastest way to find out if my tests are any good?

Deliberately introduce a bug, run the suite, and see whether it fails. Do that three or four times in different parts of the code and you will have a realistic picture within half an hour.

How did this land?

About the author

Carlo Zuercher
Carlo Zuercher

Staff Engineer, Platform

Carlo works on the platform that turns prompts into running apps. He writes the engineering deep dives and the changelog notes worth reading.

Share

Get the next post in your inbox

One email a month. Product updates, engineering posts, and the best of Built with Swarmz.

I agree to receive emails about AI building tips and Swarmz product news. Unsubscribe any time.