PhpStorm 8.0.2 Web Help

Running Unit Tests on Karma

JavScript unit tests are run in a browser against a test server which actually handles the testing process.

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 Cucumber.js test runner.

Karma executes unit tests according to a karma.conf.js configuration file which is generated semi-automatically in the interactive mode.

On this page:

Prerequisites

  1. Download and install Node.js. The framework is required for two reasons:
    • The transpiledLanguage toolType is started through Node.js.
    • NPM, which is a part of the framework, is also the easiest way to download the transpiledLanguage toolType.

  2. If you are going to use the command line mode, make sure the following paths are added to the PATH variable:
    1. The path to the parent folder of the Node.js executable file.
    2. The path to the npm folder.

    This enables you to launch the transpiledLanguage toolType and npm from any folder.

  3. Make sure the Karma test runner is installed as described in Preparing to Use Karma Test Runner.
  4. Install and enable the NodeJS repository plugin.

    The plugin is not bundled with PhpStorm, but it is available from the PhpStorm plugin repository. Once enabled, the plugin is available at the IDE level, that is, you can use it in all your PhpStorm projects. See Installing, Updating and Uninstalling Repository Plugins and Enabling and Disabling Plugins for details.

  5. Install and enable the Karma repository plugin.

    The plugin is not bundled with PhpStorm, but it is available from the PhpStorm plugin repository. Once enabled, the plugin is available at the IDE level, that is, you can use it in all your PhpStorm projects. See Installing, Updating and Uninstalling Repository Plugins and Enabling and Disabling Plugins for details.

Generating a Karma configuration file

  1. In the command line mode, switch to your project directory.
  2. Type the following command at the command line prompt:
    karma init

    If Karma does not start, check the installation: the parent folder or the Karma executable file should be specified in the PATH variable.

  3. 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, src/*.js and test/*.js. For more details, see http://karma-runner.github.io/0.10/config/files.html.

Creating a Karma run configuration

  1. Open the Run/Debug Configuration dialog box by doing one of the following:
    • On the main menu, choose Run | Edit Configurations .
    • Press RunConfiguration and choose Edit Configuration on the context menu.
    • Open the test file in the editor, and then choose Create <file name> on the context menu.
    • Select the test file in the Project tool window, and then choose Create <file name> on the context menu of the selection.
  2. Click the Add button add on the toolbar and select the Karma configuration type.
  3. In the dialog box that opens, specify the location of the Node.js and Karma executable files and the path to the karma.conf.js configuration file.
  4. Apply the changes and close the dialog box.

Launching unit tests

  1. To launch the tests according to a run configuration, select the Karma run/debug configuration from the list on the main toolbar.
    ws_select_run_configuration.png
    Then click the Run button run.png  to the right of the list.
  2. 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.
  3. Monitor test execution in the Test Runner tab of the Run tool window as described in Monitoring and Managing Tests.

See Also

Last modified: 10 December 2014