superagent-mocker
REST API mocker for the browsers. LOOK MA NO BACKEND! 👐
Written for superagent.
Install
npm i superagent-mocker
Usage
Setup
var request = ;var mock = request;
Timeout
You can provide custom timeout, that can be a function or a number. Just set
timeout
property to the mock
:
var mock = ; // set just numbermocktimeout = 100; // Or function to get randommock { return Math * 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; request ;
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; request ;
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
Or you can remove only one specified route (by method and url)
// to register routemock ... // to remove registered handlermock;
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()
method, that will rollback all patches that mock(superagent)
call make.
License
MIT © Shuvalov Anton