npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mattwhitney-superagent-mocker

v0.5.5

Published

Pretty simple mocks for the CRUD and REST API

Downloads

3

Readme

superagent-mocker

Build Status Coverage Status npm version npm downloads

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

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 {
    id: req.params.id,
    content: 'Hello World!',
    headers: req.headers
  };
});

request
  .get('/topics/1')
  .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.

Post

You may set the body of a POST request as the second parameter of mock.post() or in mock.send(). Values set in send() will overwrite previously set values.

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

request
  .post('/topics/5', {
    content: 'I will be overwritten',
    fromPost: 'Foo'
  })
  .send({
    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.

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

})

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.

License

MIT © Shuvalov Anton