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.
npm install --save-dev mswMSW is the modern JavaScript equivalent of VCR, providing powerful HTTP request interception and mocking.
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 responsesempty: Returns empty data arrayserror: Returns 404 error responsesnetworkError: Simulates network failures
- Mock Server: Configurable server that can switch between scenarios
- Helper Functions: Easy-to-use functions to change mock scenarios during tests
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
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
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();
});success: Returns HTTP 200 with mock application dataempty: Returns HTTP 200 with empty arrayserror: Returns HTTP 404 with error messagesnetworkError: Simulates network connectivity issues
- No real HTTP calls during tests
- Predictable, repeatable responses
- Fast test execution
- Tests the actual HTTP layer
- Catches network-related bugs
- Tests error handling paths
- Centralized mock definitions
- Simple scenario switching
- Clear separation of test data
- Tests run offline
- No external dependencies
- Consistent across environments
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)
- Record Mode: Add ability to record real HTTP responses and replay them
- Response Templating: Dynamic response generation based on request parameters
- Request Assertions: Verify request content, headers, and parameters
- Cassette Files: Store mock responses in JSON files like VCR