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

emocks

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

emocks

Mock server based on expressjs

  • 3.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15
increased by150%
Maintainers
1
Weekly downloads
 
Created
Source

emocks

emocks is a mocking middleware for express servers.

How does it work?

  • Organize all of your mocks using folder structure, representing your api url
  • You can use HTTP VERBS as file names to create different mocks
  • You can use static .json files for responses or .js modules, that can create dynamic answers

Supported features

  • json, dynamic answers
  • custom headers

Installation

npm install emocks

Example

Assuming we have the following folder structure:
|-- mocks
  |-- users
    |-- GET.json
    |-- :id
      |-- PUT.js
|-- server.js

server.js

const path    = require('path');
const express = require('express');
const emocks  = require('emocks');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());
app.use('/', emocks(path.join(__dirname, './mocks')));
app.listen(3000);

mocks/users/GET.json

[ 
  { "id": 1, "name": "Jon Snow", "state": "Knows nothing" },
  { "id": 2, "name": "Arya Stark", "state": "Knows valar morghulis" }
]

mocks/users/GET.headers

{ "X-Password": "The winter is coming" }

mocks/users/PUT.js

module.exports = function(req, res){
  var result = req.body;
  result.id = req.params.id;
  res.json(result);
}

Api calls

GET /users

Response

HTTP/1.1 200 OK
X-Password: The winter is coming
[ 
  { "id": 1, "name": "Jon Snow", "state": "Knows nothing" },
  { "id": 2, "name": "Arya Stark", "state": "Knows valar morghulis" }
]

PUT /users/2

{ "name": "Arya Stark", "state": "Sees nothing" }

Response

{ "id": 2, "name": "Arya Stark", "state": "Sees nothing" }

Options

/**
 * @param {string} path - absolute path to mocks directory
 * @param {object} options
 */
emocks(path.join(__dirname, './path/to/mocks-folder'), {
    //emulate server response delay
    delay: 1000,
    //custom 'Not Found' handler
    404: function(req, res){ ... },
    //global headers, will be applied to every response
    headers: { "X-Custom-Global-Header": "Hello!" },
});

Now supports typescript!

Additional info

Please offer suggestions via issues. emocks is an abbreviation for express mocks. Any similarity to emacs is unintended.

Keywords

FAQs

Package last updated on 05 Jun 2023

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