Claude Code testing

Integration Tests Fail Randomly Due to Shared State

Your integration tests pass when run individually but fail randomly when run as a suite. The failures are non-deterministic: different tests fail on different runs, tests that were passing start failing after adding new tests, and the CI pipeline has become unreliable with intermittent failures.

Flaky tests are a serious productivity drain. Developers lose trust in the test suite, start ignoring failures ('it's just a flaky test'), and eventually stop running tests altogether. Real bugs hide behind the noise of flaky failures.

Claude Code generates integration tests that work in isolation but share database state, use hardcoded ports, rely on test ordering, or have timing dependencies that cause failures when tests run in parallel or in different orders.

Error Messages You Might See

Error: listen EADDRINUSE :::3000 Expected 1 row but found 3 rows Timeout: Async callback was not invoked within 5000ms Error: relation 'users' already exists 1 test passed, 1 test failed (different test each run)
Error: listen EADDRINUSE :::3000Expected 1 row but found 3 rowsTimeout: Async callback was not invoked within 5000msError: relation 'users' already exists1 test passed, 1 test failed (different test each run)

Common Causes

  • Shared database state between tests — One test creates data that another test doesn't expect, or a test deletes data another test needs
  • No database cleanup between tests — Tests don't reset the database to a known state before or after running
  • Hardcoded ports — Multiple test files try to start servers on the same port, causing EADDRINUSE errors
  • Test order dependency — Tests rely on being run in a specific order because earlier tests set up state that later tests need
  • Timing-dependent assertions — Tests use setTimeout or fixed delays that are long enough locally but too short in CI

How to Fix It

  1. Use database transactions for isolation — Wrap each test in a transaction that rolls back after the test completes, ensuring clean state
  2. Create fresh state in each test — Use beforeEach to set up exactly the data each test needs. Never rely on state from other tests
  3. Use random ports — Start test servers on port 0 (OS assigns a random available port) to avoid conflicts
  4. Randomize test order — Configure your test runner to randomize test execution order to expose hidden dependencies
  5. Replace timeouts with polling — Instead of await sleep(1000), poll for the expected condition with a timeout: await waitFor(() => expect(result).toBe(expected))
  6. Use testcontainers — Spin up isolated database instances per test suite using Docker containers

Real developers can help you.

BurnHavoc BurnHavoc Been around fixing other peoples code for 20 years. Alvin Voo Alvin Voo I’ve watched the tech landscape evolve over the last decade—from the structured days of Java Server Pages to the current "wild west" of Agentic-driven development. While AI can "vibe" a frontend into existence, I specialize in the architecture that keeps it from collapsing. My expertise lies in the critical backend infrastructure: the parts that must be fast, secure, and scalable. I thrive on high-pressure environments, such as when I had only three weeks to architect and launch an Ethereum redemption system with minimal prior crypto knowledge, turning it into a major revenue stream. What I bring to your project: Forensic Debugging: I don't just "patch" bugs; I use tools like Datadog and Explain Analyzers to map out bottlenecks and resolve root causes—like significantly reducing memory usage by optimizing complex DB joins. Full-Stack Context: Deep experience in Node.js and React, ensuring backends play perfectly with mobile and web teams. Sanity in the Age of AI: I bridge the gap between "best practices" and modern speed, ensuring your project isn't just built fast, but built to last. Dor Yaloz Dor Yaloz SW engineer with 6+ years of experience, I worked with React/Node/Python did projects with React+Capacitor.js for ios Supabase expert Pratik Pratik SWE with 15+ years of experience building and maintaining web apps and extensive BE infrastructure Sage Fulcher Sage Fulcher Hey I'm Sage! Im a Boston area software engineer who grew up in South Florida. Ive worked at a ton of cool places like a telehealth kidney care startup that took part in a billion dollar merger (Cricket health/Interwell health), a boutique design agency where I got to work on a ton of exciting startups including a photography education app, a collegiate Esports league and more (Philosophie), a data analytics as a service startup in Cambridge (MA) as well as at Phillips and MIT Lincoln Lab where I designed and developed novel network security visualizations and analytics. I've been writing code and furiously devoted to using computers to make people’s lives easier for about 17 years. My degree is in making computers make pretty lights and sounds. Outside of work I love hip hop, the Celtics, professional wrestling, magic the gathering, photography, drumming, and guitars (both making and playing them) Victor Denisov Victor Denisov Developer Matthew Butler Matthew Butler Systems Development Engineer @ Amazon Web Services Jen Jacobsen Jen Jacobsen I’m a Full-Stack Developer with over 10 years of experience building modern web and mobile applications. I enjoy working across the full product lifecycle — turning ideas into real, well-built products that are intuitive for users and scalable for businesses. I particularly enjoy building mobile apps, modern web platforms, and solving complex technical problems in a way that keeps systems clean, reliable, and easy to maintain. Antriksh Narang Antriksh Narang 5 years+ Experienced Dev (Specially in Web Development), can help in python, javascript, react, next.js and full stack web dev technologies. Daniel Vázquez Daniel Vázquez Software Engineer with over 10 years of experience on Startups, Government, big tech industry & consulting.

You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.

Get Help

Frequently Asked Questions

How do I isolate database state between integration tests?

The best approach is to wrap each test in a database transaction and roll it back after the test. Alternatively, use TRUNCATE on all tables in beforeEach, or use testcontainers to spin up a fresh database per test suite.

Why do my tests pass locally but fail in CI?

CI environments are typically slower and have less CPU. Timing-dependent tests that pass locally may timeout in CI. Replace fixed delays with polling/waitFor patterns and increase timeouts for CI environments.

Related Claude Code Issues

Can't fix it yourself?
Real developers can help.

You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.

Get Help