Navigate back to the homepage

Testing With Mocha, Sinon.js & Mocking Request

Bulkan Evcimen
October 7th, 2013 · 2 min read

Photo by Louis Hansel @shotsoflouis on Unsplash


Writing tests with Mocha is really fun and satisfying. Combined with should.js for expectations/assertions and mocking/stubbing with sinon.js I think it becomes very powerful test environment.

In this article I will show you how to get started with test mocking/stubbing by showing an example which stubs out the get method on mikeal/request. I also assume you are somewhat familiar with Node.js and have it installed.

Before we start create a directory and install the dependencies needed.

1mkdir test_article; cd test_article
2npm install mocha should sinon request async

GET some data

Here is a function that will get any users public GitHub profile from using the GitHub API. We will use the async module to help with the asyncronousness.

1var request = require('request')
2 , async = require('async');
4function getProfile(username, cb){
5 async.waterfall([
6 function(callback){
7 request.get('' + username, function(err, response, body){
8 if (err) return callback(err);
9 callback(null, body);
10 });
11 }
12 ], cb)
15module.exports = getProfile;
17getProfile('bulkan', function(result){
18 console.log(result);

We require the two packages we need, then define a function which accepts the GitHub username & a callback function.

async.waterfall takes an array of functions as its argument then calls them one by one passing the values from that is passed to the callback to the next function. For more details read the official description here.

The first function in async.waterfall function list does a request to GitHub API passing the body to the next function. We dont have have another function so async.waterfall will call the callback we passed into getProfile with err and result as its argument. Its a good idea to read the official description of the waterfall function.

Last, we export our function as the module so we can use it later.

Tests taste better with Mocha

To test the above code we can write a Mocha test assuming the above code is in a file named gh.js.

1var getProfile = require('./gh');
3describe('User Profile', function(){
4 it('can get user profile', function(done){
5 getProfile('bulkan', function(err, result){
6 if(err) return done(err);
8 // simple should.js assertion
10 done();
11 });
12 });

We can run the above test via the following line assuming the Mocha test is in a file named gh_test.js

./node_modules/.bin/mocha --require should --ui bdd gh_test.js

The problem with this test is that each time we run it will do a HTTP GET to GitHub API which will be a slow. The more tests we add that do actual HTTP requests to third parties will slow the tests even more. What we can do is we can mock out the request.

We can change the test code.

1var request = require('request')
2 , sinon = require('sinon')
3 , getProfile = require('./gh');
5describe('User Profile', function(){
6 before(function(){
7 sinon
8 .stub(request, 'get')
9 .yields(null, null, JSON.stringify({login: "bulkan"}));
10 });
12 after(function(){
13 request.get.restore();
14 });
16 it('can get user profile', function(done){
17 getProfile('bulkan', function(err, result){
18 if(err) return done(err);
21 done();
22 });
23 });

We add a before call that stubs out request.get.

The yields allows us to simulate the call to the callback that is passed to request.get. In this case we return null for err, null for the response and JSON string of a simple object.

The after call restores the default request.get method.

Our test case tests that request.get was called.

In Node.js require(..) loads modules once into a cache. As our test case runs first it will load the request module first. We use the reference to the module to be able to stub methods on it. Later on when we load getProfile module it will do a require('request') call which will retrieve the module from the cache with the get method stubbed out.

I hope this example helps you in your testing.

More articles from Bulkan

Export Test Cases From Quality Center Using Python

Photo by Dieter de Vroomen on Unsplash Here is a Python script that will export out test cases in a folder from Quality Center into a CSV…

May 10th, 2011 · 1 min read

Building a Twitter Filter With CherryPy, Redis, and tweetstream`

Photo by Brooke Lark on Unsplash Background All the code is available at Since reading this post by…

March 18th, 2010 · 3 min read
© 2007–2020 Bulkan
Link to $ to $ to $