Walkthrough Mocking in a NodeJS Application

Reading Time: 3 minutes

Hi folks, in this blog we would be looking how to use the power of javascript moreover the javascript way of mocking for testcases, recently got chance to work on a project on NodeJS me being a JavaScript lover was very exciting for it, it was fun learning it and working on that but when it came for me to write test cases, so as I came from a background of Java and scala where we have terms like mockito, Dependency Injection etc, I thought it must be the same for NodeJS, but conventions like module.export type of things were a little confusing for me at first, but slowly I began to get a grasp of those conventions, but still I wasn’t able to join threads from two backgrounds with context to unit testing, with little research and googling I came across with two amazing libs used for mocking/stubbing in NodeJS those were sinon and rewire

So let’s get started with some code, in this blog I would be using lab as a testing utility, now let’s look at a simple JS file dependency.js that has the following content

dependency.js

pretty simple right, that’s dependency.js and here is another file util.js

util.js

 

here is our file that just requires it as a dependency, that’s a way how node applications require the modules, now in node applications we often require modules like this so let us say we want to mock some required module,  then we can simply use sinon it is a quite powerful tool for mocking, stubbing, spies, we kinda stub dependencies to get the flavor of mocking, here is how we can do it easily, here is my test.js

test.js

 

so when you run the above code you would see, that the second testcase clearly revokes the actual implementation of addInt method and that’s the power of sinon.

Now, the above example lets you easily mock the functions that return something but, what if the function you wanna mock takes a call back, well sinon has also got that cover for you, here is the example let’s add a method in dependency.js that takes a call back,

and here is how it’s used in util.js

 

and in our test file, we would have something like this

 

 

and it works like charm we can see that Its mocked gets printed on the console, that’s how javascript do it seamlessly,

So that’s all in this blog, in the next subsequent blogs we would be looking over how to use rewire and the concept of monkey patching along with some more utilities provided by sinon, here is the repo containing above code, keep coding :).

knoldus-advt-sticker

Written by 

Shubham Verma is a software consultant. He likes to explore new technologies and trends in the IT world. Shubham is familiar with programming languages such as Java, Scala, C, C++, HTML, Javascript and he is currently working on reactive technologies like Scala, Akka , spark and Kafka. His hobbies includes playing computer games and watching hollywood movies.

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!