Skip to main content
Version: 4.0.0

Basics

Main concepts

Mocks Server relies on three simple concepts for simulating, control and storage multiple API scenarios:

  • Routes: A route defines the url and method of an API resource. Wildcards can be used in urls and methods, so, one route can simulate one real API resource, or many.
  • Variants: Each route can contain many different variants. Each variant can define a response to send, or a middleware to execute, or a url to proxy the request, etc. You can choose which variant has to be used by each route on each particular moment.
  • Collections: A collection of route variants defines all current routes and variants in the mocked API. They can be created extending other collections. So, you can store many collections and change the whole API behavior by simply changing the current one.
Main concepts schema
info

Read Routes, Variants and Collections chapters for further info about how to define them

Motivation

The management of different responses for the same route and the ability to store different collections allows to easily toggle between many predefined API state simulations. This is perfect while developing an API client, because all of the responses of the API can be changed at a time using the interactive CLI, or one single response can be changed also without modifying any code. And it is also perfect while running tests, because the different route variants collections can be used to test different scenarios.

Exposing different APIs allowing to control the server while it is running makes able to integrate it easily with different tools and ecosystems. So, once the responses are defined, they can be reused in different development lifecycle stages, such as local development, continuous integration testing, etc.

Workflow schema

Here you have some examples about how to change the current collection and other settings using different integration tools from different ecosystems:

const { createServer } = require("@mocks-server/main");

const core = createServer();

core.start().then(() => {
core.mock.collections.select("collection-a");
});