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
pretty simple right, that’s dependency.js and here is another file 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
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
and in our test file, we would have something like this
and it works like charm we can see that
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 :).