Version: 2.2.0

Organizing files

Files structure#

When started for the first time, Mocks Server will create a config file and a scaffold folder named mocks in your project, containing next files and folders:

project-root/
โ”œโ”€โ”€ mocks/
โ”‚ โ”œโ”€โ”€ routes/
โ”‚ โ”‚ โ””โ”€โ”€ users.js
โ”‚ โ””โ”€โ”€ mocks.json
โ””โ”€โ”€ mocks.config.js

This scaffold contains some examples from this documentation that may help you to better understand how routes and mocks should be defined, how to use express middlewares, etc.

note

The name of the mocks folder can be changed using the path option. Read options for further info.

/routes#

All files inside the /routes folder will be loaded, including subfolders, so you can organize routes in the way you want. As a suggestion, you can create a different file for each API entity, and a different folder for each API domain. This will help to maintain your routes organized. For example:

routes/
โ”œโ”€โ”€ customers/
โ”‚ โ”œโ”€โ”€ addresses.js
โ”‚ โ””โ”€โ”€ users.js
โ””โ”€โ”€ sales/
โ”œโ”€โ”€ orders.js
โ””โ”€โ”€ products.js
info

Remember that every file inside the /routes folder must export an array containing Mocks Server routes.

/mocks.json#

This file contains the definitions of mocks, and its name can't be changed (you can also use a .js extension if you want, or even any other extension if you are using Babel). It must export an array of Mocks Server mocks.

Other files and folders#

You can create other files and folders inside the mocks folder, Mocks Server will simply ignore them. So, for example, if you want to maintain the data you use in your routes separated from the definition of the routes, you could create a data folder, and import it from the route files.

project-root/
โ”œโ”€โ”€ mocks/
โ”‚ โ”œโ”€โ”€ data/
โ”‚ โ”‚ โ””โ”€โ”€ users.js
โ”‚ โ”œโ”€โ”€ routes/
โ”‚ โ”‚ โ””โ”€โ”€ users.js
โ”‚ โ””โ”€โ”€ mocks.json
โ””โ”€โ”€ mocks.config.js
// mocks/data/users.js
module.exports = [
{
id: 1,
name: "John Doe"
},
{
id: 2,
name: "Jane Doe"
}
];
// mocks/routes/users.js
const users = require("../data/users");
module.exports = [
{
id: "get-users",
url: "/api/users"
method: "GET",
variants: [
{
id: "all",
response: {
status: 200,
body: users
}
},
{
id: "one",
response: {
status: 200,
body: [users[0]]
}
}
]
},
];

Hot reloading#

Every time a file in the mocks folder is changed (including custom files and folders outside the routes folder), Mocks Server will reload everything automatically.

If any file contains an error, Mocks Server will add an alert, and you will see the error in the interactive CLI:

Interactive CLI alerts

The alert will be removed automatically when the file containing the error is fixed and saved again:

Interactive CLI

Good practices#

Use descriptive ids#

We strongly recommend to assign very descriptive ids to the "routes", "variants" and "mocks", as they will be used afterwards in the CLI, the REST API, and all other possible Mocks Server interaction tools.

A good pattern for assigning an id to a route can be [method]-[entity], as in get-users, get-user, etc.

For assigning id to mocks, we recommend to maintain a base mock named as standard, base, or default. The rest of mocks should extend from it (at least indirectly), and their ids should be a short description of the mock itself, for example:

[
{
"id": "base",
"routesVariants": ["get-users:all", "get-user:success", "create-user:success"]
},
{
"id": "error-creating-user",
"from": "base",
"routesVariants": ["create-user:error"]
},
{
"id": "users-with-long-name",
"from": "base",
"routesVariants": ["get-users:long-names", "get-user:long-name"]
}
]