Run a mock backend for API tests
Your docs walk a reader through API calls, but the real backend is slow, rate limited, or not deployed where your tests run. Run a mock server instead. Doc Detective starts it as a background process and waits until it’s ready. It then sends your documented requests to the mock and stops it when the test ends.
This recipe uses Prism, which serves responses from the examples in an OpenAPI description. Any mock server that runs from the command line works the same way, such as json-server. For the basics of httpRequest, see Test HTTP and APIs.
Before you begin
Make sure you have the following:
- Doc Detective installed.
- An OpenAPI description of your API, saved as
openapi.yamlnext to your spec file. Prism answers each request with the response example the description defines. - Node.js, so
npxcan run Prism without a global install.
Step 1: Start the mock server
Add a startSurface step with a process descriptor. The process field is the shell command that starts the server, and name is the handle later steps use to stop it.
The step doesn’t finish until the server is ready, so the next request can’t arrive too early. waitUntil sets what “ready” means:
port: the TCP port accepts connections on127.0.0.1.httpGet: aGETrequest to a URL returns a2xxstatus. Use it when the port opens before the server can answer.stdio: the server’s output contains a string or matches a/regex/, such as a “listening” log line.delayMs: at least this many milliseconds pass.
Set more than one, and the step waits for all of them. timeout caps the wait, 60000 milliseconds by default. The example raises it because the first npx run downloads Prism.
Step 2: Point requests at the mock
Send each httpRequest step to the mock server’s address. Assert on the response the same way you would against the real API. response.body checks only the fields you list, so extra fields in the response, such as a role field, don’t fail the step.
A mock server also lets you reach error states on demand, so the docs for your error responses stay tested too. Prism doesn’t look up IDs: any valid request to /users/{id} gets the operation’s lowest 2xx example, so /users/999 returns 200. To get an error response, declare it in the description, and ask for it with a Prefer: code=<status> request header. This openapi.yaml declares a 404 with an example next to the 200:
Then send the header and expect the error code:
Use 127.0.0.1 rather than localhost when the mock server listens only on IPv4, as Prism does by default. On some systems localhost resolves to the IPv6 address first.
Step 3: Stop the mock server
Add a closeSurface step that names the process. Stopping it frees the port for the next test.
If a test fails before it reaches this step, Doc Detective still stops the process when the run finishes.
Put it together
The complete spec starts the mock, sends two requests, and stops the mock:
To use json-server instead, give it a JSON file of records, such as this db.json:
Then change the process command and the port. json-server listens on port 3000 by default:
json-server returns every ID as a string, so its body check expects "1" rather than the number 1:
json-server looks up records by ID, so a request for an ID that isn’t in db.json returns 404 on its own. It ignores the Prefer header, so leave that header off.
Step 4: Run the test
NPX
Docker
Verify it works
Each step reports PASS. The startSurface step passes once the port opens, and each httpRequest step passes when the response from the mock matches its expectations. To see where results land, see Reporters and artifacts.
Troubleshooting
The startSurface step times out
Cause: The server didn’t meet every waitUntil condition before timeout. Maybe the first npx run was still downloading. Maybe the server listens on a different port than the one you wait for, or it exited on startup.
Solution: Raise timeout, and confirm the port in waitUntil matches the port the server prints. Run the process command by hand to see any startup error.
The step fails because the port is already in use
Cause: Another process, often a mock server left over from an earlier manual run, holds the port.
Solution: Stop the other process, or start the mock on a free port and update waitUntil.port and the request URLs to match.
What’s next
- Test HTTP and APIs for response assertions and chaining calls with values saved from earlier responses.
- Generate requests from an OpenAPI description to test every operation in the same description your mock serves.
startSurfaceprocess descriptors for every field, includingargs,workingDirectory, andtty.