Skip to content

Latest commit

 

History

History
126 lines (85 loc) · 3.45 KB

File metadata and controls

126 lines (85 loc) · 3.45 KB

HTTP Mocking Implementation - VCR-like Testing

Overview

I've implemented HTTP mocking similar to the VCR Ruby library for your TypeScript SDK tests. This replaces real HTTP calls with controlled, repeatable mock responses.

Changes Made

1. Added MSW (Mock Service Worker)

npm install --save-dev msw

MSW is the modern JavaScript equivalent of VCR, providing powerful HTTP request interception and mocking.

2. Created HTTP Mock Utilities

File: src/test-utils/http-mocks.ts

  • Mock Responses: Predefined JSON responses for different scenarios (success, empty, error)
  • Request Handlers: HTTP handlers for different test scenarios:
    • success: Returns successful API responses
    • empty: Returns empty data arrays
    • error: Returns 404 error responses
    • networkError: Simulates network failures
  • Mock Server: Configurable server that can switch between scenarios
  • Helper Functions: Easy-to-use functions to change mock scenarios during tests

3. Vitest Configuration

Files: vitest.setup.ts and vitest.config.ts

  • Global MSW server setup that starts before all tests
  • Automatic handler reset after each test for isolation
  • Proper cleanup after all tests complete

4. Updated Test Files

SDK Tests (src/index.test.ts)

  • Removed real HTTP calls to external APIs
  • Uses setMockScenario() to control HTTP responses
  • Tests both success and failure scenarios with predictable responses

CLI Function Tests (src/cli/cli-functions.test.ts)

  • Replaced SDK mocking with HTTP-level mocking
  • More realistic testing of the actual network layer
  • Tests multiple scenarios: success, empty responses, errors, network failures

Usage Examples

Basic Usage

import { setMockScenario } from './test-utils/http-mocks';

it('should handle successful API response', async () => {
  // Set up successful HTTP responses
  setMockScenario('success');

  const result = await sdk.testConnection();
  expect(result).toBe(true);
});

it('should handle API errors', async () => {
  // Set up error HTTP responses
  setMockScenario('error');

  await expect(sdk.testConnection()).rejects.toThrow();
});

Available Mock Scenarios

  • success: Returns HTTP 200 with mock application data
  • empty: Returns HTTP 200 with empty arrays
  • error: Returns HTTP 404 with error messages
  • networkError: Simulates network connectivity issues

Benefits

1. VCR-like Behavior

  • No real HTTP calls during tests
  • Predictable, repeatable responses
  • Fast test execution

2. Realistic Testing

  • Tests the actual HTTP layer
  • Catches network-related bugs
  • Tests error handling paths

3. Easy Maintenance

  • Centralized mock definitions
  • Simple scenario switching
  • Clear separation of test data

4. Developer Experience

  • Tests run offline
  • No external dependencies
  • Consistent across environments

Test Results

All tests now pass without making real HTTP calls:

✓ src/cli/cli.test.ts (6 tests) 36ms
✓ src/index.test.ts (5 tests) 65ms
✓ src/cli/cli-functions.test.ts (7 tests) 89ms

Test Files  3 passed (3)
Tests  18 passed (18)

Future Enhancements

  1. Record Mode: Add ability to record real HTTP responses and replay them
  2. Response Templating: Dynamic response generation based on request parameters
  3. Request Assertions: Verify request content, headers, and parameters
  4. Cassette Files: Store mock responses in JSON files like VCR