mockyeah
"An invaluable service mocking platform built on Express."
Testing is difficult when you don't have control of your data. This project puts you in complete control, enabling you to implement real mock web services with ease. Real mock services means you have control of response payloads, HTTP Status Codes, response latency, and more.
Have a requirement to implement specific behavior when a service is slow to respond or a server returns an unexpected status code? No problem! This platform makes developing for such requirements easy.
Install
$ npm install mockyeah --save-dev
Usage
API
Mock service creation API
mockyeah.get(path, options)
mockyeah.put(path, options)
mockyeah.post(path, options)
mockyeah.delete(path, options)
mockyeah.all(path, options)
Each of the methods creates a mock service with a HTTP verb matching its respective method name.
Parameters
Path String
Path to which to mount service. Fully supports all Express path matching options.
Options Object
Response options informing mockyeah how to respond to matching requests. Supported options:
One of the following options may be used per service:
filePath
(String
; optional) - File with contents to include in response body. Assumes response Content-Type of file type.fixture
(String
; optional) - Fixture file with contents to include in response body. Assumes response Content-Type of file type. Default fixture file location is ./mockyeah/fixtures
in your project.html
(String
; optional) - HTML to include in response body. Assumes response Content-Type of text/html
.json
(Object
; optional) - JSON to include in response body. Assumes response Content-Type of application/json
.raw
(String
; optional) - Text to include in response body. Content-Type is the default Express type if not specified in header.text
(String
; optional) - Text to include in response body. Assumes response Content-Type of text/plain
.
Additional options:
headers
(Object
; optional) - Header key value pairs to include in response.latency
(Number
in Milliseconds; optional) - Used to control the response timing of a response.type
(String
; optional) - Content-Type HTTP header to return with response. Proxies option to Express response method res.type(type)
; more info here: http://expressjs.com/en/4x/api.html#res.typestatus
(String
; optional; default: 200
) - HTTP response status code.
Fixture recording and playback
mockyeah.record(name)
name
(String
; required) Directory name to save service responses recordings
(i.e. ./mockyeah/fixtures/[recording name]
).
Configures mockyeah to proxy and record service requests. Recorded responses
are written to ./mockyeah/fixtures
. To use this feature, you must update
the service addresses in your application to proxy through mockyeah. Here is an
example of an address configured for recording:
http://localhost:[mockyeah port]/http://example.com/your/service/url
mockyeah.play(name)
name
(String
; required) Directory name from which to mount contained
service responses recordings (i.e. ./mockyeah/fixtures/[recording name]
).
Mounts each service response captured during a recording. Each service response
will be mounted with exact same payload, headers, status, and latency as
experienced during recording. This behavior may be changed by altering the values
in the captured service response file.
Here is an example of a service response file:
{
"method": "GET",
"url": "http://example.com/some/service",
"path": "/some/service",
"options": {
"headers": {
"x-powered-by": "Express",
"content-type": "text/plain; charset=utf-8",
"content-length": "12",
"etag": "W/\"5-iwTV43ddKY54RV78XKQE1Q\"",
"date": "Sun, 21 Feb 2016 06:17:49 GMT",
"connection": "close"
},
"status": 200,
"raw": "Hello world!",
"latency": 57
}
}
Pseudo recordings may be created manually to ease repetitive setup of multiple
services. Here are the steps to creating a pseudo recording:
- Create a recording directory (e.g.
./mockyeah/fixtures/pseudo-example
) - Add one or more JSON files containing the following properties, at minimum:
{
"method": "GET",
"path": "/some/service",
"options": {
"text": "Hello world!"
}
}
See Mock service creation API for details on supported options
.
- Play your pseudo recording.
require('mockyeah').play('pseudo-example');
- That's it!
Mock service and server management
mockyeah.reset()
Removes all mounted mock services. Useful during after test teardown.
mockyeah.close()
Stops mockyeah Express server. Useful when running mockyeah with a file watcher.
mockyeah will attempt to start a new instance of Express with each iteration of
test execution. After all tests run, mockyeah.close()
should be called to
shutdown mockyeah's Express server. Failing to do so will likely result in
EADDRINUSE
exceptions. This is due to mockyeah attempting to start a server on
an occupied port.
Configuration File
.mockyeah
default configurations
{
"name": "mockyeah",
"host": "localhost",
"port": 4001,
"fixturesDir": "./mockyeah/fixtures"
}
name
: Used to identify the origin of logged output
host
: The host mockyeah will run on
port
: The port mockyeah will run on
fixturesDir
: The relative path to the fixtures directory
Overriding any of these configurations can be done by placing a .mockyeah
file in root of the project and adding the key value pair that needs to be updated.
This file should be written using standard JSON
.
Introductory tutorial
- Create an example project and initialized with NPM
$ mkdir example-app && cd example-app
$ npm init
- Install
mockyeah
$ npm install mockyeah --save-dev
- Create script file and add the source below
$ touch index.js
const mockyeah = require('mockyeah');
mockyeah.get('/hello-world', { text: 'Hello World' });
- Run the script file with Node
$ node index.js
-
Open http://localhost:4001/hello-world
-
Profit. You should see "Hello World" returned from your mock server.
Testing example
const request = require('supertest')('http://localhost:4001');
const mockyeah = require('mockyeah');
describe('Wondrous service', () => {
afterEach(() => mockyeah.reset());
after(() => mockyeah.close());
it('should create a mock service that returns an internal error', (done) => {
mockyeah.get('/wondrous', { status: 500 });
request
.get('/wondrous')
.expect(500, done);
});
it('should create a mock service that returns JSON', (done) => {
mockyeah.get('/wondrous', { json: { foo: 'bar' } });
request
.get('/wondrous')
.expect(200, { foo: 'bar' }, done);
});
});
Package dependencies
- mockyeah was built and tested with Node v4.2.3
- Mocha
Release notes
0.13.0
- Removed
.loadSet()
from API, easy multiple service setup is now possible
with .play()
. - Add fixture
.play()
functionality. See documentation. - Add fixture
.record()
functionality. See documentation. - Implements necessary changes to facilitate mockyeah-cli
- Added greater test coverage.
Contributing
Getting started
Installing project and dependencies
# download project
$ git clone git@github.com:ryanricard/mockyeah.git
$ cd mockyeah
# install proper Node version
$ nvm install v4.2.3
$ nvm use
# install mocha
$ npm install -g mocha
# if tests pass, you're good to go
$ npm test