Monitoring Code Coverage for JavaScript
Code Coverage shows how much of your code is covered with tests and marks covered and uncovered lines visually right in the editor.
In this section:
- Prerequisites
- Enabling running tests with coverage in Karma
- Measuring code coverage for tests executed on JSTestDriver
- Measuring code coverage for tests executed on Karma
Prerequisites
- The JSTestDriver or Karma test runner is installed and configured.
- Additional third-party testing frameworks, for example, Jasmine, QUnit, or Mocha, are downloaded and integrated with IntelliJ IDEA.
- A runner-specific configuration file is generated or written manually.
For more details, see Preparing to Use Karma Test Runner or Preparing to Use JSTestDriver Test Runner depending on your choice.
Enabling running tests with coverage in Karma
Coverage for tests run in Karma is calculated by the istanbul coverage tool.
The tool is shipped within the karma-coverage
package.
Therefore running tests with coverage in Karma requires that you download the karma-coverage
package and manually update
the karma.conf.js
configuration file.
The karma-coverage
package should be installed in the same folder with Karma (karma (library home)
package),
no matter whether Karma is installed globally or in your project.
Keep this restriction in mind when installing karma-coverage
manually in the command line mode.
If you are installing karma-coverage
through the Node Package Manager interface, the proper installation folder will be chosen automatically.
-
Install the
karma-coverage
package through the Node Package Manager (npm).- Open the Settings / Preferences Dialog by pressing Ctrl+Alt+S or by choosing for Windows and Linux or for OS X, and click Node.js and NPM under Languages&Frameworks.
- On the Node.js and NPM page that opens, click Install in the Packages area.
-
In the Available Packages dialog box that opens, select the
karma-coverage
package, click Install Package, and close the dialog box when ready. IntelliJ IDEA returns to the Node.js page, where thekarma-coverage
package is added to the Packages list. Click OK.
For details, see Installing and Removing External Software Using Node Package Manager.
-
Open the
karma.conf.js
configuration file and add the following information to it manually:
Measuring code coverage for tests executed on JSTestDriver
- Prepare tests manually or have tests generated by IntelliJ IDEA.
-
Create a JSTestDriver configuration file
*.jstd
or*.conf
. For details, see JSTestDriver Configuration file. -
Create a run configuration of the type JSTestDriver.
To have specific files excluded from coverage analysis, specify the paths to them in the Coverage tab of the Run/Debug Configuration: JSTestDriver dialog box.
- Start the JSTestDriver server and capture a browser to run the tests in.
-
On the main toolbar, select the JSTestDriver run configuration in the Run/Debug Configurations drop-down list
and click the Run with Coverage button
.
- Monitor the code coverage in the Coverage tool window.
Measuring code coverage for tests executed on Karma
- Prepare tests manually or have tests generated by IntelliJ IDEA.
-
Create a Karma configuration file
karma.conf
. For details, see Running Unit Tests on Karma. - Create a run configuration of the type Karma.
-
On the main toolbar, select the Karma run configuration in the Run/Debug Configurations drop-down list
and click the Run with Coverage button
.
-
Monitor the code coverage in the Coverage tool window.
Note that when the tests are executed on
Karma
the Coverage tool window does not contain the Generate Coverage Report toolbar buttonbecause a coverage report is actually generated on the disk every time Karma tests are run. The format of a coverage report can be configured in the configuration file, for example:
The following// karma.conf.js module.exports = function(config) { config.set({ ... // optionally, configure the reporter coverageReporter: { type : 'html', dir : 'coverage/' } ... });};
type
values are acceptable:html
produces a bunch of HTML files with annotated source codelcovonly
produces anlcov.info
filelcov
produces HTML +.lcov
files. This format is applied by default.cobertura
produces acobertura-coverage.xml
file for easy Hudson integrationtext-summary
produces a compact text summary of coverage, typically to the consoletext
produces a detailed text table with coverage for all files