Running Unit Tests on Karma
In this section:
- Before you start
- Generating a Karma configuration file
- Creating a Karma run configuration
- Launching unit tests
This server allows you to capture a browser to run tests in, loads the test targets to the captured browser, controls the testing process, and exchanges data between the browser and PhpStorm, so you can view test results without leaving the IDE.
The test server does not necessarily have to be on your machine, it can be launched right from PhpStorm through the Karma test runner.
Before you start
To prepare working with Karma test runner, follow these steps:
Download and install Node.js. The runtime environment is required for two reasons:
- The Karma test runner is started through Node.js.
- NPM, which is a part of the runtime environment, is also the easiest way to download the Karma test runner.
If you are going to use the command line mode, make sure the path to the parent folder of the Node.js executable file and the path to the
npmfolder are added to the
PATHvariable. This enables you to launch the Karma test runner and npm from any folder.
- Make sure the Karma test runner is installed as described in Preparing to Use Karma Test Runner.
- Install and enable the NodeJS repository plugin as described in Installing, Updating and Uninstalling Repository Plugins and Enabling and Disabling Plugins.
- Install and enable the Karma repository plugin. The plugin is not bundled with PhpStorm, but it can be installed from the JetBrains plugin repository as described in Installing, Updating and Uninstalling Repository Plugins and Enabling and Disabling Plugins.
Generating a Karma configuration file
Karma executes unit tests according to a
karma.conf.js configuration file
which is generated semi-automatically in the interactive mode. The instruction below ensures successful
creation of a consistent configuration file
karma.conf.js which in its turn ensures successful
execution of the tests in your project. For more details, see
To create a Karma configuration file, follow these steps:
- In the command line mode, switch to your project directory.
Type the following command at the command line prompt:
Answer the questions to specify the following basic settings:
- The testing framework to use.
- The browsers to be captured automatically.
The patterns that define the location of test files to be involved in testing or excluded from it, for example,
test/*.js. For more details, see http://karma-runner.github.io/0.10/config/files.html.
Creating a Karma run configuration
To create a Karma run configuration, follow these steps:
- Open the Run/Debug Configuration dialog box by doing one of the following:
- Click on the toolbar and select the Karma configuration type.
- In the dialog box that opens, specify the location of the Node.js and
Karma executable files and the path to the
- Apply the changes and close the dialog box.
Launching unit tests
To launch a unit test, follow these steps:
- To launch the tests according to a run configuration, select the Karma run/debug configuration from the list on the main toolbar. Then click the Run button to the right of the list.
- The Karma test server starts automatically without any steps from your side. View and analyze messages from the test server in the Karma Server tab of the Run tool window.