Skip to main content

Module snapshot

Module snapshot 

Source
Expand description

Snapshot assertions for generated JSON: assert_openapi_snapshot compares an app’s OpenAPI document with a file committed beside the tests, so a change to the API shows up in review as a change to that file.

Both sides are normalized before comparing — parsed, object keys sorted, printed with two-space indentation — so key order and whitespace never count, and a mismatch fails with a line diff of the normalized texts.

With RWS_UPDATE_SNAPSHOTS=1 in the environment (any value but empty, 0 or false) the assertion writes the normalized document to the file instead — creating it and its directory if needed — and passes:

RWS_UPDATE_SNAPSHOTS=1 cargo test     # accept the current documents
git diff tests/snapshots/             # review what changed

A missing file fails (with that hint) when the variable isn’t set, so a CI run never passes by writing its own snapshot. Relative paths are relative to the working directory, which under cargo test is the package’s root.

Constants§

UPDATE_VARIABLE
The environment variable that makes snapshot assertions write instead of compare.

Traits§

OpenApiSource
Something an OpenAPI document can be read from.

Functions§

assert_json_snapshot
assert_openapi_snapshot for any JSON document (a JSON Schema, an MCP tools/list result, a response body).
assert_openapi_snapshot
Compare an OpenAPI document with the snapshot at path (see the module docs for normalization and RWS_UPDATE_SNAPSHOTS). source is the document’s text (app.openapi_spec(&config)), a TestResponse of it, or a TestClient whose app serves /openapi.json.