LOGOMock Service Worker
  1. Getting Started
  2. Define mocks

Define mocks

To define which requests should be mocked, we are going to use request handler functions. They allow us to capture any request based on its method, URL, or other criteria, and specify which response to return.

Mock definition

When working with Mock Service Worker, the list of request handlers, browser- and server-specific setup is referred to as mock definition. There is no strict rules on managing your mock definition, however, we recommend keeping API mocking-related modules in a single directory.

Create a src/mocks directory:

1mkdir src/mocks

Once we have a directory, let's create a module where we will have all our request handlers. The handlers from this module can be reused for both browser and Node in the further steps of this tutorial.

Create a src/mocks/handlers.js file:

1touch src/mocks/handlers.js

Choose an API

The way we mock an API with Mock Service Worker resembles how that API is being used in an actual application. Please follow the respective steps below, depending on the API type you would like to mock.

Choose an API type to mock:

Mocking REST API/docs/getting-started/mocks/rest-api
Mocking GraphQL API/docs/getting-started/mocks/graphql-api

When in doubt, starting with a REST API is a good choice!