Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mockyeah

Package Overview
Dependencies
Maintainers
1
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mockyeah

An invaluable service mocking platform built on Express.

  • 0.11.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Mock Yeah Build Status

"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

Introductory tutorial

  1. Create an example project and initialized with NPM
$ mkdir example-app && cd example-app
$ npm init # all defaults will be fine
  1. Install mockyeah
$ npm install mockyeah --save-dev
  1. Create script file and add the source below
$ touch index.js
const mockyeah = require('mockyeah');

mockyeah.get('/hello-world', { text: 'Hello World' });
  1. Run the script file with Node
$ node index.js
  1. Open http://localhost:4001/hello-world

  2. Profit. You should see "Hello World" returned from your mock server.

Testing example

const request = require('supertest')('http://localhost:4041');
const mockyeah = require('mockyeah');

describe('Wondrous service', () => {
  // remove service mocks after each test
  afterEach(() => mockyeah.reset());

  it('should create a mock service that returns an internal error', (done) => {
    // create failing service mock
    mockyeah.get('/wondrous', { status: 500 });

    // assert service mock is working
    request
      .get('/wondrous')
      .expect(500, done);
  });

  it('should create a mock service that returns JSON', (done) => {
    // create service mock that returns json data
    mockyeah.get('/wondrous', { json: { foo: 'bar' } });

    // assert service mock is working
    request
      .get('/wondrous')
      .expect(200, { foo: 'bar' }, done);
  });
});

Package Dependencies

  • Mock Yeah was built and tested with Node v4.2.3
  • Mocha

API

Mock Service Creation API

mockyeah.get(path, options)
mockyeah.put(path, options)
mockyeah.post(path, options)
mockyeah.delete(path, options

Each of the methods above create a mock service with a HTTP verb matching its respective method name.

Arguments
Path

Path to which to respond. Fully supports all Express path matching options.

Options

Response options informing Mock Yeah how to respond to matching requests. Supported options:

  • 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.
  • headers (Object; optional) - Header key value pairs to include in response.
  • 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.
  • latency (Number/Milliseconds; optional) - Used to control the response timing of a response.
  • text (String; optional) - Text to include in response body. Assumes response Content-Type of text/plain.
  • 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.type
  • status (String; optional; default: 200) - HTTP response status code.

Note, only one of the following is permitted per service: filePath, fixture, html, json, or text.

Mock Service Sets API

mockyeah.loadSet(setName)

Creates a set of mock services. Sets support the full Mock Service Creation API documented above. A set is a Node module that returns an array of service parameter objects, each being the definition for a single mock service. The default set file location is ./mockyeah/sets.

Example of usage:

Invoking:

mockyeah.loadSet('example')

Creates mock services from definitions defined in ./mockyeah/sets/example.js

module.exports = [
  {
    method: 'get',
    path: '/say-hello',
    options: {
      text: 'Well, hello there.'
    }
  },
  {
    method: 'get',
    path: '/say-your-lost',
    options: {
      text: 'I\'m lost.',
      status: 404
    }
  },
  {
    method: 'get',
    path: '/say-oh-noes',
    options: {
      text: 'Oh noes!',
      status: 500
    }
  },
  {
    method: 'get',
    path: '/respond-with-a-file',
    options: {
      filePath: './test/fixtures/some-data.json'
    }
  },
  {
    method: 'get',
    path: '/respond-with-a-fixture',
    options: {
      fixture: 'some-data.json'
    }
  },
  {
    method: 'get',
    path: '/wait-to-respond',
    options: {
      text: 'Oh, hey there.',
      latency: 1000
    }
  },
  {
    method: 'get',
    path: '/say-anything-you-want',
    options: (req, res) => {
      res.status(200);
      res.send('Inversion of service control enables you to respond with whatever you want.');
    }
  }
];

Mock Service Management Methods

mockyeah.reset()

Resets all existing mock services. Useful on test teardown.

mockyeah.close()

Shuts down the Mock Yeah Express server. Useful if running Mock Yeah with a file watcher. Mock Yeah attempts to start a new instance of Express each test iteration. After all tests run, mockyeah.close() should be called to shutdown Mock Yeah's Express server. Failing to do so will result in EADDRINUSE exceptions. This is due to Mock Yeah attempting to start a server on a port occupied by a server it started previously.

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

Keywords

FAQs

Package last updated on 28 Jan 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc