0.5.0 • Published 4 years ago

superagent-nock v0.5.0

Weekly downloads
953
License
-
Repository
github
Last release
4 years ago

superagent-nock

Very simple mock of superagent http requests for testing purpose (from Node.js or the browser). Inspired by superagent-mocker, superagent-mock and nock.

Used for testing React components with Redux and Observable.

Usage

Setup

import request from 'superagent';
import nocker from 'superagent-nock';
const nock = nocker(request);

Use

Define the base url

nock('http://localhost')

The url to mock

nock.get('/events/10')

The result to return

nock.reply(httpStatus, responseBody, responseHeaders, foo)

or specify a function

nock.reply(function(url) {
   return {
      status: 200,
      result: responseBody,
      headers: {'access-token': 'example-JSON-Web-Token'},
      foo: {
        something,
      },
   };
})

Then, when you do a get request on the url, the callback return the specified result

nock('http://localhost')
   .get('/events/10')
   .reply(200, {
    	id: 10,
      title: 'My event'
   });

request
   .get('http://localhost/events/10')
   .end((err, res) => {
      console.log(res.body); // { id: 10, title: 'My event'}
   };

Chaining

You can chain your urls to mock:

nock('http://localhost')
   .get('/events/10')
   .reply(200, {
	    id: 10,
	    title: 'My event'
   })
	 .get('/members/1')
   .reply(404);

Install

You should probably install it in devDependencies (-D)

$ npm i -D superagent-nock

Next TODO

nock.delay nock.query

0.5.0

4 years ago

0.4.0

6 years ago

0.3.1

6 years ago

0.3.0

7 years ago

0.1.2

7 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago