Intro
#
The projectThis project provides a mock server that can simulate and store multiple API behaviors. It can be added as a dependency of your project, and started simply running an NPM command.
#
Main features- Multiple api behaviors: It allows to define different responses for the same route, and group them into different behaviors.
- Multiple formats: Responses can be defined using
json
files or Javascript files. Definitions can be plain objects, and even Express middlewares can be used to send dynamic responses. - Multiple interfaces: Settings can be changed using the interactive CLI or the admin REST API. The CLI is perfect for development, and the API can be used in other scenarios, as the Cypress plugin does.
#
InstallationAdd it to your dependencies using NPM:
Add next script to your package.json
file:
#
UsageNow, you can start the Mocks Server with the command:
#
ConfigurationConfigure the server simply creating a file at the root folder of your project.
For changing settings (such as current behavior, delay time, etc.) while it is running, you can use:
- Interactive command line interface, which is very useful in local environments for development.
- REST API which is very useful to change behaviors from E2E tests, for example, as the Cypress plugin does.
#
How does it work?As input, it needs "fixtures", which are handlers for specific requests, and "behaviors", which are sets of "fixtures".
You can simulate all the api behaviors you need for your development or tests environments simply extending behaviors to change the response of some specific uris, even when the received request is exactly the same.
You can easily change the current behavior while the server is running, which will make your development or acceptance tests environments very much agile and flexible, and not api dependant.
#
Why a mock server?Controlling the responses of the api will improve the front-end development workflow, avoiding early dependencies with back-end. It also improves the testing and development of error cases or another cases that are commonly hard to reproduce with a real api.
Defining the api responses during the earliest phases of development will improve the communication among team members and align their expectations.
Working with Node.js, it integrates better in front-end projects as any other NPM dependency, and it will be easier for front-end developers to maintain the mocks.
#
Why "Mocks" in plural?As explained, the mocks-server can simulate multiple api behaviors and send different responses to the same request, so it is like we have different mock servers that we can change while running at our convenience.
#
CustomizationThe mocks-server is very customizable, and gives you the possibility of extend it with every new amazing feature you want:
- Start it programmatically and use his multiple methods and events to manage it from your program.
- Add new options and features adding plugins, or developing your owns.
- Add new fixtures handlers, which allows to customize the format in which fixtures are defined.