Giter VIP home page Giter VIP logo

superagent-mocker's Introduction

superagent-mocker

Build Status

REST API mocker for the browsers. LOOK MA NO BACKEND! ๐Ÿ‘

Written for superagent.

Install

npm i superagent-mocker

Usage

Setup

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

Timeout

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;
}

Get

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

request
  .get('/topics/1')
  .end(function(err, data) {
    console.log(data); // { id: 1, content: 'Hello World' }
  })
;

Post

mock.post('/topics/:id', function(req) {
  return {
    id: req.params.id,
    content: req.body.content
  };
});

request
  .post('/topics/5', { content: 'Hello world' })
  .end(function(err, data) {
    console.log(data); // { id: 5, content: 'Hello world' }
  })
;

mock.put() and mock.del() methods works as well.

Teardown

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(){

  beforeEach(function(){
    // Guarentee each test knows exactly which routes are defined
    mock.clearRoutes()
  })

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

  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
    mock.post('/me', function(){done()})
    api.saveMe()
  })

})

Note

Sadly, but request.send() doesn't work :( Sorry

License

MIT ยฉ Shuvalov Anton

superagent-mocker's People

Contributors

a avatar kilahm avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.