Skip to content
Pretty simple in-browser mocks for CRUD and REST API
Branch: master
Clone or download
Latest commit 4f6f789 Feb 14, 2017
Type Name Latest commit message Commit time
Failed to load latest commit information.
.eslintrc Fix incorrect values in .eslintrc. Feb 5, 2016
.gitignore re #16 add coverage tests Oct 5, 2015
.travis.yml Bump to the latest node in CI Sep 29, 2016 Bump to 0.5.2 Sep 30, 2016
index.js Call callback only once (even on error) Jan 25, 2017
mocha.opts inital commit May 12, 2015
package.json Bump to 0.5.2 Sep 30, 2016 NPM badges in the readme Feb 11, 2017
test.js Treat other 20x statuses (besides 200) as success Sep 30, 2016


Build Status Coverage Status npm version npm downloads

REST API mocker for the browsers. LOOK MA NO BACKEND! 👐

Written for superagent.


npm i superagent-mocker



var request = require('superagent');
var mock = require('superagent-mocker')(request);


You can provide custom timeout, that can be a function or a number. Just set timeout property to the mock:

var mock = require('superagent-mocker');

// set just number
mock.timeout = 100;

// Or function to get random
mock.timeout = function () {
  return Math.random() * 1e4 |0;


You may set headers using the mock.set(). To ensure header keys are not case sensitive, all keys will be transformed to lower case (see example).

mock.get('/topics/:id', function(req) {
  return {
    content: 'Hello World!',
    headers: req.headers

  .set({ 'X-Custom-Header': 'value of header' })
  .end(function(err, data) {
    console.log(data); // { id: 1, content: 'Hello World', headers: { 'x-custom-header': 'value of header' } }

mock.del() works in a similar way.


You may set the body of a POST request as the second parameter of or in mock.send(). Values set in send() will overwrite previously set values.'/topics/:id', function(req) {
  return {
    body: req.body

  .post('/topics/5', {
    content: 'I will be overwritten',
    fromPost: 'Foo'
    content: 'Hello world',
    fromSend: 'Bar'
  .end(function(err, data) {
    console.log(data); // { id: 5, body: { content: 'Hello world', fromPost: 'Foo', fromSend: 'Bar' } }

mock.put(), mock.patch() methods works in a similar way.


You can remove all of the route handlers by calling mock.clearRoutes(). This is useful when defining temporary route handlers for unit tests.

// Using the mocha testing framework
define('My API module', function(){

    // Guarentee each test knows exactly which routes are defined

  it('should GET /me', function(done){
    mock.get('/me', function(){done()})

  it('should POST /me', function(done){
    // The GET route handler no longer exists
    // So there is no chance to see a false positive
    // if the function actually calls GET /me'/me', function(){done()})


Or you can remove only one specified route (by method and url)

// to register route
mock.get('/me', function(){done()})


// to remove registered handler
mock.clearRoute('get', '/me');

Rollback library effect

In some cases it will be useful to remove patches from superagent lib after using mocks. In this cases you can use mock.unmock(superagent) method, that will rollback all patches that mock(superagent) call make.


MIT © Shuvalov Anton

You can’t perform that action at this time.