Skip to content
Get Free Trial

Javascript Code Coverage

Sahi Pro can measure how much of the web application's JavaScript your tests run, by function and by decision point, for each JavaScript file.

Passing tests don’t tell you how much of the application they checked. JS Code Coverage measures how much of the application’s JavaScript your tests actually ran: which functions were called, and which branches of each decision, such as both sides of an if, were taken. Parts that no test reaches are the ones to write tests for next.

Set this in userdata/config/userdata.properties:

js.code_coverage.enabled.default=true

In the Run Configuration, open Advanced Settings and tick JS Code Coverage:

Advanced Settings in the Run Configuration: Offline Reports, Retry Count with Aborted and Failure, Show Periodic Report Summary, Timeout (Minutes), Enable Autoheal, Enable Autothread, Send Email Reports, JS Code Coverage, Extra Parameters and User Defined ID

See Run Tests via Editor for the other settings.

Open the suite report of the run, and click the JS Code Coverage tab:

The JS Code Coverage tab for codecoverage_test.sah: Total Scripts 5, Total Functions 5, Total Decision Points 10, Total Functions Covered 5, Total Decision Points Covered 5, Total Functions Covered 100% in green with a thumbs-up and Total Decision Points Covered 50% in red with a thumbs-down; below, a table of the five JavaScript files script1.js to script5.js on sahitest.com, each with Functions Covered 1 of 1 (100%) and Decision Points Covered 1 of 2 (50%)

The summary at the top shows, for the whole run:

  • Total Scripts: the number of JavaScript files the run loaded.
  • Total Functions and Total Functions Covered: how many functions the files have, and how many the run called.
  • Total Decision Points and Total Decision Points Covered: how many branches the files have, such as each side of an if or else, and how many the run took.

Below it, the table shows the same numbers for each JavaScript file.

A percentage above 50% is green with a thumbs-up. 50% or less is red with a thumbs-down.

Click a file to see its code, with the number of times each function and branch ran next to it. A count of 0, in red, is code that no test ran:

script1.js with performAction1() marked 1 in green, the if (flag) branch marked 1 in green, and the else branch marked 0 in red

In this example, the test clicked the button that calls performAction1(), so the function and its if branch ran once. The else branch never ran, so half the decision points are covered.