4.1.0 • Published 11 months ago

@mocks-server/main v4.1.0

Weekly downloads
1,349
License
Apache-2.0
Repository
github
Last release
11 months ago

Build status Coverage Status Quality Gate

NPM dependencies Renovate Last commit Last release

NPM downloads License

Mocks Server Mocks Server

The project

This project provides a mock server that can store and simulate multiple API behaviors. It can be added as a dependency of your project, and started simply running an NPM command.

Providing an interactive command line user interface and a REST API for changing the responses of the API, it is easy to use both for development and testing.

Main features

  • Route variants: Define many responses for a same route.
  • Multiple mocks: Group different route variants into different mocks. Change the used mock while the server is running using the interactive command line interface or the API.
  • Multiple formats: Responses can be defined using json files or Javascript files.
  • Express middlewares: Route variants can be defined as express middlewares.
  • 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 from tests, for example.
  • Integrations: Integrations with other tools are available, as the Cypress plugin.
  • Customizable: You can develop your own plugins, or even route handlers, that allows you to customize the format in which route variants are defined.

Installation

Add it to your dependencies using NPM:

npm i @mocks-server/main --save-dev

Add next script to your package.json file:

{
  "scripts": {
    "mocks" : "mocks-server"
  }
}

Usage

Now, you can start the Mocks Server with the command:

npm run mocks

When started for the first time, it creates a scaffold folder named mocks in your project, containing next files and folders:

project-root/
├── mocks/
│   ├── routes/
│   │   ├── middlewares.js
│   │   └── users.js
│   └── mocks.json
└── mocks.config.js

The folder contains examples from this documentation providing a simple API with two different mocks and some route variants. You can use the interactive CLI that is also started to change the server settings and see how you can change the responses of the API changing the current mock, changing route variants, etc.

Interactive CLI

How does it work?

It loads all files in the "routes" folder, containing handlers for routes, and the "mocks" file, which defines sets of "route variants".

// mocks/routes/users.js
module.exports = [
  {
    id: "get-users", // id of the route
    url: "/api/users", // url in express format
    method: "GET", // HTTP method
    variants: [
      {
        id: "empty", // id of the variant
        response: {
          status: 200, // status to send
          body: [] // body to send
        }
      },
      {
        id: "error", // id of the variant
        response: {
          status: 400, // status to send
          body: { // body to send
            message: "Error"
          }
        }
      }
    ]
  }
]

The server will respond to the requests with the route variants defined in the current mock.

// mocks/mocks.json
[
  {
    "id": "base", //id of the mock
    "routesVariants": ["get-users:empty", "get-user:success"] //routes variants to use
  },
  {
    "id": "users-error", //id of the mock
    "from": "base", //inherits the route variants of "base" mock
    "routesVariants": ["get-users:error"] //get-users route uses another variant
  }
]

Then, you can easily change the responses of the API while the server is running changing the current mock, or defining specific route variants. This can make your development or acceptance tests environments very much agile and flexible, as you can define different "mocks" for every different case you want to simulate.

Configuration

Configure the server simply creating a mocks.config.js file at the root folder of your project.

For changing settings (such as current mock, delay time, etc.) while it is running, you can use:

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 store different mocks, which are sets of different route variants. So it simulates multiple api behaviors and send different responses to the same request at your convenience, so it is like having different mock servers that can be changed while running.

Customization

Mocks Server is very customizable, and gives you the possibility of extend it with every new amazing feature you want:

Contributing

Contributors are welcome. Please read the contributing guidelines and code of conduct.

5.0.0-beta.4

11 months ago

5.0.0-beta.2

12 months ago

5.0.0-beta.3

11 months ago

5.0.0-beta.1

12 months ago

4.1.0

11 months ago

4.0.1

1 year ago

3.12.0

2 years ago

4.0.0

2 years ago

3.11.0

2 years ago

3.9.0

2 years ago

3.4.0

2 years ago

3.2.0

2 years ago

3.8.0

2 years ago

3.10.0

2 years ago

3.6.0

2 years ago

3.3.0

2 years ago

3.7.0

2 years ago

3.5.0

2 years ago

3.1.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.6.3

2 years ago

2.6.1

2 years ago

2.6.2

2 years ago

2.5.0

2 years ago

2.6.0

2 years ago

2.4.0

3 years ago

2.3.3

3 years ago

2.3.2

3 years ago

2.3.1

3 years ago

2.3.0

3 years ago

2.2.0

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

2.0.0-beta.2

3 years ago

2.0.0-beta.1

3 years ago

1.9.0

3 years ago

1.8.8

3 years ago

1.8.7

4 years ago

1.8.6

4 years ago

1.8.5

4 years ago

1.8.4

4 years ago

1.8.3

4 years ago

1.8.2

4 years ago

1.8.1

4 years ago

1.8.0

4 years ago

1.7.2

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.0

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago