The JS Code Coverage Debug application allows administrators and application developers to log the server-side scripts triggered during a user session and then review which lines of code the system ran.

Users with the js_coverage_debugger role can debug server-side scripts without having to set breakpoints or review onscreen debug messages. Instead, the system saves script usage data in the JavaScript Code Coverage [sys_js_code_coverage] table. Each JavaScript Code Coverage record contains:
  • The user session that called the script.
  • The script record the system called identified by table, sys_id, and script field.
  • The script record the system called identified by type and name.
  • The transaction that called the script.
  • The start time of the transaction.
  • The contents of the script field highlighted to indicate which lines the system ran.
Note: The JS Code Coverage Debug application doesn't log information for client-side scripts.
Figure 1. Sample code coverage highlighting
Sample code coverage highlighting

JS Code Coverage highlighting

The JS Code Coverage application highlights script fields to indicate whether the system ran or skipped each line.

Figure 2. Sample code highlighting
Sample code highlighting

The color of the highlight indicates how the system evaluated the code line.

Table 1. Meaning of code highlighting
Highlight color Meaning
Green This is an executable line of code that the system ran during the session.
Red This is an executable line of code that the system skipped for some reason. The system may have skipped an executable line of code because the necessary script conditions were not met or because the script function was never called. You may want to use the Script Debugger to determine why the system skipped the line of executable code.
Gray This is a non-executable line of code such as white space, code comment, or a portion of an expression split across multiple lines that cannot run on its own.

Administrators and application developers can use this information to conduct more targeted debugging activities such as using the Script Debugger to determine why script conditions are not being met.

Activate JS Code Coverage Debug

You can activate the JS Code Coverage Debug plugin (com.glide.js.coverage) if you have the admin role.

Before you begin

Role required: admin

Procedure

  1. Navigate to All > System Applications > All Available Applications > All.
  2. Find the plugin using the filter criteria and search bar.

    You can search for the plugin by its name or ID. If you cannot find a plugin, you might have to request it from ServiceNow personnel.

  3. Select Install to start the installation process.
    Note: When domain separation and delegated admin are enabled in an instance, the administrative user must be in the global domain. Otherwise, the following error appears: Application installation is unavailable because another operation is running: Plugin Activation for <plugin name>.
    You will see a message after installation is completed. For information about the components installed with a plugin, see Find components installed with an application.

What to do next

To see the components the plugin installed, refresh the plugin form and select the Plugin Files related list.

Debug with JS Code Coverage Debug

Use JS Code Coverage Debug to record a user session and then review which server-side scripts and lines of code the system ran.

Before you begin

Role required: js_coverage_debugger or admin

Procedure

  1. Navigate to All > JS Code Coverage Debug > Enable Coverage.
    The system logs which server-side scripts and code lines the system runs as well as displays session debug messages in the JS Code Coverage namespace.
    Debug with code coverage
  2. Navigate to the table or page whose logic you want to test.
    For example, navigate to Incident > Create New.
  3. Trigger the server-side script or scripts you want to test.
    For example, create an incident with an associate CI item to test several business rules.
  4. When you have completed testing, navigate to JS Code Coverage Debug > Disable Coverage.
    The system stops logging script and code lines run.
  5. Navigate to JS Code Coverage Debug > Coverage Data.
    The system displays the list of coverage data associated with the current user session.
    Sample JavaScript Code Coverage records
  6. Select the script or transaction you want to review.
    Table 2. JavaScript Code Coverage fields
    Field Description
    Script Name Displays the script run by table name, sys_id value, and script field.
    Script Reference Displays the script run by script type and name.
    Transaction Name Displays the transaction that called the script by thread ID and URI.
    For example, select the Script Reference Business Rule: incident events.
    The system displays the JS Code Coverage Debug record.
    JS code coverage debug section
  7. Review the Script field to determine which lines of code the system ran.
    For example, the business rule added the incident.inserted event to the event queue.

Result

You determine which lines of code the system ran.

What to do next

Use the code coverage information to do more targeted debugging activities such as set breakpoints and review variable values with the Script Debugger.