Mocks
#
Intro- 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. The file must export an array of mocks.
#
APIA 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"[routeId]:[variantId]"
note
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.
#
Defining mocks using JavaScriptMocks can also be defined using a JavaScript file. Simply rename the file mocks/mocks.json
into mocks/mocks.js
and export the mocks array:
#
How to change current mock#
Using command line argumentsFor defining the current mock, you can use command line arguments when starting the server:
#
Using the interactive CLIYou can also change the current mock using the interactive CLI:
#
Using the admin API RESTMake a request to the Mocks Server administration REST API provided by @mocks-server/plugin-admin-api
(included in the main distribution):
#
IntegrationsOr install by yourself and use one plugin providing integration with other tools:
- Use the Cypress command provided by
@mocks-server/cypress-commands
:
#
The order mattersNote 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.