API Tests Hitting Real Endpoints in Windsurf Project
Tests in your Windsurf-generated project are making real HTTP requests to external APIs instead of using mocks. This causes tests to be slow, flaky, expensive (burning API credits), and destructive (creating real data in third-party services). Password reset emails get sent to real users during tests, real charges appear on payment processors, and tests fail when external APIs are down.
Cascade generates test files that set up the test scenario but doesn't intercept outbound HTTP requests. The test code calls your app's functions, which in turn call real Stripe, SendGrid, OpenAI, or other APIs. In development this may go unnoticed because the API keys are for test/sandbox environments, but it still makes tests slow and unreliable.
The problem becomes critical when tests run in CI/CD pipelines where they execute on every push, rapidly consuming API quotas and creating garbage data in external services.
Error Messages You Might See
Common Causes
- No HTTP interception library — The test suite doesn't use nock, msw, or similar tools to intercept outbound HTTP requests
- Service layer not abstracted — External API calls are made directly in business logic instead of through injectable service classes
- Environment variables pointing to real APIs — Test environment uses real API keys instead of test/mock keys
- Missing test environment configuration — No .env.test file or NODE_ENV=test check to switch to mock implementations
- Mocks set up but not activated — HTTP mocking library is installed but the mocks aren't started before tests or cleaned up after
How to Fix It
- Install MSW (Mock Service Worker) — Set up msw to intercept all outbound HTTP requests in tests. Define handlers that return predetermined responses for each external API
- Create a .env.test file — Use fake or empty API keys in test configuration so even if mocks fail, real APIs aren't called
- Add a global test setup — In jest.setup.ts or a beforeAll, start the MSW server. In afterAll, close it. In afterEach, reset handlers to default
- Use nock for specific API mocking — For precise control, use nock to mock individual HTTP endpoints and assert they were called with expected parameters
- Fail tests on unmocked requests — Configure MSW or nock to throw an error on any unhandled HTTP request, making it impossible for tests to hit real APIs
- Abstract external services — Create wrapper classes for each external API and inject mock implementations in tests
Real developers can help you.
You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.
Get HelpFrequently Asked Questions
What is the best way to mock HTTP requests in tests?
Use MSW (Mock Service Worker) for broad HTTP interception — it works at the network level and catches all requests. Use nock for precise assertions about specific API calls. Both can be configured to fail on unmocked requests.
Should I use Stripe test mode or mock Stripe entirely?
For unit tests, mock Stripe entirely with MSW or nock for speed and reliability. For integration tests, use Stripe's test mode with test API keys. Never use live mode in any test environment.