WireMock Captain

4.1.3 · active · verified Tue Apr 21

WireMock Captain is a Node.js library (supporting TypeScript and JavaScript) designed to simplify integration testing of HTTP APIs by providing a programmatic interface to the WireMock simulator. Currently at version 4.1.3, this library is actively maintained and used in commercial products, emphasizing a stable release cadence. Its core differentiator lies in offering a native TypeScript/JavaScript experience for configuring and interacting with the Java-based WireMock service, which typically runs in a Docker container. This approach bypasses the limitations of in-process mocks (which often lack real-world accuracy) and the complexities of testing against live non-production service instances, providing a fast, full-featured, and debuggable solution for API mocking without requiring Java tooling for the test runner.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to set up, register stubs for GET and POST requests, and verify interactions with a WireMock instance using WireMock Captain in a typical Jest testing environment. Requires an external WireMock Docker container.

import { WireMock } from 'wiremock-captain';
import axios from 'axios'; // Example HTTP client

describe('API Integration Test with WireMock Captain', () => {
  const wiremockEndpoint = 'http://localhost:8080';
  let mock: WireMock;

  // Ensure WireMock Docker container is running before tests
  // docker run -itd --rm -p 8080:8080 --name mocked-service wiremock/wiremock:3.9.1

  beforeAll(() => {
    mock = new WireMock(wiremockEndpoint);
  });

  beforeEach(async () => {
    await mock.resetAll(); // Clear all previous stubs and scenarios
  });

  afterAll(async () => {
    await mock.resetAll();
    // Optionally, stop the WireMock container if managed by the test runner
    // e.g., using 'docker stop mocked-service'
  });

  test('should mock a GET request and receive the predefined response', async () => {
    const request = {
      method: 'GET',
      endpoint: '/api/resource/123',
      headers: { 'Accept': 'application/json' }
    };
    const mockedResponse = {
      status: 200,
      body: { id: '123', name: 'Mocked Resource' },
      headers: { 'Content-Type': 'application/json' }
    };

    await mock.register(request, mockedResponse);

    const response = await axios.get(`${wiremockEndpoint}/api/resource/123`);

    expect(response.status).toBe(200);
    expect(response.data).toEqual({ id: '123', name: 'Mocked Resource' });
    
    // Verify WireMock received the request (optional but good practice)
    const receivedRequests = await mock.getAllRequests();
    expect(receivedRequests.length).toBe(1);
    expect(receivedRequests[0].url).toContain('/api/resource/123');
  });

  test('should handle a POST request with specific body matching', async () => {
    const postRequest = {
      method: 'POST',
      endpoint: '/api/items',
      body: { item: 'new item' }
    };
    const postResponse = {
      status: 201,
      body: { message: 'Item created' },
      headers: { 'Content-Type': 'application/json' }
    };

    await mock.register(postRequest, postResponse);

    const response = await axios.post(`${wiremockEndpoint}/api/items`, { item: 'new item' });

    expect(response.status).toBe(201);
    expect(response.data).toEqual({ message: 'Item created' });
  });
});

view raw JSON →