Mumbo is a simple http server designed to be used in testing as an alternative to mocking.
If your project is dependent on HTTP services, Mumbo allows you to quickly set up a HTTP server and define its routes and their responses, meaning you don't have to mock them.
There are some additional methods provided to help with assertions.
Import and call CreateServer
to create a Mumbo server instance.
You can start a Mumbo server on a specific port.
import { CreateServer } from "mumbo";
const mumboServer = CreateServer();
// Tell the mumboServer to start listening to requests on port 1234
await mumboServer.Start(1234)
Or let Mumbo use any free port.
import { CreateServer } from "mumbo";
const mumboServer = CreateServer();
// Tell the mumboServer to start listening to requests on a free port
const mumboStartResult = await mumboServer.Start()
// Use the mumboStartResult to access the port the server is listening on
const port = mumboStartResult.port;
You can configure responses that Mumbo should return for specific requests. By default, any requests made will return a 200 and an empty body.
// Any GET requests made to the server to the /faked-response-body will
// return a status code of 201, and a JSON body of { example: "response" }
mumboServer.ConfigureResponse("GET", "/faked-response-body", () => {
return {
statusCode: 201,
body: JSON.stringify({
example: "response",
}),
};
});
Mumbo keeps a log of all requests that have been made to it.
// Returns a list of all requests made to the Mumbo server
const requests: MumboRequestLog[] = mumboServer.GetRequests();
type MumboRequestLog = {
url: URL;
method: string;
body: string;
};
You can also clear the list of requests.
mumboServer.ClearRequests();
Mumbo can be stopped easily.
// Stops Mumbo from listening for requests
await server.Stop();