Code Coverage for JavaScript Applications
In this 10mins video, you will see how to generate code coverage for JavaScript based application in command line interface using RKTracer Tool with 3 simple steps.
- Enable the RKTracer tool and rebuild the application.
- Test instrumented application and save coverage data.
- Generate code coverage reports
Setup RKTracer tool server to capture code coverage
Prerequisites: Ensure NodeJS is installed from NodeJS official page and it’s in the system path.
You need to install the NodeJS rkserver app to receive code coverage from instrumented application at
runtime and its one-time job per system.
Windows: npm install C:\rktracer\share\rktracer\rkserver-2.0.0.tgz -g
Linux: npm install /home/rk/rktracer/share/rktracer/rkserver-2.0.0.tgz -g
Note: If you have installed NodeJS in /usr/node then you won’t be executing above command
You can check the path using command
which node
You can download the NodeJS from official website and unzip in home folder and create Simulink to
node and then you need to set path in “.bashrc” file in home.
ln -s node-v14.17.5-linux-x64 node
Code Coverage for JavaScript/TypeScript applications example
Identify the project folder that needs to be instrumented and generate coverage data. Please make sure the project is configured and you’re able to test it.
1: Install rktracer runtime in the project folder.
To install the rktracer runtime for nodejs/webpack to collect code coverage at runtime.
Windows: npm install C:\rktracer\share\rktracer\rk4js-2.0.11.tgz
Linux: npm install /home/rk/rktracer/share/rktracer/rk4js-2.0.11.tgz
The above command should be executed from a project folder.
Enable the RKTracer tool
Instrument the selected folder with the rktracer tool. There might be multiple folders in the project, and
based on the requirement, you can do selected instrumentation.
For JavaScript source files
rktracer -on foldername1 .js -v
rktracer -on foldername2 .js -v
For TypeScript source files
rktracer -on foldername1 .ts -v
rktracer -on foldername2 .ts -v
Start RKTracer server to capture code coverage at runtime
Before you start testing your application you need to execute command “rkserver” in the project
build folder to coverage data at runtime. You need to allow port 9999 i.e., localhost:9999
Run Tests on instrumented application
Testing can be unit-testing or functional or integration testing of the application.
Generate Code Coverage reports
Once you have the rk-coverage.txt file, you need to run the “rkresults” command to generate HTML reports. Ensure that you run the “rkresults” command in the application root folder or where you have the “rktracer” folder generated at the time of build/Instrumentation.
rkresults command will search coverage data file “rk-coverage.txt” and map with JSON files (generated during instrumentation) in the rktracer folder and generate the HTML reports. You can manually open the HTML report using index.html.
After generating code coverage, make sure to turn off(un-instrument) the RKTracer tool for the application that we instrument to create reports.
For JavaScript source files
rktracer -off foldername .js -v
rktracer -off foldername2 .js -v
For TypeScript source files
rktracer -off foldername .ts -v
rktracer -off foldername2 .ts -v
Enable coverage for selected files
Generate coverage reports for the selected folders
C:\project\sound\drivers\base\power\
C:\project\sound\drivers\base\power\firmware_loader\
C:\project\sound\drivers\core\
C:\project\sound\security\keys\
C:\project\sound\security\lockdown\
Suppose you need code coverage for source files from three different folders, i.e., core, keys, power, and ignore coverage for folders firmware_loader and lockdown. Edit rktracer.config in the RKTracer installation folder and go to the end of the file add the following information.
ignore *.js
ignore *.ts
instrument */power/* */core/* */keys/*
never */firmware_loader/* */lockdown/*
ignore *.js = Ignores all JavaScript source files from instrumentation
ignore *.ts= Ignores all typescript source files from instrumentation
Instrument = Instrument source files from given folders
never = ignore selected folder
Generate coverage reports only for selected functions from three different files.
fun_X() in source-file-X
fun_Y() in source-file-Y
fun_Z() in source-file-Z
Suppose you need code coverage for selected functions from three different files. Edit rktracer.config in the RKTracer installation folder and go to the end of the file and set the following variables as shown below.
ignore *.js
ignore *.ts
instrument *source-file-X.js *source-file-Y.js *source-file-Z.js
instrument *source-file-X.ts *source-file-Y.ts *source-file-Z.ts
function-ignore *
function-instrument fun_X() fun_Y() fun_Z()
ignore *.js = Ignore all JavaScript source files
instrument *file-X.js *file-Y.js *file-Z.js = Instrument only these three source files.
function-ignore * = Then ignore all functions in the above three files.
ignore *.ts = Ignore all TypeScript source files
instrument *file-X.ts *file-Y.ts *file-Z.ts  = Instrument only these three source files.
nction-ignore * = Then ignore all functions in the above three files.
function-instrument fun_X() fun_Y() fun_Z() = Don’t ignore these three functions from these three files from instrumentation.