Looping every recorded response

replay() serves one response per request shape: the newest recording of the status that occurred most often. Everything else Stubsmith holds for that shape goes untested, which is usually where the bugs are, since the 429 and the 500 your API really returned are recorded and never exercised. replay_all() runs the block once per recording. Requires SDK 0.3.0 or later.

def test_parses_every_recorded_success():
    for attempt in stubsmith.replay_all(statuses={200}):
        with attempt:
            result = connector.sync_orders()
        assert result.orders is not None


def test_degrades_on_every_recorded_failure():
    for attempt in stubsmith.replay_all(statuses={429, 500}):
        with attempt:
            with pytest.raises((RateLimited, UpstreamUnavailable)):
                connector.sync_orders()
import { replayAll } from "stubsmith";

test("parses every recorded success", async () => {
  for await (const pass of replayAll({ statuses: [200] })) {
    const result = await connector.syncOrders();
    assert.notEqual(result.orders, undefined);
  }
});

test("degrades on every recorded failure", async () => {
  for await (const pass of replayAll({ statuses: [429, 500] })) {
    await assert.rejects(connector.syncOrders());
  }
});

In Node each pass restores itself, so there is no inner block as in Python.

Filter by status unless you have a reason not to. It decides what a failing build means. Unfiltered, the loop feeds successes and failures into one test body, so the body has to hold for both; the day a 500 first appears in the window, assertions written when only 200s existed start failing, and the build breaks because the recording changed rather than because the code did. That is the opposite of what a failing test should tell you.

Filtered, every pass is the same kind of response, the assertions can be specific, and a failure means your code cannot handle a response your API genuinely returns. The filter also bounds the work: unfiltered, the pass count is roughly (samples per response) × (distinct statuses), and it grows with traffic on its own. A shape with recordings but none of the requested status raises StubNotFound naming the filter and listing what it does have, rather than serving a response the loop was told to exclude. Requires SDK 0.4.0 or later.

No endpoint is named. Which shapes a pass touches is discovered by running your code, so this works for a client whose call sequence you would rather not spell out, and for one whose sequence changes between passes because it branches on the response it got: a shape first reached on a later pass is still looped from its own first recording.

The first pass serves exactly what replay() serves, so a test that passes under replay() still passes on pass one. Iteration stops once every shape that was actually touched has served its last recording, and a shape with a shorter window keeps serving its final recording rather than raising, so one endpoint's thin history cannot truncate the loop for the rest.

Note what this asks of the test body: it runs against a 200 on one pass and possibly a 500 on the next, so the assertions have to hold across the whole recorded range. That is the point, and it is also why replay() remains the right tool for a test written against one known response. To pin a single response, pass select:

def test_backs_off_when_rate_limited():
    with stubsmith.replay(select=stubsmith.by_status(429)):
        with pytest.raises(RateLimited):
            connector.sync_orders()
import { replay, byStatus } from "stubsmith";

test("backs off when rate limited", async () => {
  const r = replay({ select: byStatus(429) });
  try {
    await assert.rejects(connector.syncOrders(), RateLimited);
  } finally {
    r.restore();
  }
});

by_status raises StubNotFound when no recording of that status exists rather than quietly serving a different one, because a rate-limit test that ran against a 200 would pass while testing nothing.

Either way, served() reports what actually ran, so a test can assert its coverage instead of assuming it. Each entry carries domain, method, path_template, fingerprint, status, capture_id, captured_at, and its position in that shape's window as index and total.

with stubsmith.replay() as r:
    connector.sync_orders()

assert {s.status for s in r.served()} == {200}
const r = replay();
try {
  await connector.syncOrders();
  assert.deepEqual(new Set(r.served().map((s) => s.status)), new Set([200]));
} finally {
  r.restore();
}

served() lists every call in order. Field names are camelCase in Node (pathTemplate, capturedAt).

Getting the window into a bundle

A bundle holds one recording per response by default, so replay_all() would give one pass per status. Ask for the window explicitly:

export STUBSMITH_API_KEY=<your project key>

stubsmith pull --samples all \
  --endpoint "GET /admin/orders" \
  --endpoint "GET /admin/products" \
  --out tests/bundles/admin.json
export STUBSMITH_API_KEY=<your project key>

npx stubsmith pull --samples all \
  --endpoint "GET /admin/orders" \
  --endpoint "GET /admin/products" \
  --out tests/bundles/admin.json

The flags are the same in both SDKs. In Node, pullBundle() does the same from code without writing a file.

--samples above 1 requires --endpoint, because the full window for a whole project is not served. Repeat the flag to cover several endpoints in one file: that is one request each, merged for you, with duplicate request shapes dropped and any truncated report carried through. Repeating requires SDK 0.4.0 or later.

Scoping is what you want anyway: a project-wide bundle is capped at 2,000 request shapes (counted per endpoint and fingerprint) and reports the rest in truncated rather than carrying them. Commit the files and the suite needs no key and no network; treat a refresh like a lockfile change.

Or fetch at collection time, with no file involved:

BUNDLE = stubsmith.fetch_bundle("GET /admin/orders", samples="all")

def test_every_recorded_response():
    for attempt in stubsmith.replay_all(BUNDLE):
        with attempt:
            connector.list_orders()
import { fetchBundle, replayAll } from "stubsmith";

const bundle = await fetchBundle("GET /admin/orders", { samples: "all" });

test("every recorded response", async () => {
  for await (const pass of replayAll({ bundle })) {
    await connector.listOrders();
  }
});

fetchBundle() is async in Node, so it needs await.

Fetch once at module level, not per test: it is a blocking HTTP call. It needs a live key wherever the tests run, and recordings roll as the sample window rolls, so a failure you see today may not reproduce next week. Fetch live while iterating locally; commit a pulled bundle for CI.

That split matters more under replay_all() than under replay(). With a committed bundle the loop runs the same passes until someone re-pulls, and a new recording arrives as a reviewable file change. Fetching live, both the number of passes and the responses in them change as traffic changes, so a build can turn red with no commit behind it.