Unit Testing is good way to check the Code Quality but handling with Promise code is bit tricky since its a future completion. So we have to use few libraries to achieve it. In this article, we will see how to accomplish it.
Take a look at below snippet which uses Promise API where it resolves the success message after two seconds and you can increase duration as well but console triggers whenever its get the message. In this example, We used setTimeout function to return after two seconds like server response.
Before going to unit test the above code. Lets understand the tools what we are going to use to do the unit testing. Below are the tools.
Use below any one command to install Mocha, Chai and Sinon Module and these must to be installed as Dev Dependencies modules, because it’s not required when application running in the server but during build.
If you are very much interested on how much code covered during unit testing. Try Istanbul which make test coverage simple with various types of report format.
Simple Unit Testing
Lets have a look at the below code. It has very simple function called basic and returns the promise data after 1.6 seconds (Assume that its retrieved from Database after 1.6 seconds).
Unit Testing Basics
As explained in the previous step, Adding Mocha and Chai for this basic Promise Code. In order to write basic unit testing in mocha, understanding this below two methods is must.
* describe — This function helps to groups selected test cases. so its easy to refer and maintain.
* it — This function helps to write your use case and test your library / module and expect the result.
There is few more functions which are considered as Hooks. below are the four important hooks where this can be place inside describe function before any it function.
* before — runs before all tests in this block.
* beforeEach — runs before each test in this block.
* afterEach — runs after each test in this block.
* after — runs after all tests in this block.
In this Unit Testing example, We have main group Basic Testing and three sub groups Basic, Error Case, Success Case and each sub groups has one test case. Scroll this Code below to get basic understanding.
If you are still not clear, then have a look at below explaination about each test cases.
* Basic — We are testing whether basic is a function.
* Error Case — basic should return “Failure”, when there is no parameter passed to the function.
* Success Case — basic should return given parameter as a result for success scenario.
Cool. If you noticed the Success Case and Error Case Scenario, We have used return keyword for Promise method, so that it can wait and complete the operation. Its suggested to return instead of done() for these types of test cases.
Lets see different example where it included external file which is Database library.
Unit Testing with Dependent Files
In Real time, Long files used to be split into number of small files as models, libs and services. Here, We will see service File which is included the db helper file where we get the DB data.
DB Helper File — This is the file which is used above.
This Unit Testing included Mocha, Chai and Sinon Libraries and included file like Service and DB Helper.
Here, you can notice there is before, beforeEach, after hook were added for method request params and response with Sinon Stubs. It actually helps to proxy the calls of DB Library.
Why we need Sinon?
SinonJS has many good features. Below are few methods and we used for Stubs.
Spies — http://sinonjs.org/releases/v4.1.3/spies
Stubs — http://sinonjs.org/releases/v4.1.3/stubs
Mocks — http://sinonjs.org/releases/v4.1.3/mocks
Fake timers — http://sinonjs.org/releases/v4.1.3/fake-timers
Unit Testing with Async.js
In this below example, We use async.parallel where we need to trigger the DB Service Method at the same time.
Similar to Previous example, Added Mocha, Chai and Sinon and added only one example for success scenario.
Mainly we are by passing the DB Methods db.userData and db.activityData and return the custom data using Sinon Stub.
In this test case, we are checking the expected result is deeply equal to the method response.
Click here for the whole project. Happy Coding !