This guide shows you how to generate code coverage for a JavaScript based application in the command line interface using the RKTracer tool with 3 simple steps. The same workflow applies to JavaScript and TypeScript applications.
Setup RKTracer tool server to capture code coverage
You need to install the NodeJS rkserver app to receive code coverage from the instrumented application at runtime, and it is a one-time job per system. Install rkserver globally with the -g flag, then install the rk4js runtime package.
On Windows:
npm install C:\rktracer\share\rktracer\rkserver-2.0.0.tgz -g npm install C:\rktracer\share\rktracer\rk4js-2.0.11.tgz
On Linux:
npm install /home/rk/rktracer/share/rktracer/rkserver-2.0.0.tgz -g npm install /home/rk/rktracer/share/rktracer/rk4js-2.0.11.tgz
NodeJS setup on Linux
You can download the NodeJS from the official website and unzip it in your home folder, create a symlink to node, and then you need to set the path in the .bashrc file in home.
which node 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 are able to test it.
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. Use rktracer -on to enable instrumentation for the chosen file type, and rktracer -off to disable it.
rktracer -on foldername1 .js -v rktracer -on foldername1 .ts -v rktracer -off foldername .js -v rktracer -off foldername .ts -v
Start RKTracer server to capture code coverage at runtime
Before you start testing your application you need to execute the command rkserver in the project build folder to capture coverage data at runtime.
rkserver
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 the HTML reports.
rkresults
Enable coverage for selected files
Edit rktracer.config in the RKTracer installation folder, go to the end of the file, and add the following information.
Coverage for selected folders
ignore *.js ignore *.ts instrument */power/* */core/* */keys/* never */firmware_loader/* */lockdown/*
ignore *.jsandignore *.tsignore all JavaScript and TypeScript source files from instrumentation.instrumentinstruments source files from the given folders.neverignores the selected folders.
Coverage for selected functions
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 *.jsandignore *.tsignore all JavaScript and TypeScript source files.instrumentinstruments only the listed source files.function-ignore *then ignores all functions in the above files.function-instrument fun_X() fun_Y() fun_Z()targets these specific functions for instrumentation.