Photo by Louis Hansel @shotsoflouis on Unsplash

Introduction

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.

Copy 1 mkdir test_article ; cd test_article 2 npm 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.

Copy 1 var request = require ( 'request' ) 2 , async = require ( 'async' ) ; 3 4 function getProfile ( username , cb ) { 5 async . waterfall ( [ 6 function ( callback ) { 7 request . get ( 'https://api.github.com/users/' + username , function ( err , response , body ) { 8 if ( err ) return callback ( err ) ; 9 callback ( null , body ) ; 10 } ) ; 11 } 12 ] , cb ) 13 } 14 15 module . exports = getProfile ; 16 17 getProfile ( 'bulkan' , function ( result ) { 18 console . log ( result ) ; 19 } ) ;

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 .

Copy 1 var getProfile = require ( './gh' ) ; 2 3 describe ( 'User Profile' , function ( ) { 4 it ( 'can get user profile' , function ( done ) { 5 getProfile ( 'bulkan' , function ( err , result ) { 6 if ( err ) return done ( err ) ; 7 8 9 result . should . not . be . empty ; 10 done ( ) ; 11 } ) ; 12 } ) ; 13 } ) ;

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.

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

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.