autohand "These tests are failing. Read the test output, identify the root cause, and fix the code. Do not modify the tests unless they have a bug."

What you'll learn

  • How to capture and share test output so the agent can trace failures to their source
  • How to instruct the agent to fix the code rather than change the test assertions
  • How to review a targeted fix and verify the full suite still passes
  • How to ask the agent to scan for the same bug pattern across the codebase

Before you start

  • Autohand Code installed
  • A failing test suite with visible error output
  • A test runner accessible from the command line (npm test or equivalent)
  • The file path of the failing test, if it lives in a deeply nested module

Capture the test output

Run your test suite and copy the full output. Do not trim it. The stack trace and the diff between expected and received values are both important.

bash

npm test 2>&1 | tee test-output.txt

Piping to a file lets you share the complete output without scrolling back through your terminal. The output will look something like this.

text

FAIL src/services/order.test.js
  OrderService
    createOrder
      x calculates the total correctly (12 ms)

  ● OrderService > createOrder > calculates the total correctly

    expect(received).toBe(expected)

    Expected: 110
    Received: 100

      47 |   it('calculates the total correctly', () => {
      48 |     const order = createOrder([{ price: 100, quantity: 1 }], { taxRate: 0.1 });
    > 49 |     expect(order.total).toBe(110);
         |                         ^
      50 |   });

      at Object.toBe (src/services/order.test.js:49:25)

Test Suites: 1 failed, 4 passed, 5 total
Tests:       1 failed, 23 passed, 24 total

Provide context to the agent

Start an Autohand session and paste the failing output directly into the prompt. Use the instruction from the prompt block above so the agent knows the rules of engagement.

bash

autohand "These tests are failing. Read the test output, identify the root cause, and fix the code. Do not modify the tests unless they have a bug.

FAIL src/services/order.test.js
  OrderService > createOrder > calculates the total correctly

  Expected: 110
  Received: 100

  at Object.toBe (src/services/order.test.js:49:25)"

The instruction "do not modify the tests unless they have a bug" is important. Without it, the agent might take the path of least resistance and change the expected value to match the current broken behavior. That would make the test pass but hide the bug.

Tip: If you know the test is wrong and that is what you want fixed, say so explicitly: "The test expected value is wrong. Fix the test to match the correct behavior." Be direct about which side of the assertion needs to change.

Watch the debugging process

The agent follows a systematic approach rather than guessing. Here is what you will see it do.

First, it reads the test file to understand what the test is asserting.

text

Reading: src/services/order.test.js
Reading: src/services/order.js

Then it locates the function under test and reads its implementation.

javascript

// What the agent finds in order.js
function createOrder(items, options = {}) {
  const subtotal = items.reduce((sum, item) => sum + item.price * item.quantity, 0);
  // Bug: tax is calculated but never added to the total
  const tax = subtotal * (options.taxRate || 0);
  return { subtotal, tax, total: subtotal };
}

It identifies that tax is calculated but left out of total. It then shows you the fix before writing it.

text

Root cause: createOrder calculates tax correctly but returns subtotal
as total instead of subtotal + tax.

Fix: change the return statement in src/services/order.js line 8
from { subtotal, tax, total: subtotal }
to   { subtotal, tax, total: subtotal + tax }

It applies the fix and runs the test suite to confirm the failure is resolved.

Review the fix

Always look at what the agent changed before moving on. The fix should be minimal and targeted. A good fix changes exactly what caused the failure and nothing else.

javascript

// Before
function createOrder(items, options = {}) {
  const subtotal = items.reduce((sum, item) => sum + item.price * item.quantity, 0);
  const tax = subtotal * (options.taxRate || 0);
  return { subtotal, tax, total: subtotal };
}

// After
function createOrder(items, options = {}) {
  const subtotal = items.reduce((sum, item) => sum + item.price * item.quantity, 0);
  const tax = subtotal * (options.taxRate || 0);
  return { subtotal, tax, total: subtotal + tax };
}

If the fix looks larger than expected, ask the agent to explain every change it made. Understand each one before accepting.

bash

autohand "Walk me through every line you changed and why each change was necessary."

Verify all tests pass

After the fix, run the full test suite. A targeted fix should not break other tests. If it does, you have uncovered a wider problem.

bash

npm test

If a different test now fails that was passing before, paste the new failure into the session and continue. The agent has context from the previous fix and can see whether the new failure is related.

bash

autohand "The first fix worked. Now a different test is failing. Here is the new output:

FAIL src/services/invoice.test.js
  InvoiceService > generateInvoice > includes the correct tax amount

  Expected: 10
  Received: 0"

Learn from the pattern

After the tests are green, ask the agent to summarize what it found. This is useful for team retrospectives and for updating your code review checklist.

bash

autohand "Summarize the root cause of the failures we just fixed. What pattern should I add to my code review checklist to catch this earlier next time?"

Common patterns the agent identifies include return statements that compute a value but use the wrong variable, off-by-one errors in loops, missing await on async calls in synchronous-looking code, and mutation of function arguments that other tests depend on being unchanged.

You can also ask the agent to scan for similar patterns elsewhere in the codebase before they turn into future failures.

bash

autohand "Search the rest of the codebase for the same pattern where a calculated value is returned incorrectly. List any other places this might happen."

Tip: Proactively finding related bugs after fixing one is one of the highest-value things Autohand can do during a debugging session. The agent already has context about what went wrong and can apply that understanding across the whole codebase in seconds.

What you learned

  • You captured test output and provided it to the agent with clear instructions about what should change
  • You watched the agent trace a failure from assertion to root cause and apply a minimal fix
  • You verified the full test suite passes after the fix without regressions
  • You asked the agent to scan for the same bug pattern elsewhere in the codebase
Try next autohand "Generate unit tests for the functions in src/services/ that have no test coverage yet. Include edge cases."