Over the past 20 years, I cannot recall how many times I was in the middle of writing API tests or helper scripts when suddenly the backend service went offline either due to an issue or maintenance. Earlier in my career, this would result in a major roadblock as I would have to stop what I was working on and wait for the backend to be restored. After several incidents like this, I was determined to find a solution that would allow me to continue working. This is when I discovered "JSON-Server".

JSON-Server is a light weight application that can run locally and be configured to return custom responses mirroring the real backend. Having this setup as a backup plan has been a game changer, allowing me to continue writing tests and delivering on time.

I quickly discovered this could also be leveraged to easily test edge cases and error scenarios since I have the ability to configure the response payloads. This helped strengthen the test suites and catch bugs that may occur intermittently. In the next section, I'll walk through how to set up json-server and write some simple tests using Jest to validate the API responses.


Installation

Setting up JSON Server is pretty straightforward. You can install it globally using npm:

npm install -g json-server

Defining Endpoints and Response Payloads

The simplicity of JSON Server is one of its greatest strengths. Endpoints and response payloads can be quickly defined in the repo's "db.json" file, which serves as the data source for the API. Ideally, the response payloads should mirror the real backend, allowing you to easily test edge cases and error scenarios.

Multiple endpoints and response payloads can be defined in the "db.json" file, but for demonstration purposes, I'll define a single endpoint ("Orders") which returns the response payload shown below.


Starting the server

Run the following command in the terminal:

npx json-server

By default, this will start the server on port 3000. If this conflicts with another service running on your machine, use the "--port" flag to specify a different port. For Example, to start the server and use port 4000:

npx json-server --port 4000

Once the server is running, you should see a message similar to the following in the terminal. This indicates that the server is up and running and ready to accept requests. In this screenshot, the terminal indicates there are a total of 3 endpoints defined in the "db.json" file (orders, users, products).
Starting the JSON Server in the terminal with the command 'json-server --watch db.json --port 4000'


Retrieving data from the mock server

Now that the server is up and running, we can make a GET request to the "orders" endpoint to retrieve the response payload. Since I started the server on port 4000, the endpoint I will be sending a request to: "http://localhost:4000/orders"

To quickly ensure the orders endpoint is returning the expected response payload, try the following:

Paste the endpoint URL in a Web browser

Retrieving the orders response by entering the endpoint URL in the browser


Run a cURL Command in the terminal
curl http://localhost:4000/orders | jq
cURL command from the terminal.

Validating the response

Ideally, I would use a testing framework like Jest to write a proper test suite with assertions, but for demonstration purposes, I'll create a script that iterates over the Orders response and prints a table with the problematic records.

In your project, create a file called "getOrders.js" and copy/paste the code below. Make sure to update the URL in the fetch function if your JSON Server is running on a different port.

Run the script in the terminal via:

node getOrders.js

The following tables are returned, displaying the records in which errors were discovered.

Script to find errors in orders response.