Awesum Stuff

Code Coverage from Functional Tests for Node.js Applications

Node.js is a beauty! (read open-source, cross-platform JavaScript runtime environment for executing JavaScript code server-side in a non-blocking and event driven fashion). So we have a couple of applications running on Node.js and one fine day we realize that we have a lot of functional (Manual/Automated) tests in place and the litmus test for our “testing” would be to see how much percentage of code that they actually cover.

So basically we need how much code coverage can we get from our functional automated tests. It is a cakewalk with unit tests using NYC as detailed by this article where the unit tests are present in the source code and written in the same framework. But we want to find out the code coverage from our functional tests written in some other language/framework.As NYC is based on Istanbul, so for finding code coverage we used Istanbul.

Here goes the solution:- The trick is to launch the Node.js application using Istanbul, then execute the tests (Manual/Automated) to get the code coverage.

Following is a step by step guide for accomplishing this:-

  1. Install Node.js on your machine.
  2. Fetch the application source code from your version management tool(TFS/GitHub/any other)
  3. Once you have the code, open the command prompt in the root source code folder and resolve all dependencies using command:

           npm install

  1. To Install Istanbul, execute the below mentioned following command:-

          npm install istanbul -g    

         Note:  -g: This parameter is used to install a package globally.

  1. Find out the your Application’s entry point JS file from package.json and now launch the         application through Istanbul using the command:

          istanbul cover server/server.js –handle-sigint  

This will launch your application on localhost:3000. Note that Server.js is the entry point for the application that was used to do this POC. It may differ based on the application you are automating.

  1. Now the application is hosted, you can run any tests on your localhost.
  2. After the tests are done, we need to stop the sites using Ctrl+C. This step generates a coverage folder and all report are present in that folder. The index.html file contains the code coverage details.

Limitation: This approach can’t be used to hook into a running application, we need to launch a fresh instance of the application through Istanbul so that we can catch the required code coverage information.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s