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:-
- Install Node.js on your machine.
- Fetch the application source code from your version management tool(TFS/GitHub/any other)
- Once you have the code, open the command prompt in the root source code folder and resolve all dependencies using command:
- To Install Istanbul, execute the below mentioned following command:-
npm install istanbul -g
Note: -g: This parameter is used to install a package globally.
- 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.
- Now the application is hosted, you can run any tests on your localhost.
- 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.
Categories: Awesum Stuff