0.0.4 • Published 6 years ago

mocknario v0.0.4

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

mocknario

mocknario is a tool to help you develop frontend projects without the need to run a real backend api. As simple as an express server with capability to handle scenarios.

First you define mocks, which is a response for a route. The response can be a simple object, a JSON file or even a promise. Then you define scenarios, which will be a variation of the original mock.

Installation

Either through cloning with git or by using npm (the recommended way):

npm install mocknario --save-dev

Usage

mocknario runs as a standalone server and will listen for requests based on configuration set upon command line. Example:

mocknario serve -m ./examples -p 3000

With the above options mocknario will search for *.mock.js files under the examples folder (relative to current dir) load then as routes and listen to port 3000.

For CLI options, use the --help argument:

mocknario --help

Defining mocks

The simplest mock can be defined by the following snippet having the route detected based on the file name and its location.

File: /examples/users/find.mock.js

const { setMock } = require('mocknario')

setMock({ id: '123', username: 'john', email: 'john@example.org' })

In this case mocknario will serve the example as:

http://localhost:3000/users/find

HTTP/1.1 200 OK
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 57
Content-Type: application/json; charset=utf-8
ETag: W/"39-cAIbdui/IywYjOhdbs16BX/lMHM"
X-Powered-By: Mocknario

{
    "email": "john@example.org",
    "id": "123",
    "username": "john"
}

Documentation

Mocks

setMock(result, options)

Accepts result as object, function or promise.

setFileMock(file, options)

Accepts a file path to be used as result. The file is only read when accessing the route.

Options

optiondefaultdescription
pathdefined by the filename and locationDefine the route path. Ex: /users/find
methodGETThe handled http method
status200The http status response

Scenarios

To create a scenario just chain from one of the mock methods to withScenario.

Example:

const { setMock } = require('mocknario')

setMock({id: '123', username: 'john', email: 'john@example.org' })
.withScenario({
  alias: 'inactive',
  result: ({ body }) => {
    return {
      body: {
        ...body,
        status: 'INACTIVE'
      }
    }
  }
})

withScenario(options)

optiondescription
aliasUsed to identify a scenario in the settings page and in requests.
resultTransformation method. It receives an object containing body and status. The return should be the modified object for that scenario.

Requesting scenarios

Using the settings page

Once the server is started, open the following url:

http://localhost:3000/scenarios

You should see a list of mocks and respective scenarios. You can pick and choose which scenarios to enable/disable. This is going to save a cookie with those settings and any other request made by your browser, for instance from your web application, would apply the those scenarios.

Using HEADER in the request

You can also include a HEADER to your request with the desired scenarios.

http http://localhost:3000/users/find scenarios:[\"inactive\"]

HTTP/1.1 200 OK
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 77
Content-Type: application/json; charset=utf-8
Mocknario: inactive
X-Powered-By: Mocknario

{
    "email": "john@example.org",
    "id": "123",
    "status": "INACTIVE",
    "username": "john"
}

License

MIT