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.
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-coveragepackage through the Node Package Manager (npm).- Open the Settings / Preferences Dialog 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-coveragepackage, click Install Package, and close the dialog box when ready. IntelliJ IDEA returns to the Node.js page, where thekarma-coveragepackage is added to the Packages list. Click OK.
For details, see Installing and Removing External Software Using Node Package Manager.
-
Open the
karma.conf.jsconfiguration file and add the following information to it manually:-
Locate the
reportersdefinition and addcoverageto the list of values in the following format:reporters: ['progress', 'coverage'],
-
Add a
preprocessorsdefinition and specify the coverage scope according to the following format:preprocessors: { '**/*.js': ['coverage'] }
-
Locate the
- Prepare tests manually or have tests generated by IntelliJ IDEA.
-
Create a JSTestDriver configuration file
*.jstdor*.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.
- 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
Karmathe Coverage tool window does not contain the Generate Coverage Report toolbar button
because 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/' } ... });};
typevalues are acceptable:htmlproduces a bunch of HTML files with annotated source codelcovonlyproduces anlcov.infofilelcovproduces HTML +.lcovfiles. This format is applied by default.coberturaproduces acobertura-coverage.xmlfile for easy Hudson integrationtext-summaryproduces a compact text summary of coverage, typically to the consoletextproduces a detailed text table with coverage for all files