- A mock defines a set of "route variants"
- Mocks can extend from another mock, so you can "copy" an existent one, and change only one "route variant", for example.
- Mocks must be defined in the
mocks/mocks.(json|js)file of your project (or any other compatible extension if you are using Babel). The file must export an array of mocks.
A mock is an object containing:
id(String): Identifier for the mock.
from(String): Optional. Mock id from which this mock extends.
routesVariants(Array of Strings): Routes variants ids, expressed as
We recommend to always define a base mock containing one variant of all your routes. If you extend all other mocks from this one (or from another extending from this one), when you add new routes or middlewares, you'll only have to add them to the base mock.
mocks/mocks.js and export the mocks array:
For defining the current mock, you can use command line arguments when starting the server:
You can also change the current mock using the interactive CLI:
Make a request to the Mocks Server administration REST API provided by
@mocks-server/plugin-admin-api (included in the main distribution):
Or install by yourself and use one plugin providing integration with other tools:
- Use the Cypress command provided by
Note that the order in which route variants are added to the array may be important. As seen in the previous chapter, route variants responses can be defined as
express middlewares, so maybe some routes are not going to send a response, and should be added in an specific order.
The order in which Mocks Server register express middlewares is strictly the same in which route variants are defined in the array, so take it into account when adding your route variants middlewares.
When extending from another mock, the new route variant will replace the old one in the same position that it was originally defined.
Read the "using middlewares" guide for further info and examples.