Testing Helpers: Mock Server
The mock server provides several methods to help verify that your requests were made as expected.
Verifying Route Calls
Tracking which routes have been called is a key part of many tests. The mock server provides several methods and properties to help with this.
allRoutesCalled()
The allRoutesCalled()
method checks if all routes have been called. It returns true
if all routes have been called at least once, and false
otherwise:
import { MockServer, FetchMocker } from "mentoss";
const server = new MockServer("https://api.example.com");const mocker = new FetchMocker({ servers: [server] });
server.get("/users", 200);server.post("/users", 200);
console.log(server.allRoutesCalled()); // false
await mocker.fetch("https://api.example.com/users");console.log(server.allRoutesCalled()); // false
await mocker.fetch("https://api.example.com/users", { method: "POST" });console.log(server.allRoutesCalled()); // true
In this example, the allRoutesCalled()
method first returns false
because neither the GET nor the POST /users
route has been called yet. After both routes have been called, the method returns true
.
assertAllRoutesCalled()
The assertAllRoutesCalled()
method is an assertion that checks if all routes have been called. It throws an error if any routes have not been called:
import { MockServer, FetchMocker } from "mentoss";
const server = new MockServer("https://api.example.com");const mocker = new FetchMocker({ servers: [server] });
server.get("/users", 200);server.post("/users", 200);
await mocker.fetch("https://api.example.com/users");server.assertAllRoutesCalled(); // Error!
The thrown error contains a message with information about the uncalled routes.
called()
The called()
method checks if a specific route has been called. You can pass either a URL string (which defaults to a GET request) or a request pattern object:
import { MockServer, FetchMocker } from "mentoss";
const server = new MockServer("https://api.example.com");const mocker = new FetchMocker({ servers: [server] });
server.post( { url: "/users", body: { name: "John Doe", }, }, 200,);
console.log(server.called("/users")); // false
await mocker.fetch("https://api.example.com/users", { method: "POST", body: { name: "John Doe", },});
console.log(server.called("/users")); // true
console.log(server.called({ url: "/users", method: "POST", body: { name: "John Doe", },})); // true
In this example, the called()
method returns false
because the /users
route has not been called yet. The matching is done using the same comparison logic as if a fetch()
request was made to the route.
uncalledRoutes
The uncalledRoutes
property returns an array with information about uncalled routes. You can use this property to check if any routes have not been called:
import { MockServer, FetchMocker } from "mentoss";
const server = new MockServer("https://api.example.com");const mocker = new FetchMocker({ servers: [server] });
server.get("/users", 200);server.post("/users", 200);
await mocker.fetch("https://api.example.com/users");
console.log(server.uncalledRoutes); // ["🚧 [Route: POST https://api.example.com/users -> 200]"]
The uncalledRoutes
property is helpful when you want to format your own error messages or assertions.
Additional Helpers
clear()
The clear()
method removes all routes from the server so you can reuse the same server for multiple tests.
import { MockServer, FetchMocker } from "mentoss";
const server = new MockServer("https://api.example.com");const mocker = new FetchMocker({ servers: [server] });
server.get("/users", 200);
server.clear();
await mocker.fetch("https://api.example.com/users"); // Error!