top

Tools for Promises Unit Testing

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. MochaJS Mocha is a JavaScript test framework running on node.js, featuring browser support, asynchronous testing, test coverage reports, and use of any assertion library. It can be used with libraires like should.js, expect.js and chai.js. ChaiJS Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework. SinonJS SinonJs is a standalone library for test spies, stubs and mocks for JavaScript. Works with any unit testing framework. Installation 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. $ npm i -D mocha chai sinon or $ npm install mocha chai sinon — save-dev Code Coverage 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. $ npm i -D nyc 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. Mocha Hooks 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. Unit Testing 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. https://gist.github.com/ramsunvtech/9debc0eaa04ba19215af1eab269b2b78#file-imported-db-helper-js Unit Testing 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 Async.js Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. Although originally designed for use with Node.js and installable via `npm install — save async`, it can also be used directly in the browser. In this below example, We use async.parallel where we need to trigger the DB Service Method at the same time. Unit Testing 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 !
Rated 4.0/5 based on 0 customer reviews
Normal Mode Dark Mode

Tools for Promises Unit Testing

Venkatraman Ramamoorthy
Blog
21st Feb, 2018
Tools for Promises Unit Testing

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.

MochaJS
Mocha is a JavaScript test framework running on node.js, featuring browser support, asynchronous testing, test coverage reports, and use of any assertion library. It can be used with libraires like should.js, expect.js and chai.js.

ChaiJS
Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.

SinonJS
SinonJs is a standalone library for test spies, stubs and mocks for JavaScript. Works with any unit testing framework.

Installation
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.

$ npm i -D mocha chai sinon
or
$ npm install mocha chai sinon — save-dev

Code Coverage
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.

$ npm i -D nyc

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.

Mocha Hooks
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.

Unit Testing
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.

https://gist.github.com/ramsunvtech/9debc0eaa04ba19215af1eab269b2b78#file-imported-db-helper-js

Unit Testing
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. 

Spieshttp://sinonjs.org/releases/v4.1.3/spies
Stubshttp://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

Async.js
Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. Although originally designed for use with Node.js and installable via `npm install — save async`, it can also be used directly in the browser.

In this below example, We use async.parallel where we need to trigger the DB Service Method at the same time.

Unit Testing
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 !

Venkatraman

Venkatraman Ramamoorthy

Blog Author
Front-end Architect ★, Tech Writer ★, Open Source Enthusiast

Leave a Reply

Your email address will not be published. Required fields are marked *

SUBSCRIBE OUR BLOG

Follow Us On

Share on

other Blogs

20% Discount