Stop maintaining brittle hand-written API mocks and fighting flaky upstream staging servers.
auto-mock records real HTTP traffic once into deterministic .automock/ snapshots and replays it completely offline — with recorded latency playback, random error injection, and zero external dependencies.
Require Node.js >= 18.0.0. Run immediately with npx or install globally:
# Record upstream API traffic directly with npx npx @dkhproject/auto-mock record https://api.example.com --port 4000 # Replay recorded snapshots offline with npx npx @dkhproject/auto-mock replay --port 4000 # Or install globally npm install -g @dkhproject/auto-mock auto-mock --help
| Capability | auto-mock |
Manual JSON Mocks | In-Process Interceptors (MSW / Nock) | Heavy Mock Servers (WireMock / Hoverfly) |
|---|---|---|---|---|
Zero external dependencies (npx-ready) |
✅ | ✅ | ❌ | ❌ (JVM / Go binary) |
| Language-agnostic local HTTP proxy | ✅ | ❌ | ❌ (JS/TS only) | ✅ |
| Automatic record & deterministic replay | ✅ | ❌ | Manual setup | ✅ |
| Realistic recorded latency + chaos simulation | ✅ | ❌ | Custom handlers | Partial / Paid |
Per-request override headers (x-automock-*) |
✅ | ❌ | ❌ | ❌ |
- Zero external dependencies: Built strictly on Node.js built-in modules (
http,https,fs,path,crypto,url). - Deterministic snapshots: Keys fixtures on HTTP method, pathname, canonicalized query string, and SHA-256 request body hash.
- Lossless text & binary capture: Stores UTF-8 text inline and binary/compressed payloads as base64, while stripping RFC hop-by-hop headers.
- Offline replay: Serves saved
.automock/responses without network access and returns clear404JSON diagnostics on unrecorded routes. - Time-travel & chaos simulation: Replays captured latency (
--realistic-latency), injects fixed or jittered delays (--latency), simulates random failures (--fail-rate,--fail-status), and honors per-request override headers (x-automock-delay,x-automock-status). - Fixture management & colored logs: Inspect or wipe snapshots with
listandclean, and follow color-coded[REC],[HIT],[CHAOS], and[MISS]logs (respectingNO_COLOR).
Start the recording proxy pointing at your upstream API (default port 4000):
npx @dkhproject/auto-mock record https://api.example.com --port 4000
Send requests to http://127.0.0.1:4000/... instead of https://api.example.com/.... Each response is forwarded transparently and persisted inside .automock/.
Shut down your upstream server (or disconnect from the network) and start auto-mock in replay mode:
npx @dkhproject/auto-mock replay --port 4000
Matching requests are served directly from .automock/. Unrecorded requests return HTTP 404 with JSON details:
{
"error": "Fixture not found",
"message": "No recorded fixture matches this request. Run `auto-mock record <targetUrl>` first to record it.",
"request": {
"method": "GET",
"pathname": "/missing",
"query": "",
"expectedFixture": "GET_missing_..."
}
}Simulate real-world network latency and upstream outages during replay mode:
| Flag | Description | Example |
|---|---|---|
--realistic-latency |
Delay each response by the exact latencyMs captured when recorded |
auto-mock replay --realistic-latency |
--latency <ms|min-max> |
Add a fixed delay (ms) or uniform random jitter (min-max) |
auto-mock replay --latency 500-2000 |
--fail-rate <0-1> |
Randomly fail requests with probability between 0 and 1 |
auto-mock replay --fail-rate 0.25 |
--fail-status <code> |
HTTP status code returned on simulated failures (default 500) |
auto-mock replay --fail-rate 0.2 --fail-status 503 |
Example simulating slow, flaky upstream behavior:
auto-mock replay --port 4000 --realistic-latency --latency 200-800 --fail-rate 0.1 --fail-status 503
Simulated failures return:
{
"error": "Simulated failure via auto-mock",
"status": 503
}Integration tests can trigger delays or error codes on demand for individual requests without restarting the proxy:
x-automock-delay: <ms|min-max>— Overrides CLI latency flags for that single request.x-automock-status: <code>— Forces a specific status code (>= 400returns the simulated error JSON payload;< 400overrides the replayed status code).
# Inject a 1500ms delay on a single call curl -H "x-automock-delay: 1500" http://127.0.0.1:4000/api/users # Force a 429 rate-limit response on demand curl -i -H "x-automock-status: 429" http://127.0.0.1:4000/api/users
Inspect all recorded snapshots in .automock/ (or pass --dir <path>):
auto-mock list
Example output:
METHOD PATH STATUS LATENCY SNAPSHOT FILE
------ -------------------------- ------ ------- -----------------------------------------
GET /api/users?active=true 200 42ms GET_api_users_3a91c0d4e2f81b09.json
POST /api/users 201 18ms POST_api_users_94b71a02c51d440a.json
2 fixture(s) in /path/to/project/.automock
Delete all saved fixture files:
auto-mock clean
Each request emits a concise log line tagged by outcome:
[REC] GET /api/users?active=true -> 200 (42ms)
[REC] POST /api/users -> 201 (18ms)
[HIT] GET /api/users?active=true -> 200 (1ms)
[CHAOS] GET /api/users?active=true -> 503 (412ms)
[MISS] GET /api/missing -> 404 (0ms)
[REC](cyan) — Recorded from upstream inrecordmode[HIT](green) — Served from.automock/inreplaymode[CHAOS](magenta) — Delayed or failed via chaos simulation or override headers[MISS](yellow) — No matching snapshot found (404)
ANSI colors are automatically enabled in interactive TTYs and disabled when NO_COLOR is set (per no-color.org).
Each file in .automock/ stores deterministic request keys alongside the captured response:
{
"request": {
"method": "GET",
"pathname": "/api/users",
"query": "active=true&role=admin",
"bodyHash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
},
"statusCode": 200,
"statusMessage": "OK",
"headers": {
"content-type": "application/json"
},
"latencyMs": 18,
"bodyEncoding": "utf8",
"body": "{\"users\":[\"alice\",\"bob\"]}",
"recordedAt": "2026-11-24T06:30:00.000Z"
}Run the built-in node:test suite:
npm test