node package manager
Love JavaScript? Your insights can make it even better. Take the 2017 JavaScript Ecosystem Survey »

mock-office

Mock Office

travis build status npm version Coverage Status

Tool for running mock servers locally to faciliate frontend development.

  • Create and run multiple HTTP and Web Socket servers instantly.
  • Cover complex scenarios, including delays, backend error responses, custom headers etc.
  • Run it locally or on shared resources, CI.
  • Use GUI or REST to manage servers.

Table of contents:

  1. Installation
  2. Usage
  3. Introduction
    1. Behaviours
    2. Events
    3. Reactions
  4. Adding server
  5. Adding behaviour
    1. Behaviour configuration
    2. Event configuration
    3. Reaction configuration
  6. Fallback mode
  7. Development
  8. Test

Instalation

Install from npm:

$ npm run install mock-office -g

Usage

$ mock-office [--gui]

It exposes:

  • REST API: http://localhost:3060
  • GUI: http://localhost:3070

Introduction

Mock Office works like respository of mock servers. Idea was to have one common interface for spawning and managing HTTP and Web Socket servers.

You have two ways to interact with it.

  • REST API
  • GUI

Behaviours

Mocks are done by adding behaviours to servers. Behaviour is simply pair of event and reactions. Every type of server has its own accepted event types and reactions.

Events

When something happens(e.g. incoming request, client connected to server) server generates event objects which are being compared with behaviour event requirements and if match succeeds then reactions trigger.

Reactions

Reactions are simply server commands e.g. send a http response, start sending web socket messages.

Adding server

POST /add-server

key type required default description
name string yes - identify your server
type string yes - server type, it can be 'http' or 'ws'
port number no 3000 listening port
fallbackUrl string no '' if provided it triggers fallback mode

Adding behaviours

POST /add-behaviour

key type required default description
serverId string yes - the server id to which we want to add the behavior
behaviour object yes - behaviour config object

Behaviour options

key type required default description
event event config object yes - object describing event which must happen to trigger reactions, see event types and they params for specific servers
reactions array of reaction config object yes - list of reaction configs

Event options

key type required default description
type string yes - event type
params ajv schema properties object no {} custom params specific to the event type

Reaction options

key type required default description
type string yes - reaction type
params ajv schema properties object no {} custom params specific to the event type
schedule schedule config object no {} schedule params e.g. delay, interval

Schedule options

key type required default description
delay number no 0 reaction delay in ms
interval number no 0 interval in ms

Fallback mode

Fallback mode let you use your real API while mocking some endpoints not ready yet. This is done by mock server proxying request to fallback API when it can't find behaviour. Nevertheless when in fallback mode you can cover existing endpoints with mocked counterparts.

Development

To start app development clone repo first and then:

$ npm i
$ npm run watch # runs webpack dev-server for GUI development 
$ npm run serve-app # runs node app 
$ npm run serve-app-with-gui # runs node app with GUI events server required for GUI being updated 

Test

To run tests:

$ npm run test