Skip to content
Back to AI Coding

Fixing Failing Tests with AI

Decide whether the test or the code is wrong, then fix the right side.

Debugging & FixingIntermediate

Why

Stack Overflow's 2024 survey found 76% of developers use or plan AI tools, with 46% of non-users interested in using AI for testing and debugging code. A red test has two possible causes: the code is wrong or the test is wrong (stale, flaky, wrong expectation).

AI can read both and tell you which to fix, avoiding the trap of 'fixing' the test to hide a real bug.

When

Use on any non-obvious test failure. Skip for clearly flaky timing tests.

How

1. Paste the failing test + the code under test. 2. Ask: "is the test or the code wrong? explain; then fix the right side". 3. If the test is stale (API changed): update the expectation. 4. If the code is wrong: fix the code, keep the test. 5. Re-run; commit only the side that changed.

Example

Test: expect(parsePage("3")).toBe(3) — fails, got 1. Code: parsePage returns 1 for any input (bug). AI: "code is wrong — parsePage ignores input; fix the code, keep the test." Fix parsePage to actually parse; test goes green; commit fix: parsePage ignores input.

Common Problems

  • ⚠TypeError: Cannot read properties of undefined (reading 'x') → add optional chaining `obj?.x` and a null check; trace via the stack trace.
  • ⚠Error: ENOTFOUND / ECONNREFUSED → check `ping <host>` and proxy settings; common in corporate networks behind a proxy.
  • ⚠Error: "Maximum call stack size exceeded" → look for infinite recursion; add a base case or depth guard and test with a small input first.

Best Practices

  • ✓Always determine test-vs-code fault before fixing.
  • ✓Keep tests as the source of truth; fix code unless the test is genuinely stale.
  • ✓Measure the outcome with a concrete metric (time saved, defect rate, test coverage) so the practice can be justified and improved.

FAQ

What is Fixing Failing Tests with AI?

Decide whether the test or the code is wrong, then fix the right side. It helps developers quickly grasp the core value and use cases of Fixing Failing Tests with AI.

How do I get started with Fixing Failing Tests with AI?

To get started with Fixing Failing Tests with AI, focus on these points first: Paste the failing test + the code under test.

What are common mistakes with Fixing Failing Tests with AI?

Common mistakes when using Fixing Failing Tests with AI include: TypeError: Cannot read properties of undefined (reading 'x') → add optional chaining `obj?.x` and a null check; trace via the stack trace.; Error: ENOTFOUND / ECONNREFUSED → check `ping <host>` and proxy settings; common in corporate networks behind a proxy.; Error: "Maximum call stack size exceeded" → look for infinite recursion; add a base case or depth guard and test with a small input first..

What are the best practices for Fixing Failing Tests with AI?

Best practices for Fixing Failing Tests with AI include: Always determine test-vs-code fault before fixing.; Keep tests as the source of truth; fix code unless the test is genuinely stale.; Measure the outcome with a concrete metric (time saved, defect rate, test coverage) so the practice can be justified and improved..

How do I learn more about Fixing Failing Tests with AI?

Start with the official docs, then search real-world examples on GitHub. Validate patterns in a small scratch repo before applying them to production code.