# ermock

> Express middleware to mock requests with js and json files

Latest version **1.1.0** (published 2020-01-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install ermock
pnpm add ermock
yarn add ermock
bun add ermock
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2020-01-14 |
| First published | 2020-01-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6 |
| Dependencies | 5 |
| Unpacked size | 11.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jeppe Hasseriis |
| Maintainers | cenobitedk |
| Keywords | express, middleware, mock, json, node, nodejs |

## Links

- npm: https://www.npmjs.com/package/ermock
- Repository: https://github.com/cenobitedk/ermock
- Homepage: https://github.com/cenobitedk/ermock#readme
- Issues: https://github.com/cenobitedk/ermock/issues
- npm.io page: https://npm.io/package/ermock

## Dependencies (5)

- [cors](https://npm.io/package/cors.md) ^2.8.5
- [debug](https://npm.io/package/debug.md) ^4.1.1
- [rcload](https://npm.io/package/rcload.md) ^1.0.0
- [is-what](https://npm.io/package/is-what.md) ^3.5.0
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^6.1.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.0 (latest) — 2020-01-14
- 1.0.0 — 2020-01-13

## README

# ermock

Express middleware to mock requests with JSON and JS files.

## Features

- File based mocks using JSON and JS files.
- Flexible configuration with [rcload](https://www.npmjs.com/package/rcload).
- Supports preflight requests using [cors](https://www.npmjs.com/package/cors).
- Uses express style path matching with [path-to-regexp](https://www.npmjs.com/package/path-to-regexp).

## Installation

```
npm i ermock
```

## Usage

```
const express = require("express");
const ermock = require("ermock");

const app = express();

// Add middleware as early as possible.
app.use(ermock());

// Setup routes etc.
```

## Setup

It uses [rcload](https://www.npmjs.com/package/rcload) and thus supports the following:

- `ermock` property in `package.json`
- `.ermockrc` file in JSON format
- `.ermockrc.json` or `.ermockrc.js` file
- `ermock.config.js` file exporting a JS object

Furthemore it also accepts a configuration object when setting up in express, eg.:

```
app.use(mocker({
    root: "/api",
    dir: "my-mock-reposnses",
    delay: 500,
    table: { ... }
}));
```

The complete configuration will consist of default values, file based configuration (cosmiconfig) and object based configuration merged in the mentioned order.  
This makes it possible to dynamically overwrite properties, e.g. the `root` prefix upon application load.

## Configuration

The configuration object accepts the following properties:

| Property      | Type             | Default value | Description                                                             |
| ------------- | ---------------- | ------------- | ----------------------------------------------------------------------- |
| `root`        | string           | `""`          | Path prefix, e.g. `"/api"`                                              |
| `dir`         | string           | `""`          | Folder name where mock files can be found, e.g. `"mocks"`               |
| `table`       | string or object | `{}`          | Filename or object with url path as key and filename as key, see below. |
| `delay`       | number           | `0`           | Add delay to reponses in milliseconds.                                  |
| `corsOptions` | object           | `{}`          | Options object to use with [cors](https://www.npmjs.com/package/cors).  |

## Table configuration

The `table` accepts either a filename (relative to application root) or an object with the table configuration.  
If you supply a filename it will be loaded automatically, e.g. `mocks/table.json` or `mock-table.json`.

The object must:

- have `url path` as **key**, starting with forward slash and excluding the `root` prefix.
- have `filename` as **value**, either a `.json` file or `.js` file returning a method.

The path matching is done with [path-to-regexp](https://www.npmjs.com/package/path-to-regexp) using **default options**.

Example of table configuration:

```
// mock-table.json
{
    "/myservice/users": "users.json",
    "/myservice/user/:id": "user.js
}

// users.json
{
    "users": [
        1,
        2
    ]
}

// user.js
module.exports = function (props) {
    const { id } = props;

    if (id === 1) {
        return {
            id,
            name: "Arnold S.",
            email: "illbeback@gmail.com",
            username: "T800",
            permissions: ["ALL"]
        };
    } else {
        return {
            id,
            name: "Connor, Sarah",
            email: "",
            username: "donttrustthemachines",
            permissions: ["RESTRICTED"]
        }
    }

}
```

When a method is returned from the mock file, it is executed with the match object containing named keys and indexed values from the path matched. See the following example:

```
// table config
{
    "/service/subscription/:id([^/\\?]+)(\\?.*)?": "mockfile.js"
}

// req url:
//  "/service/subscription/123?userinfo=true"

// mockfile.js
module.exports = function (props) {
    // props = { '0': '?userinfo=true', id: '123' }
}
```

For detailed configuration of the url path, see the [path-to-regexp readme](https://github.com/pillarjs/path-to-regexp#readme).

## License

MIT

---
_Source: https://npm.io/package/ermock · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
