Everyday Programmer
Work with Jimmy
AI Workflows

The Best AI Coding Prompt Might Be a Failing Test

Before asking a coding agent to fix a bug, make sure the regression test can see it. A small search example makes the red, green, and restored-defect checks concrete.

6 min readAI-assisted development, Regression testing, Frontend engineering, JavaScript
Isometric test checkpoint on a dark grid. An amber barrier stops a circuit module with a missing square connector, while a repaired teal module passes the same geometric test and reaches the output platform.

“Fix the search” leaves a coding agent with a lot of room to be helpful in the wrong direction. It could rewrite the component, add a debounce, change the matching rules, or make the empty state look better. None of that necessarily fixes the reported behavior.

A failing regression test gives the task a more useful starting point: a specific action, a specific expected result, and a way to observe the difference.

The important part is seeing the test fail before asking the agent to make it pass. Otherwise, a passing test might describe behavior the code already had, or miss the defect entirely.

Consider a small product-list example. This is a constructed demonstration, not a production incident.

Start with the sequence that loses the data

The list contains a desk lamp, a desk mat, and a monitor arm. Searching for “lamp” correctly leaves one result. Clear the search, and only the lamp remains. The other products do not come back.

The requirement is simple: clearing the query restores the original products in their original order. A new query must also search the original collection, not whatever the previous query happened to leave behind.

The bug lives in a state transition that overwrites the source collection with the filtered result:

search.broken.mjs
javascript

The first search appears to work. It also discards the data needed for the next one. Testing only that first search would reward the broken implementation.

Make the intended failure visible

Here is the focused regression test from the companion example. The companion files use Node’s built-in test runner, so the example does not need a test framework installation. Node documents running tests with the --test flag.

search.test.mjs
javascript

Run the test against the broken version first. The last assertion should fail: the actual result contains only lamp, while the expected result contains all three IDs.

Read that failure. An import error, a missing fixture, or a test that never ran is not evidence that the behavior has been reproduced. Nor is an assertion that fails because its expected result was guessed incorrectly.

If the test passes on the broken version, stop. Check whether it reaches the relevant implementation and exercises the full sequence. Do not change production code to satisfy a test that cannot see the reported bug.

Give the agent a bounded repair

Now the prompt can be specific without dictating every implementation detail:

“Reproduce the clear-query failure in search.test.mjs before changing search.mjs. Preserve the source product collection, original order, and current matching rules. Make the smallest repair. Keep the existing regression assertions intact; if an assertion conflicts with the requirement, explain the conflict before changing it. Do not add dependencies or refactor unrelated code. Report the command and relevant failure before the fix, then the commands and results after it.”

This still requires judgment. Review the test and the requirement yourself. A coding agent can produce a plausible test for the wrong contract, even when the code looks reasonable.

Keeping the expected behavior intact is different from declaring tests untouchable. Requirements can be wrong or incomplete. What matters is making a changed expectation an explicit decision instead of quietly weakening an assertion until the run turns green.

Keep the source collection and derive the view

For this example, the repair is one line in setQuery. The complete repaired search.mjs is:

search.mjs
javascript

The state retains the source products and current query. visibleProducts calculates the result from those values whenever it is called. In a React component, the same design usually means storing the query and source data, then deriving the filtered rows during rendering. React’s guidance uses a filtered-list example to explain why redundant derived state often does not need an Effect. You Might Not Need an Effect.

This recommendation assumes a local collection that the client already has. Server-side search and paginated data have different contracts. Clearing a query cannot restore records that were never loaded.

The companion suite also checks that replacing “lamp” with “monitor” finds the monitor arm, and that the transitions leave the caller’s collection and previous state intact. Those checks matter because a special-case reset for an empty query could pass the first test while leaving ordinary query changes broken.

Run the unchanged tests after the repair. Then review the implementation diff. A green result should not hide a removed assertion, a skipped test, a changed fixture, or a wider rewrite than the task needed.

Put the defect back once

There is one more useful check before trusting the regression: temporarily run the same test against the retained broken implementation in an isolated copy.

It should fail again at the clear-query assertion. The example package does this without modifying the repaired file. That gives you three observations: the original defect fails, the repair passes, and the restored defect fails.

On Node v24.22.1, the focused broken-case run failed at the expected assertion, all three companion tests passed after the repair, and the restored defect failed again. The test file was unchanged between the original failing and passing runs. From the companion directory, run node --test search.test.mjs for the repaired suite and node verify-control.mjs for the isolated defect check.

This is a small manual mutation check. Mutation testing changes implementation behavior and observes whether tests detect the change; Stryker’s introduction explains the approach. One deliberately restored defect does not establish comprehensive mutation coverage. It answers a narrower, valuable question: would this particular regression test notice if this particular bug came back?

Connect the model back to the UI

The example validates a JavaScript state model. It does not prove that a real search input calls setQuery, that the component renders visibleProducts, or that the clear button is usable.

For a real application, repeat the sequence through its existing component or browser tests: search, clear, check the visible rows, then try a different query. Test through user-visible behavior rather than a private state shape. Testing Library’s guiding principles describe that distinction.

Then run the repository’s relevant checks and inspect the resulting interface. The broader release decision belongs to a different question, covered in A Green Test Suite Is Not a Ship Decision.

The useful handoff to a coding agent is a behavior it can reproduce and a boundary it should preserve. Before asking it to fix the bug, make sure the test can see the bug.

Download the runnable JavaScript example (ZIP, includes the tests and retained defect control).