TypeScript SDK for Test Server

0.2.9 · active · verified Sun Apr 19

The `test-server-sdk` is a TypeScript SDK designed to provide client-side interaction with a companion `test-server` for robust HTTP and WebSocket request recording and replaying. This package facilitates deterministic and isolated testing by allowing developers to capture network traffic and simulate server responses. It's currently on version 0.2.9 and exhibits a rapid release cadence with frequent minor updates, focusing on new features and bug fixes. Key functionalities include recording and replaying HTTP and WebSocket requests, supporting dynamic subdirectory recording paths, streaming capabilities, and redacting sensitive information from response bodies. A notable differentiator is its ability to manage recording formats, which was updated to a JSON serializable format in v0.2.6, enhancing interoperability and storage. This SDK is a core component for integrating `test-server` capabilities into TypeScript-based test environments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize, start, and interact with the `TestServer` in `Record` mode to capture HTTP requests. It sets up a local directory for recordings, sends a sample request through the proxy, and ensures the server is properly shut down.

import { TestServer, RecordingMode, TestServerOptions } from 'test-server-sdk';
import fetch from 'node-fetch'; // For example HTTP client
import * as path from 'path';
import * as fs from 'fs';

async function runTestScenario() {
  const recordingsDir = path.join(__dirname, 'recordings');
  if (!fs.existsSync(recordingsDir)) {
    fs.mkdirSync(recordingsDir);
  }

  const serverOptions: TestServerOptions = {
    port: 9000,
    recordingPath: recordingsDir,
    mode: RecordingMode.Record, // Start in recording mode
    // Assume health check path for the test server itself
    healthPath: '/health'
  };

  const testServer = new TestServer(serverOptions);

  try {
    console.log('Starting Test Server in Record mode...');
    await testServer.start();
    console.log(`Test Server listening on http://localhost:${serverOptions.port}`);

    // Make a request to be recorded
    console.log('Making a request to the test server...');
    const response = await fetch(`http://localhost:${serverOptions.port}/api/data?id=123`);
    const data = await response.json();
    console.log('Received data:', data);

    // In a real test, you'd assert on `data` or switch to replay mode for subsequent runs.

  } catch (error) {
    console.error('Test scenario failed:', error);
  } finally {
    console.log('Stopping Test Server...');
    await testServer.stop();
    console.log('Test Server stopped.');
  }
}

runTestScenario().catch(err => console.error('Unhandled scenario error:', err));

view raw JSON →