Test isolation
Start one container per test file; reset it between tests – no restarts.
| Method | Stubs | Scenarios | Journal |
|---|---|---|---|
reset() | Registered ones only – runtime stubs dropped | Restarted | Emptied |
resetRequests() | Kept | Kept | Emptied |
Reset between tests
examples/isolation/isolation.e2e.test.ts
afterEach(async () => {
await wiremock.reset(); // registered stubs only, scenarios restarted, journal empty – no restart
});
Source: examples/isolation/isolation.e2e.test.ts (lines 23–25)
Reset stateful scenarios
A scenario – PENDING, then SETTLED:
examples/isolation/isolation.ts
const payment = (status: string) => ({ id: "pay-1", amount: "42.00", currency: "USD", status });
/** A stateful stub: the first GET answers PENDING, every later one SETTLED. */
export const SETTLEMENT_SCENARIO = {
mappings: [
{
scenarioName: "settlement",
requiredScenarioState: "Started",
newScenarioState: "settled",
request: { method: "GET", url: "/payments/pay-1" },
response: { status: 200, jsonBody: payment("PENDING") },
},
{
scenarioName: "settlement",
requiredScenarioState: "settled",
request: { method: "GET", url: "/payments/pay-1" },
response: { status: 200, jsonBody: payment("SETTLED") },
},
],
} satisfies { mappings: WiremockStubMapping[] };
Source: examples/isolation/isolation.ts (lines 15–34)
reset() starts it over:
examples/isolation/isolation.e2e.test.ts
it("a scenario moves on with every request…", async () => {
expect((await client.get("pay-1")).status).toBe("PENDING");
expect((await client.get("pay-1")).status).toBe("SETTLED");
});
it("…and reset() restarts it for the next test", async () => {
expect((await client.get("pay-1")).status).toBe("PENDING");
});
Source: examples/isolation/isolation.e2e.test.ts (lines 33–40)
Clear only the journal
examples/isolation/isolation.e2e.test.ts
it("resetRequests() empties the journal but keeps the scenario's state", async () => {
await client.get("pay-1");
expect(await wiremock.countRequests({ method: "GET", url: "/payments/pay-1" })).toBe(1);
await wiremock.resetRequests();
expect(await wiremock.countRequests({ method: "GET", url: "/payments/pay-1" })).toBe(0);
expect((await client.get("pay-1")).status).toBe("SETTLED"); // still in state "settled"
});
Source: examples/isolation/isolation.e2e.test.ts (lines 44–52)
Add a stub for one test
POST it to getMappingUrl(); it lasts until the next reset():
examples/isolation/isolation.ts
/** Adds a stub to a running WireMock through its admin API; it lasts until the next `reset()`. */
export async function addStub(
wiremock: Pick<StartedWiremockContainer, "getMappingUrl">,
stub: WiremockStubMapping,
): Promise<void> {
const response = await fetch(wiremock.getMappingUrl(), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(stub),
});
if (response.status !== 201) throw new Error(`WireMock rejected the stub with ${response.status}: ${await response.text()}`);
}
Source: examples/isolation/isolation.ts (lines 43–54)
examples/isolation/isolation.e2e.test.ts
it("a stub added at runtime serves until reset()", async () => {
await addStub(wiremock, {
request: { method: "GET", url: "/payments/pay-404" },
response: { status: 404, body: "no such payment" },
});
await expect(client.get("pay-404")).rejects.toThrow(PaymentsApiError);
await wiremock.reset();
// Unmatched again – WireMock answers 404 "Request was not matched" – and the recorded stubs remain.
await expect(client.get("pay-404")).rejects.toMatchObject({ status: 404, message: expect.stringContaining("Request was not matched") });
expect((await client.list()).meta.totalItems).toBe(2);
});
Source: examples/isolation/isolation.e2e.test.ts (lines 56–68)
Prefer registering stubs before start() – they are validated up front.