Skip to content

Mockserver Module

MockServer allows you to mock any server or service via HTTP or HTTPS, such as a REST or RPC service.

Install

npm install @testcontainers/mockserver --save-dev

Examples

it("should start and accept mocks", async () => {
  const container = await new MockserverContainer().start();
  const client = mockServerClient(container.getHost(), container.getMockserverPort());
  const url = container.getUrl();

  await client.mockAnyResponse({
    httpRequest: {
      method: "GET",
      path: "/foo",
    },
    httpResponse: {
      body: {
        string: "bar",
      },
      statusCode: 200,
    },
  });

  const response = await superagent.get(`${url}/foo`);

  expect(response.statusCode).toBe(200);
  expect(response.text).toBe("bar");
});