Getting Started
Mentoss is a utility for mocking JavaScript fetch()
requests both in the browser and on server runtimes like Node.js, Deno, and Bun. All of these environments have a global fetch()
function capable of making HTTP requests. When testing your JavaScript code, you don’t want to make network requests, so Mentoss is used to simulate those requests and their responses.
-
Install Mentoss. If you’re using Mentoss in a server-side runtime, you can install it using the package manager of your choice.
-
Create a mock server. Import the
MockServer
constructor from Mentoss and create a new instance like this:The
server
object is where you can set up the routes that you want to mock. For example:This example creates a route of
/ping
that returns a 200 status when called.Mock servers can specify routes in a number of different ways. See XXX for more details.
-
Create a fetch mocker. A
FetchMocker
instance is used to create a mocked version of thefetch()
function. To create a fetch mocker, import theFetchMocker
constructor and specify the server(s) you want to use:Once you have a fetch mocker created, it’s time to use the mocked
fetch()
function. -
Use the mocked
fetch()
function. CallmockGlobal()
and then usefetch()
as you normally would: