This feature is only supported in the Ultimate edition.
You can run and debug tests with Mocha
right in IntelliJ IDEA.
You can see the test results in a treeview and easily navigate to the test source from there.
Test status is shown next to the test in the editor with an option to quickly run it or debug it.
Before you start
-
Make sure the Node.js
runtime environment
is installed on your computer.
- Install and enable the NodeJS repository plugin on the Plugins page as described in Installing, Updating and Uninstalling Repository Plugins and Enabling and Disabling Plugins.
Installing Mocha
Open the built-in IntelliJ IDEA Terminal (⌥F12⌥F12⌥F12⌥F12⌥F12) and type one of the following commands at the command prompt:
-
npm install mochafor local installation in your project. -
npm install -g mochafor global installation. -
npm install --save-dev mochato install Mocha as a development dependency
.
.
You can also install the mocha package on the Node.js and NPM page
as described in NPM.
Running tests
With IntelliJ IDEA, you can quickly run a single Mocha 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
or
in the left gutter
and choose Run <test_name> from the pop-up list.
You can also see whether a test has passed or failed right in the editor,
thanks to the test status icons
in the left gutter.
To create a Mocha run configuration
-
Open the Run/Debug Configuration dialog box
( on the main menu).
Alternatively, select a test file in the Project tool window and choose Create <file name> on the context menu.
-
Click the
on the toolbar and select Mocha from the list.
The Run/Debug Configuration: Mocha dialog box opens.
-
Specify the Node interpreter to use and the location of the
mochapackage. -
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.
-
Optionally configure rerunning the tests automatically on changes in the related source files.
To do that, add the
--watchflag in the Extra Mocha options field. - Specify the tests to run. This can be a specific test or suite, an entire test file, or a folder with test files. You can also define patterns to run only the tests from the matching files.
-
Choose the interface
used in the test to run.
To run tests via a run configuration
-
Select the Mocha run/debug configuration from the list on the main toolbar
and click
to the right of the list.
- The test server starts automatically without any steps from your side. View and analyze messages from the test server in the Run tool window.
- Monitor test execution in the Test Runner tab of the Run tool window as described in Monitoring and Managing Tests.
Navigation
With IntelliJ IDEA, you can jump between a file and the related test file. Navigation from a test result in the Test Runner Tab to the test is also supported.
To jump between a file and the related test file
Open the file in the editor and choose or
on the context menu, or just press ⇧⌘T⇧⌘T⌃⇧T⌃⇧T.
The test file should follow popular naming conventions,
e.g. have a .test., .spec. or _spec. suffix and should be located either next to the source file or in a test folder.
To jump from a test result to the test
Select the test name in the Test Runner tab and choose Jump to Source on the context menu.

Debugging tests
With IntelliJ IDEA, you can quickly start debugging a single Mocha 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
or
in the left gutter
and choose Debug <test_name> from the pop-up list.
To launch test debugging via a run/debug configuration
- Create a Mocha run/debug configuration as described above.
-
Select the Mocha run/debug configuration from the list on the main toolbar
and click
to the right of the list.
- In the Debug Tool Window that opens, proceed as usual: step through the tests, stop and resume test execution, examine the test when suspended, etc.
Monitoring code coverage
With IntelliJ IDEA,
you can also monitor how much of your code is
covered with Mocha tests
.
IntelliJ IDEA displays this statistics in a dedicated tool window and marks covered and uncovered lines visually right in the editor.
To monitor coverage, you need to install nyc
, the command-line interface for Istanbul
.
To install nyc
Open the built-in IntelliJ IDEA Terminal (⌥F12⌥F12⌥F12⌥F12⌥F12)
and type npm install --save-dev nyc.
You can also install Istanbul itself, version 1.1.0 or later. These versions have support for ES6 and TypeScript.
To run tests with coverage
- Create a Mocha run/debug configuration as described above.
-
Select the Mocha run/debug configuration from the list on the main toolbar
and click
to the right of the list.
Alternatively, quickly run a specific suite or a test with coverage from the editor: click
or
in the left gutter
and choose Run <test_name> with Coverage from the pop-up list.
-
Monitor the code coverage in the Coverage tool window.
The report shows how many files were covered with tests and the percentage of covered lines in them.
From the report you can jump to the file and see what lines were covered – marked green – and what lines were not covered – marked red:
