On this page:
- Before you start
- Installing Mocha
- Quick test launch
- Creating a Mocha run/debug configuration
- Launching tests via a run/debug configuration
Before you start
- Make sure the Node.js runtime environment is installed on your computer.
- Make sure the NodeJS repository plugin is installed and enabled. The plugin is not bundled with IntelliJ IDEA, but it can be installed from the JetBrains plugin repository as described in Installing, Updating and Uninstalling Repository Plugins and Enabling and Disabling Plugins.
Open the built-in IntelliJ IDEA Terminal (press Alt+F12 or choose on the main menu)
and at the command line prompt type
npm install --global mocha for global installation or
npm install --save-dev mocha to install Mocha as a development dependency.
See NPM documentation for more details about installing the Mocha test runner and npm operation modes. You can also install the
mocha package on the Node.js and NPM page of the Settings dialog box
as described in Installing and Removing External Software Using Node Package Manager.
Quick test launch
You can run and debug Mocha tests and suites right from the editor:
click or in the left gutter and choose Run <test_file_name>
or Debug <test_file_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.
Creating a Mocha run/debug configuration
Open the Run/Debug Configuration dialog box by doing one of the
- On the main menu, choose .
- 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.
Click the Add button on the toolbar and select the Mocha configuration type. The Run/Debug Configuration: Mocha dialog box opens.
- Specify the Node interpreter to use and the location of the mocha package.
working directory of the application.
All references in test scripts will be resolved relative to this folder, unless such references use full paths.
By default, the field shows the project root folder. To change this predefined setting, choose the desired folder from the drop-down list, or type the path manually, or click the Browse button and select the location in the dialog box, that opens.
Specify the tests to run:
- All in directory: choose this option to run all the tests from files stored in a folder. In the Test directory field, specify the folder with the tests. To have IntelliJ IDEA look for tests in the subfolders under the specified directory, select the Include subdirectories check box.
- File pattern: choose this option to have IntelliJ IDEA look for tests in all the files with the names that match a certain mask and specify the mask in the Test file pattern field.
- Test file: choose this option to run only the tests from one file and specify the path to this file in the Test file field.
- Suite: choose this option to run individual suites from a test file. In the Test file field, specify the file where the required suites are defined. Click the Suite name field and configure a list of suites to run. To add a suite to the list, click and type the name of the required suite. To remove a suite, select it in the list and click
- Test: choose this option to run individual tests from a test file. In the Test file field, specify the file where the required tests are defined. Click the Test name field and configure a list of tests to run. To add a test to the list, click and type the name of the required test. To remove a test, select it in the list and click
- Choose the interface used in the test to run.
Launching tests via a run/debug configuration
- To launch the tests according to a run configuration, select the Mocha run/debug configuration from the list on the main toolbar and click the Run button 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.