Skip to content

Creating a Mock Server

A mock server in Mentoss is how you define what requests you’re expecting to send and what responses should be returned for those requests. Mock servers are different from real servers in that mock servers don’t run on a port and aren’t open to network requests. Instead, mock servers exist only in memory, eliminating any concerns about side effects or security issues.

You can assign one or more mock servers to a FetchMocker instance, therefore limiting the requests a mocked fetch() call can make.

Create a new MockServer instance

To get started, import the MockServer class and create a new instance. The only argument is the base URL for the server. For example, to create a new MockServer that responds to requests for https://api.example.com, you would create an instance as follows:

import { MockServer } from "mentoss";
const server = new MockServer("https://api.example.com");

With your server created, the next step is to add some routes.

Add routes to a MockServer

You can add routes to a MockServer by calling one of several methods:

  • get(request, response)
  • post(request, response)
  • put(request, response)
  • delete(request, response)
  • patch(request, response)
  • options(request, response)
  • head(request, response)

Each method accepts two arguments: a request pattern and a response pattern. These patterns can be a single value or an object with multiple properties, depending on your desired behavior for the route.

Add a simple route to a MockServer

At its simplest, you can add a route by passing a URL for the request pattern and a status code for the response pattern, as in this example:

import { MockServer } from "mentoss";
const server = new MockServer("https://api.example.com");
server.get("/ping", 200);

This instructs the server to respond to GET requests for https://api.example.com/ping with a 200 status code.

Use URL parameters in the request URL

If you’d like the server to respond to a request to any URL that matches a particular pattern, you can do so by using placeholder parameters directly in the URL. Here’s an example:

import { MockServer } from "mentoss";
const server = new MockServer("https://api.example.com");
server.get("/users/:userId", 200);

This instructs the server to respond to GET requests for any URL that matches the pattern https://api.example.com/users/:userId with a 200 status code. The :userId part of the URL is a placeholder that can match any value, so URLs like https://api.example.com/users/123 or https://api.example.com/users/456 match the pattern. This is helpful if you aren’t quite sure what the exact URL will be during a test.