Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0
Note
iconfalse

This tutorial describes how to use the Karma test runner in the WebStorm IDE.

Table of Contents

Karma integration in WebStorm

Starting with WebStorm 7.0, you can use the Karma test runner to run JavaScript tests for your project.
Karma integration in WebStorm:

  • Uses local server to run the tests in the selected browsers installed on your computer;
  • Allows you to run tests written with the use of Jasmine, QUnit, or Mocha, or to write a simple adapter to use any other framework you like;
  • Presents test results and code coverage report by the istanbul code coverage engine right in the IDE.

Installing Karma

First, make sure that Node.js is installed on your computer.

Install Karma using npm:

The recommended approach is to install Karma (and all the plugins your project needs) locally in the project's directory.

Use WebStorm’ built-in npm ( Settings | JavaScript | Node.js and NPM) search and install Karma for your project.

Or install it in the current directory using Terminal:

Code Block
npm install karma

Install karma-cli globally:

Use WebStorm’ built-in npm and search for karma-cli, add -g option before hitting Install for global installation.
Or use built-in Terminal to install karma-cli with:

Code Block
npm install -g karma-cli

Karma configuration

To run new tests with Karma, you need to have a Karma configuration file (learn more about it).

To generate new configuration file, for example, karma.conf.js in the project directory run in built-in Terminal:

Code Block
karma init karma.conf.js

Follow the suggested steps in the configuration dialog.

Panel

Note: Most of the framework adapters, reporters, preprocessors and launchers need to be loaded as plugins.

Create a WebStorm Karma Run Configuration:

  • Click Create 'karma.conf.js' in the context menu for karma.conf.js file
  • Check the paths in the Create Run/Debug Configuration window
  • Click OK.

Running tests

Run

A new Karma Configuration will be added to the list of Run/Debug Configurations.
To start running your tests, click Run in the toolbar or in the context menu of Karma configuration file.

Panel

Note: WebStorm disables autoWatch in Karma configuration.
You can rerun your tests with the shortcut Alt+Shift+R on Win/Linux or ctrl-cmd-R on Mac.
Click auto-test in the WebStorm Test Run window to enable automatic test rerun: The test will be run 10 seconds after your code was changed, if there are no syntax errors.

Run with coverage

WebStorm Karma integration allows you to run tests with code coverage provided by the istanbul code coverage engine.

To use Run with coverage feature, a karma-coverage module should be installed via npm.

Make the required changes in the Karma configuration file, for example:

Code Block
reporters: ["coverage"],
    preprocessors: {
        "**/*.js": "coverage"
    }

You can navigate through test results in the Run window, check statistics for the tests execution, and make HTML reports from it. Code coverage report will be also available.