API Mocking for CI/CD Pipelines
Eliminate flaky tests and speed up your pipeline with deterministic mock APIs.
Flaky tests are the bane of CI/CD pipelines. External API dependencies make tests slow and unreliable. MockStation provides stable, fast mock APIs that your pipeline can depend on.
The Flaky Test Problem
Tests that depend on external APIs fail for reasons outside your control:
- External service downtime: Your tests fail because Stripe is having issues
- Rate limiting: Too many test runs hit API limits
- Network latency: Slow responses cause timeouts
- Data changes: External data changes break assertions
- Cost: Each test run costs money (payment APIs)
The Impact
A 10% flaky test rate means 1 in 10 builds fail randomly. Teams lose trust in the pipeline. Developers start ignoring failures. Real bugs slip through.
The Solution: Mock External APIs
Replace external API calls with MockStation endpoints:
Fast
Mock responses in milliseconds, not seconds
Reliable
No external dependencies to fail
Deterministic
Same request, same response, every time
Always Available
99.9% uptime, no maintenance windows
GitHub Actions Example
Here's how to use MockStation in a GitHub Actions workflow:
name: Test Suite
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
env:
# Point to MockStation instead of real APIs
API_BASE_URL: https://api.mockstation.io/v1/your-project
PAYMENT_API_URL: https://api.mockstation.io/v1/your-project/payments
steps:
- uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Run E2E tests
run: npm run test:e2eEnvironment-Based Configuration
Use environment variables to switch between real and mock APIs:
const config = {
development: {
apiUrl: 'https://api.mockstation.io/v1/your-project',
},
test: {
apiUrl: 'https://api.mockstation.io/v1/your-project',
},
staging: {
apiUrl: 'https://staging-api.yourcompany.com',
},
production: {
apiUrl: 'https://api.yourcompany.com',
},
};
export const API_URL = config[process.env.NODE_ENV]?.apiUrl
|| config.development.apiUrl;Mock Third-Party Services
Create mock endpoints for all external services your app depends on:
Payment Gateway (Stripe-like)
{
"id": "ch_{{$randomUUID}}",
"status": "succeeded",
"amount": "{{request.body.amount}}",
"currency": "{{request.body.currency}}",
"created": "{{$now}}"
}Email Service (SendGrid-like)
{
"messageId": "msg_{{$randomUUID}}",
"status": "queued",
"to": "{{request.body.to}}"
}Testing Error Scenarios
Create dedicated error endpoints for testing failure handling:
/payments/charge/declinedreturns payment declined error/payments/charge/timeouthas 30-second delay/payments/charge/500returns server error
Your test suite can hit these specific endpoints to verify error handling.
Speed Comparison
| Metric | Real APIs | MockStation |
|---|---|---|
| Response time | 200-2000ms | 10-50ms |
| Reliability | 95-99% | 99.9% |
| Rate limits | Often hit | Generous |
| Cost per call | $0.01-0.10 | Included |
Best Practices
- Use environment variables: Never hardcode mock URLs
- Mirror real API structure: Mock responses should match production
- Test both success and failure: Create endpoints for each scenario
- Run integration tests against real APIs: Mocks for CI, real APIs for staging
Get Started
Set up your CI/CD mocks in minutes:
- Create a free account
- Create a project for your CI environment
- Add mock endpoints for external services
- Update your CI config with mock URLs
- Watch your tests become reliable
Stabilize Your Pipeline
Create reliable mock APIs that make your CI/CD tests deterministic.