RubyMine 2021.3 Help

Karma

Before you start

  1. Download and install Node.js.

  2. Install and enable the Karma plugin on the Settings/Preferences | Plugins page, tab Marketplace, as described in Installing plugins from JetBrains repository.

Installing Karma and plugins

  • In the embedded Terminal (Alt+F12) , type one of the following commands:

    • npm install if Karma and all the required plugins are already defined in package.json.

    • To install Karma and the required plugins (for example karma-jasmine or jasmine-core) as development dependencies:

      npm install --save-dev karma npm install --save-dev <required_karma_plugin> <another_required_karma_plugin>

    Learn more from the Karma official website.

Generating a Karma configuration file

Karma tests are run according to a karma.conf.js configuration file which is generated in the interactive mode. If you already have karma.conf.js in your project, just skip this step. For more details on Karma configuration, see Karma official website.

To create a Karma configuration file

  1. Open the Terminal and start the karma.conf.js generation wizard by typing one of the following depending on your operating system:

    • For macOS and Linux: ./node_modules/karma/bin/karma init

    • For Windows:

      npm install -g karma-cli karma init
  2. Answering the questions of the wizard, specify the testing framework to use and the browsers to be captured automatically.

    See also Karma Files: Pattern matching.

Running tests

With RubyMine, you can quickly run a single Karma test right from the editor or create a run/debug configuration to execute some or all of your tests.

To run a single test from the editor

  • Click the Run button or the Rerun button in the left gutter and select Run <test_name> from the list.

    You can also see whether a test has passed or failed right in the editor, thanks to the test status icons Test passed and Test failed in the gutter.

To create a Karma run configuration

  1. Open the Run/Debug Configuration dialog (Run | Edit Configurations on the main menu), click the Add button in the left-hand pane, and select Karma from the list. The Run/Debug Configuration: Karma dialog opens.

  2. Specify the Node.js interpreter to use. This can be a local Node.js interpreter or a Node.js on Windows Subsystem for Linux.

    Optionally, specify the Node.js-specific option parameters and the environment variables to be passed to Node.js.

  3. Specify the location of the karma package and the path to karma.conf.js.

  4. Specify the working directory of the application. By default, the Working directory field shows the project root folder. To change this predefined setting, specify the path to the desired folder or choose a previously used folder from the list.

  5. Optionally, specify the command-line options that you want to pass to Karma to override the default settings from the karma.conf.js configuration file.

    For example, to run or debug tests in Headless Chrome, type --browsers ChromeHeadless in the Karma options field. See Automated testing with Headless Chrome for details.

    To see all the available CLI options, type karma start --help in the TerminalAlt+F12.

To run tests via a run configuration

  1. Select the Karma run/debug configuration from the list on the main toolbar and click the Run icon  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 and analyze test results in the Test Runner tab of the Run tool window, see Explore test results for details.

To rerun failed tests

  • In the Test Runner tab, click Rerun Failed Tests button on the toolbar. RubyMine will execute all the tests that failed during the previous session.

    Rerun a failed Karma test
  • To rerun a specific failed test, select Run <test name> on its context menu.

See Rerunning tests for details.

Navigation

With RubyMine, you can jump between a file and the related test file or from a test result in the Test Runner Tab to the test.

  • To jump between a test and its subject or vice versa, open the file in the editor and select Go to | Test or Go to | Test Subject from the context menu, or just press Ctrl+Shift+T.

  • To jump from a test result to the test definition, click the test name in the Test Runner tab twice, or select Jump to Source from the context menu, or just press F4. The test file opens in the editor with the cursor placed at the test definition.

  • For failed tests, RubyMine brings you to the failure line in the test from the stack trace. If the exact line is not in the stack trace, you will be taken to the test definition.

Debugging tests

With RubyMine, you can quickly start debugging a single Karma test right from the editor or create a run/debug configuration to debug some or all of your tests.

To start debugging a single test from the editor

  • Click the Run button or the Rerun button in the left gutter and choose Debug <test_name> from the list.

To launch test debugging via a run/debug configuration

  1. Create a Karma run/debug configuration as described above.

  2. Select the Karma run/debug configuration from the list on the main toolbar and click the Debug icon  to the right of the list.

  3. In the Debug tool window that opens, proceed as usual: step through the tests, stop and resume test execution, examine the test when suspended, run JavaScript code snippets in the Console, and so on.

Monitoring code coverage

With RubyMine, you can also monitor how much of your code is covered with Karma tests. RubyMine displays this statistics in a dedicated Coverage tool window and marks covered and uncovered lines visually in the editor and in the Project tool window. To monitor coverage, you need to install the karma-coverage package and update karma.conf.js.

To install karma-coverage

  • In the embedded Terminal (Alt+F12) , type:

    npm install  --save-dev karma-coverage

To add karma-coverage definition to the configuration file

  1. Open karma.conf.js in the editor.

  2. Locate the reporters definition and add coverage to the list of values in the format:

    reporters: ['progress', 'coverage']
  3. Add a preprocessors definition and specify the coverage scope in the format:

    preprocessors: {'**/*.js': ['coverage']}

To launch tests with coverage

  1. Create a Karma run/debug configuration as described above.

  2. Select the Karma run/debug configuration from the list on the main toolbar and click the Run with Coverage icon  to the right of the list.

    Alternatively, use the test icons in the editor to quickly run a specific suite or a test with coverage.

    ws_mocha_quickly-run-with-coverage.png
  3. Monitor the code coverage in the Coverage tool window.

    Every time Karma tests are run, a coverage report is actually generated on the disk. The format of a coverage report can be configured in the configuration file, for example:

    // karma.conf.js module.exports = function(config) { config.set({ ... // optionally, configure the reporter coverageReporter: { type : 'html', dir : 'coverage/' } ... });};

    The following type values are acceptable:

    • html produces a bunch of HTML files with annotated source code.

    • lcovonly produces an lcov.info file.

    • lcov produces HTML + .lcov files. This format is applied by default.

    • cobertura produces a cobertura-coverage.xml file for easy Hudson integration.

    • text-summary produces a compact text summary of coverage, typically to the console.

    • text produces a detailed text table with coverage for all files.

Last modified: 13 October 2021