Skip to content
Esc
↑↓navigate↵open⌘Jpreview
Start
On this page

Fix Failing Tests with Debugging Agent

Use Autohand's debugging workflow to diagnose and fix failing tests systematically. The agent reads the error output, traces it to the source, and fixes the right thing.

Intermediate · 15 min

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.

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.

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.

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.

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.

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

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

// 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.

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.

// 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.

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.

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.

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.

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.

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."

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
autohand "Generate unit tests for the functions in src/services/ that have no test coverage yet. Include edge cases."

Last updated on

Was this helpful?

Send Feedback

Help us improve this documentation page. Your feedback is valuable.

We'll only contact you if we need clarification