Windsurf testing

Test Database Not Resetting Between Tests in Windsurf Project

Tests in your Windsurf-generated project fail intermittently because the database retains data from previous test runs. Tests that pass individually fail when run together, test order matters, and adding a new test breaks existing ones. The test suite is unreliable and developers lose confidence in the results.

Cascade generates tests that create database records but doesn't clean them up afterward. Test A inserts a user, Test B expects an empty users table, and Test B fails because Test A's user is still there. The problem gets worse as more tests are added, creating an increasingly tangled web of data dependencies.

The failures are often non-deterministic — they depend on which tests run first, whether a previous test run completed fully, and sometimes even on timing. This makes them extremely hard to debug.

Error Messages You Might See

Expected 1 row but found 5 Error: duplicate key value violates unique constraint Test passed individually but fails in suite Error: relation has dependent rows - cannot truncate Expected empty array but received [previous test data]
Expected 1 row but found 5Error: duplicate key value violates unique constraintTest passed individually but fails in suiteError: relation has dependent rows - cannot truncateExpected empty array but received [previous test data]

Common Causes

  • No afterEach cleanup — Tests create database records but don't have afterEach hooks to clean up created data
  • Shared database between test runs — Tests use the development database instead of a dedicated test database that gets reset
  • No transaction rollback — Tests don't wrap operations in transactions that get rolled back, so changes persist
  • Truncation order wrong — Tables with foreign keys must be truncated in the correct order, or truncation fails silently
  • Seed data conflicts — Database seeds run before tests and create data with fixed IDs that conflict with test-created records
  • Parallel tests sharing state — Tests running in parallel write to the same tables and interfere with each other

How to Fix It

  1. Use transactions with rollback — Wrap each test in a database transaction and roll it back in afterEach. This is the fastest and cleanest approach
  2. Add truncation in beforeEach — Truncate all tables before each test with TRUNCATE TABLE ... CASCADE (PostgreSQL) or DELETE FROM (SQLite)
  3. Use a dedicated test database — Configure a separate database for tests in .env.test. Never run tests against your development or production database
  4. Install a test cleanup library — Use libraries like pg-mem (in-memory PostgreSQL), or prisma's test utils that handle cleanup automatically
  5. Isolate parallel tests — If running tests in parallel, use separate schemas or databases per worker to prevent interference
  6. Reset sequences and IDs — After truncation, reset auto-increment sequences so tests don't depend on specific ID values

Real developers can help you.

Caio Rodrigues Caio Rodrigues I'm a full-stack developer focused on building practical and scalable web applications. My main experience is with **React, TypeScript, and modern frontend architectures**, where I prioritize clean code, component reusability, and maintainable project structures. I have strong experience working with **dynamic forms, state management (Redux / React Hook Form), and complex data-driven interfaces**. I enjoy solving real-world problems by turning ideas into reliable software that companies can actually use in their daily operations. Beyond coding, I care about **software quality and architecture**, following best practices for componentization, code organization, and performance optimization. I'm also comfortable working across the stack when needed, integrating APIs, handling business logic, and helping transform prototypes into production-ready systems. My goal is always to deliver solutions that are **simple, efficient, and genuinely useful for the people using them.** Milan Surelia Milan Surelia Milan Surelia is a Mobile App Developer with 5+ years of experience crafting scalable, cross-platform apps at 7Span and Meticha. At 7Span, he engineers feature-rich Flutter apps with smooth performance and modern UI. As the Co-Founder of Meticha, he builds open-source tools and developer-focused products that solve real-world problems. Expertise: 💡 Developing cross-platform apps using Flutter, Dart, and Jetpack Compose for Android, iOS, and Web. 🖋️ Sharing insights through technical writing, blogging, and open-source contributions. 🤝 Collaborating closely with designers, PMs, and developers to build seamless mobile experiences. Notable Achievements: 🎯 Revamped the Vepaar app into Vepaar Store & CRM with a 2x performance boost and smoother UX. 🚀 Launched Compose101 — a Jetpack Compose starter kit to speed up Android development. 🌟 Open source contributions on Github & StackOverflow for Flutter & Dart 🎖️ Worked on improving app performance and user experience with smart solutions. Milan is always happy to connect, work on new ideas, and explore the latest in technology. 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. Jared Hasson Jared Hasson Full time lead founding dev at a cyber security saas startup, with 10 yoe and a bachelor's in CS. Building & debugging software products is what I've spent my time on for forever 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 Omar Faruk Omar Faruk As a Product Engineer at Klasio, I contributed to end-to-end product development, focusing on scalability, performance, and user experience. My work spanned building and refining core features, developing dynamic website templates, integrating secure and reliable payment gateways, and optimizing the overall system architecture. I played a key role in creating a scalable and maintainable platform to support educators and learners globally. I'm enthusiastic about embracing new challenges and making meaningful contributions. PawelPloszaj PawelPloszaj I'm fronted developer with 10+ years of experience with big projects. I have small backend background too Pratik Pratik SWE with 15+ years of experience building and maintaining web apps and extensive BE infrastructure Kingsley Omage Kingsley Omage Fullstack software engineer passionate about AI Agents, blockchain, LLMs. Costea Adrian Costea Adrian Embedded Engineer specilizing in perception systems. Latest project was a adas camera calibration system.

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

Should I use truncation or transaction rollback for test cleanup?

Transaction rollback is faster and cleaner — each test runs in a transaction that gets rolled back, leaving no trace. However, it doesn't work if your code commits transactions internally. Truncation is simpler but slower because it actually deletes and recreates data.

Why do my tests pass individually but fail together?

This is a classic sign of missing test isolation. One test creates data that another test doesn't expect. Add cleanup (truncation or rollback) in beforeEach or afterEach hooks so each test starts with a known database state.

Related Windsurf 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