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:

.github/workflows/test.yml
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:e2e

Environment-Based Configuration

Use environment variables to switch between real and mock APIs:

config/api.ts
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)

POST/payments/charge
{
  "id": "ch_{{$randomUUID}}",
  "status": "succeeded",
  "amount": "{{request.body.amount}}",
  "currency": "{{request.body.currency}}",
  "created": "{{$now}}"
}

Email Service (SendGrid-like)

POST/email/send
{
  "messageId": "msg_{{$randomUUID}}",
  "status": "queued",
  "to": "{{request.body.to}}"
}

Testing Error Scenarios

Create dedicated error endpoints for testing failure handling:

  • /payments/charge/declined returns payment declined error
  • /payments/charge/timeout has 30-second delay
  • /payments/charge/500 returns server error

Your test suite can hit these specific endpoints to verify error handling.

Speed Comparison

MetricReal APIsMockStation
Response time200-2000ms10-50ms
Reliability95-99%99.9%
Rate limitsOften hitGenerous
Cost per call$0.01-0.10Included

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:

  1. Create a free account
  2. Create a project for your CI environment
  3. Add mock endpoints for external services
  4. Update your CI config with mock URLs
  5. Watch your tests become reliable

Stabilize Your Pipeline

Create reliable mock APIs that make your CI/CD tests deterministic.

Get Started FreeNo credit card required