All Projects → A → Superagent Mocker

A / Superagent Mocker

Pretty simple in-browser mocks for CRUD and REST API

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Superagent Mocker

Nsubstitute
A friendly substitute for .NET mocking libraries.
Stars: ✭ 1,646 (+1196.06%)
Mutual labels:  testing-tools, mock
mockafka
A testing DSL for kafka-streams
Stars: ✭ 14 (-88.98%)
Mutual labels:  mock, testing-tools
laika
Log, test, intercept and modify Apollo Client's operations
Stars: ✭ 99 (-22.05%)
Mutual labels:  mock, testing-tools
ts-mock-imports
Intuitive mocking library for Typescript class imports
Stars: ✭ 103 (-18.9%)
Mutual labels:  mock, testing-tools
Lyrebird
移动应用插件化测试工作台
Stars: ✭ 663 (+422.05%)
Mutual labels:  testing-tools, mock
main
Mocks Server monorepo
Stars: ✭ 109 (-14.17%)
Mutual labels:  mock, testing-tools
deckard
DNS test harness
Stars: ✭ 28 (-77.95%)
Mutual labels:  mock, testing-tools
Charlatan
Go Interface Mocking Tool
Stars: ✭ 195 (+53.54%)
Mutual labels:  testing-tools, mock
Miragejs
A client-side server to build, test and share your JavaScript app
Stars: ✭ 4,384 (+3351.97%)
Mutual labels:  mock, testing-tools
DeepfakeHTTP
DeepfakeHTTP is a web server that uses HTTP dumps as a source for responses.
Stars: ✭ 373 (+193.7%)
Mutual labels:  mock, testing-tools
mocat
🐈 Mocat is a mocking toolbar that allows you to interactively develop and test network requests.
Stars: ✭ 27 (-78.74%)
Mutual labels:  mock, testing-tools
Mongodb Memory Server
Spinning up mongod in memory for fast tests. If you run tests in parallel this lib helps to spin up dedicated mongodb servers for every test file in MacOS, *nix, Windows or CI environments (in most cases with zero-config).
Stars: ✭ 1,376 (+983.46%)
Mutual labels:  testing-tools, mock
ruby-dns-mock
DNS mock server written on 💎 Ruby. Mimic any DNS records for your test environment with fake DNS server.
Stars: ✭ 50 (-60.63%)
Mutual labels:  mock, testing-tools
Ava Playback
📼 🚀 Record and playback http requests from your ava tests
Stars: ✭ 124 (-2.36%)
Mutual labels:  testing-tools, mock
Mockito Scala
Mockito for Scala language
Stars: ✭ 231 (+81.89%)
Mutual labels:  testing-tools, mock
dubbo-mock
dubbo mock web server
Stars: ✭ 62 (-51.18%)
Mutual labels:  mock, testing-tools
Mockito
Most popular Mocking framework for unit tests written in Java
Stars: ✭ 12,453 (+9705.51%)
Mutual labels:  testing-tools, mock
Mocktopus
Mocking framework for Rust
Stars: ✭ 179 (+40.94%)
Mutual labels:  testing-tools, mock
Retromock
Java library for mocking responses in a Retrofit service.
Stars: ✭ 48 (-62.2%)
Mutual labels:  mock, testing-tools
Webmockr
R library for stubbing and setting expectations on HTTP requests
Stars: ✭ 37 (-70.87%)
Mutual labels:  testing-tools, mock

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].