Karma integration in WebStorm
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.
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.
Or install it in the current directory using Terminal:
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:
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:
Follow the suggested steps in the configuration dialog.
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.
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.
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:
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.