hubcap coverage

Get JavaScript code coverage data for the current page.

When to use

Use coverage to identify unused JavaScript code. For the most accurate results, run this after performing page interactions so that event handlers and lazy code paths are exercised. Use csscoverage for CSS rule coverage instead.

Usage

hubcap coverage

Arguments

None.

Flags

None.

Output

Returns JavaScript code coverage data for each script.

FieldTypeDescription
[].scriptIdstringIdentifier of the script
[].urlstringURL of the script
[].functionsarrayArray of function coverage entries
[].functions[].functionNamestringName of the function
[].functions[].rangesarrayArray of coverage ranges with start/end offsets and count
[
  {
    "scriptId": "42",
    "url": "https://example.com/app.js",
    "functions": [
      {
        "functionName": "init",
        "ranges": [
          {"startOffset": 0, "endOffset": 120, "count": 1}
        ]
      },
      {
        "functionName": "unusedHelper",
        "ranges": [
          {"startOffset": 121, "endOffset": 200, "count": 0}
        ]
      }
    ]
  }
]

Errors

ConditionExit codeStderr
Chrome not connected2error: connecting to Chrome: ...
Timeout3error: timeout

Examples

Get JavaScript coverage:

hubcap coverage

Find functions that were never called:

hubcap coverage | jq '[.[] | .functions[] | select(.ranges[0].count == 0) | .functionName]'

Interact with the page first, then measure coverage:

hubcap goto --wait "https://example.com" && hubcap click "#menu-toggle" && hubcap click "#submit" && hubcap coverage | jq '[.[] | {url, unused: [.functions[] | select(.ranges[0].count == 0)] | length}]'

See also