# Mocha

> **TL;DR**
> Required plugins:
>
>
>
> `JavaScript and TypeScript` -    the plugin is bundled with IntelliJ IDEA and enabled by default.
>
>
>
> `Node.js` -    install the plugin on the Settings | Plugins page, tab Marketplace.     The plugin is available only in IntelliJ IDEA with the Ultimate subscription.

[Mocha](http://mochajs.org/)  is a JavaScript test framework that is especially helpful for executing asynchronous test scenarios. You can run Mocha tests from outside IntelliJ IDEA, examine test results arranged in a treeview, and easily navigate to the test source from there. Next to the test, in the editor, IntelliJ IDEA shows the status of the test with an option to quickly run or debug it.

## Before you start

Procedure:

1. Download and install [Node.js](http://nodejs.org/#download).

2. Make sure the  JavaScript and TypeScript plugin is enabled in the settings.  Press `Ctrl+Alt+S` (Windows), `⌘ Comma` (macOS), `⌘ Comma` (IntelliJ IDEA Classic (macOS)), `⌘ Comma` (macOS System Shortcuts), `Ctrl+Alt+S` (XWin), `Ctrl+Alt+S` (GNOME), `Ctrl+Alt+S` (KDE), `Ctrl+Alt+S` (Emacs), `Ctrl+Alt+S` (Sublime Text), `⌘ Comma` (Sublime Text (macOS)), `Ctrl+Alt+S` (NetBeans), `Ctrl+Alt+S` (Visual Studio), `⌘ Comma` (Visual Studio (macOS)), `Ctrl+Alt+S` (Eclipse), `⌘ Comma` (Eclipse (macOS)) to open settings and then select `Plugins`.  Click the Installed tab. In the search field, type JavaScript and TypeScript. For more information about plugins, refer to [Managing plugins](managing-plugins.html).

3. Install and enable the Node.js plugin on the Settings | Plugins page, tab Marketplace, as described in [Installing plugins from
JetBrains Marketplace](managing-plugins.html#install_plugin_from_repo).     The plugin is available only in IntelliJ IDEA with the Ultimate subscription.

## Install Mocha

Procedure:

* In your command-line shell or in the embedded Terminal (`Alt+F12` (Windows), `⌥ F12` (macOS), `⌥ F12` (IntelliJ IDEA Classic (macOS)), `⌘ ⌃ T` (macOS System Shortcuts), `Alt+F12` (XWin), `Alt+F12` (GNOME), `Alt+F12` (KDE), `Alt+F12` (Emacs), `Alt+F12` (Sublime Text), `⌥ F12` (Sublime Text (macOS)), `Alt+F12` (NetBeans), `Ctrl+Alt+1` (Visual Studio), `⌘ ⌃ 1` (Visual Studio (macOS)), `Alt+F12` (Eclipse), `⌥ F12` (Eclipse (macOS))) , type one of the following commands:

* `npm install mocha` for local installation in your project.

* `npm install -g mocha` for global installation.

* `npm install --save-dev mocha` to install Mocha as a [development dependency](https://docs.npmjs.com/getting-started/using-a-package.json#specifying-packages).

Learn more from [Getting Started on the Mocha official website](https://mochajs.org/#installation).

## Write tests

Procedure:

* Create tests according to the instructions from the [Mocha
official website](https://mochajs.org/).

* Mark the folder where the unit tests are stored as a test source folder, see [Content roots](content-roots.html).

## Run 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.

Procedure: Run a single test from the editor

* Click ![the Run button](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app-client.expui.run.run.svg) or ![the Rerun button](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.actions.rerun.svg) in the gutter and select Run <test_name> from the list.

![Run a Mocha test from the editor](https://resources.jetbrains.com.cn/help/img/idea/2026.2/ws_node_test_run_single_test.png)

You can also see whether a test has passed or failed right in the editor, thanks to the test status  icons ![Test passed](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.runConfigurations.testState.green2.svg) and ![Test failed](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.runConfigurations.testState.red2.svg) in the gutter.

![Test status in the gutter](https://resources.jetbrains.com.cn/help/img/idea/2026.2/ws_node_test_mocha_status_gutter.png)

Procedure: Create a Mocha run configuration

1. Open the [Run/Debug Configuration](run-debug-configurations-dialog.html) dialog (`Run | Edit Configurations` in the main menu), click ![the Add button](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.general.add.svg) in the left-hand pane, and select Mocha from the list.  The [Run/Debug Configuration: Mocha](run-debug-configuration-mocha.html) dialog opens.

2. Specify the Node.js runtime to use and the location of the `mocha` package.

3. 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.

4. Optionally:

Configure rerunning tests automatically on changes in the related source files. To do that, add the `--watch` flag in the Extra Mocha options field.

5. 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 matching files, for example, `*.test.js`.  If the files with tests are stored in a separate folder, for example, `test`, specify the path to this folder in the pattern relative to the working directory:

```SHELL
./folder1/folder2/test/*.test.js
```

6. Choose the [interface](http://mochajs.org/#interfaces) used in the test to run.

Procedure: Run tests via a Mocha run configuration

1. Select the Mocha run/debug configuration from the list of configurations and click ![the Run icon](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.run.run.svg) in the list or on the toolbar.

2. Monitor test execution and analyze test results in the Test Runner tab of the Run tool window. For more information, refer to [Explore test results](viewing-and-exploring-test-results.html).

* Use the Click to see the difference link to open the Difference viewer and compare the actual result with the expectation.

* The name of file with the selected test is show as a link. Click this link to jump to the source code.

3. Monitor test execution and analyze test results in the Test Runner tab of the Run tool window. For more information, refer to [Explore test results](viewing-and-exploring-test-results.html).

![Monitor test execution](https://resources.jetbrains.com.cn/help/img/idea/2026.2/ws_node_test_mocha_monitor_tests_and_navigation.png)

Procedure: Rerun failed tests

1. Click ![the Rerun Failed Tests icon](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app-client.expui.run.restartFailedTests.svg) on the test results toolbar. IntelliJ IDEA will execute all the tests that failed during the previous session.

![Mocha: rerun all failed tests](https://resources.jetbrains.com.cn/help/img/idea/2026.2/ws_node_test_mocha_rerun_failed_tests.png)

2. To rerun a specific failed test, select `Run <test name>` on its context menu.

![Rerun single failed test from the editor: context menu](https://resources.jetbrains.com.cn/help/img/idea/2026.2/ws_node_test_mocha_rerun_one_failed_test_context_menu.png)

3. Alternatively, click ![the Failed Test icon](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.runConfigurations.testState.red2.svg) in the gutter next to a failed test and select `Run <test name>` from the list.

![Rerun single failed test from the editor: gutter icon](https://resources.jetbrains.com.cn/help/img/idea/2026.2/ws_node_test_mocha_rerun_one_failed_test_gutter_icon.png)

## Navigation

With IntelliJ IDEA, you can jump between a file and the related test file or from a test result in the [Test Runner Tab](null) to the test.

> **Tip:**
> Such navigation works only if the test file follows popular naming conventions, for example, has a `.test.`, `.spec.`, or `_spec.` suffix and is located either next to the source file or in a `test` folder.

Procedure:

* 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` (Windows), `⌘ ⇧ T` (macOS), `⌘ ⇧ T` (IntelliJ IDEA Classic (macOS)), `⌃ ⇧ T` (macOS System Shortcuts), `Ctrl+Shift+T` (XWin), `Ctrl+Shift+T` (GNOME), `Ctrl+Shift+T` (KDE), `Ctrl+Shift+T` (Emacs), `Ctrl+Shift+T` (Sublime Text), `Ctrl+Shift+T` (Sublime Text (macOS)), `Ctrl+Shift+T` (NetBeans), `Ctrl+Shift+T` (Visual Studio), `Ctrl+Shift+T` (Visual Studio (macOS)), `Ctrl+Shift+T` (Eclipse), `Ctrl+Shift+T` (Eclipse (macOS)).

* 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` (Windows), `⌘ ↓` (macOS), `F4` (IntelliJ IDEA Classic (macOS)), `F4` (macOS System Shortcuts), `F4` (XWin), `F4` (GNOME), `F4` (KDE), `F4` (Emacs), `F4` (Sublime Text), `F4` (Sublime Text (macOS)), `F4` (NetBeans), `F4` (Visual Studio), `⌘ ↓` (Visual Studio (macOS)), `F12` (Eclipse), `F3` (Eclipse (macOS)). The test file opens in the editor with the caret placed at the test definition.

* For failed tests, IntelliJ IDEA 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.

## Debug 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.

Procedure: Start debugging a single test from the editor

1. Set a breakpoint in the gutter next to the test you want to debug.  You can jump to a failed test by double-clicking it in the Run tool window or by pressing `F4` (Windows), `⌘ ↓` (macOS), `F4` (IntelliJ IDEA Classic (macOS)), `F4` (macOS System Shortcuts), `F4` (XWin), `F4` (GNOME), `F4` (KDE), `F4` (Emacs), `F4` (Sublime Text), `F4` (Sublime Text (macOS)), `F4` (NetBeans), `F4` (Visual Studio), `⌘ ↓` (Visual Studio (macOS)), `F12` (Eclipse), `F3` (Eclipse (macOS)).

2. Click ![the Run button](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app-client.expui.run.run.svg) or ![the Rerun button](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.actions.rerun.svg) in the gutter and select Debug <test_name> from the list.

3. In the Debug tool window,   proceed as usual: [step through the program](stepping-through-the-program.html),  [stop
and resume](starting-the-debugger-session.html#pause-resume)  program execution, [examine it when suspended](examining-suspended-program.html), explore the call stack and variables, set watches, evaluate variables, [view actual HTML DOM](viewing-actual-html-dom.html), and so on.

![Debug Mocha test](https://resources.jetbrains.com.cn/help/img/idea/2026.2/ws_node_test_mocha_debug.animated.gif)

Procedure: Launch test debugging via a run/debug configuration

1. Set the breakpoints next to the tests that you want to debug.  You can jump to a failed test by double-clicking it in the Run tool window or by pressing `F4` (Windows), `⌘ ↓` (macOS), `F4` (IntelliJ IDEA Classic (macOS)), `F4` (macOS System Shortcuts), `F4` (XWin), `F4` (GNOME), `F4` (KDE), `F4` (Emacs), `F4` (Sublime Text), `F4` (Sublime Text (macOS)), `F4` (NetBeans), `F4` (Visual Studio), `⌘ ↓` (Visual Studio (macOS)), `F12` (Eclipse), `F3` (Eclipse (macOS)).

2. Create a Mocha run/debug configuration [as described
above](unit-testing-node-js.html#createRunConfigMocha).

3. Select the Mocha run/debug configuration from the list of configurations and click ![the Debug icon](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.run.run.svg) in the list or on the toolbar.

4. In the [Debug tool window](debug-tool-window.html) that opens,   proceed as usual: [step through the program](stepping-through-the-program.html),  [stop
and resume](starting-the-debugger-session.html#pause-resume)  program execution, [examine it when suspended](examining-suspended-program.html), explore the call stack and variables, set watches, evaluate variables, [view actual HTML DOM](viewing-actual-html-dom.html), and so on.

To ignore source maps and step through the actually executed code, press ![the Disable Source Maps toggle button](https://resources.jetbrains.com.cn/help/img/idea/2026.2/sourceMapDisable.png) on the toolbar of the Debug tool window.

## Monitor code coverage

With IntelliJ IDEA,    you can also monitor how much of your code is [covered with Mocha tests](http://en.wikipedia.org/wiki/Code_coverage). IntelliJ IDEA 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  (`Alt+1` (Windows), `⌘ 1` (macOS), `⌘ 1` (IntelliJ IDEA Classic (macOS)), `⌘ 1` (macOS System Shortcuts), `Alt+1` (XWin), `Alt+1` (GNOME), `Alt+1` (KDE), `Alt+1` (Emacs), `Alt+1` (Sublime Text), `⌘ 1` (Sublime Text (macOS)), `Ctrl+1` (NetBeans), `Ctrl+Alt+L` (Visual Studio), `⌘ ⌥ L` (Visual Studio (macOS)), `Alt+1` (Eclipse), `Alt+1` (Eclipse (macOS))) .  To monitor coverage, you need to install [nyc](https://github.com/istanbuljs/nyc), the command-line interface for [Istanbul](https://istanbul.js.org/).

Procedure: Install nyc

* In your command-line shell or in the embedded Terminal (`Alt+F12` (Windows), `⌥ F12` (macOS), `⌥ F12` (IntelliJ IDEA Classic (macOS)), `⌘ ⌃ T` (macOS System Shortcuts), `Alt+F12` (XWin), `Alt+F12` (GNOME), `Alt+F12` (KDE), `Alt+F12` (Emacs), `Alt+F12` (Sublime Text), `⌥ F12` (Sublime Text (macOS)), `Alt+F12` (NetBeans), `Ctrl+Alt+1` (Visual Studio), `⌘ ⌃ 1` (Visual Studio (macOS)), `Alt+F12` (Eclipse), `⌥ F12` (Eclipse (macOS))) , type:

`npm install --save-dev nyc`

> **Tip:**
> You can also install Istanbul itself, version 1.1.0 or later. These versions have support for ES6 and TypeScript.

Procedure: Run tests with coverage

1. Launch the tests:

* Create a Mocha run/debug configuration [as described above](#createRunConfigMocha), select it from the list on the main toolbar, and click ![Run with Coverage](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.general.runWithCoverage.svg) to the right of the list. ![Launch Mocha tests with coverage](https://resources.jetbrains.com.cn/help/img/idea/2026.2/ws_node_test_mocha_run_with_coverage.png)

* Alternatively, run a specific suite or a test with coverage from the editor: click ![the Run button](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app-client.expui.run.run.svg) or ![the Rerun button](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.actions.rerun.svg) in the left gutter and select Run <test_name> with Coverage from the list.

2. Monitor the code coverage in the [Coverage](code-coverage.html) 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.

![Mocha tests: coverage report](https://resources.jetbrains.com.cn/help/img/idea/2026.2/ws_mocha-coverage-report.png)

## Run Mocha tests with Node.js inside a Docker container

With IntelliJ IDEA, you can run [Mocha](http://mochajs.org/) tests inside a Docker container just in the same way as you do it locally.

Procedure: Before you start

1. Install and enable the Node.js Remote Interpreter plugin on the Settings | Plugins page, tab Marketplace, as described in [Installing plugins from
JetBrains Marketplace](managing-plugins.html#install_plugin_from_repo).

2. Make sure the Node.js and Docker required plugins are enabled on the Settings | Plugins page, tab Installed. For more information, refer to [Managing plugins](managing-plugins.html).

3. Download, install, and configure Docker as described in [Docker](docker.html).

4. [Configure a Node.js remote
runtime in Docker](node-with-docker.html#ws_node_docker_configure_interpreter) or via [Docker Compose](node-with-docker-compose.html#ws_node_docker_compose_configure_interpreter) and [set it as default](node-with-docker.html#ws_node_docker_manage_dependencies) in your project. Also make sure the package manager associated with this remote runtime is [set as project default](node-with-docker.html#ws_node_docker_manage_dependencies).

5. Open your `package.json` and make sure Mocha is listed in the `devDependencies` section:

```JSON
{
    "name": "node-express",
    "version": "0.0.0",
    "private": true,
    "dependencies": {
        "cookie-parser": "~1.4.4",
        "debug": "~2.6.9",
        "express": "~4.16.1",
        "http-errors": "~1.6.3",
        "morgan": "~1.9.1",
        "pug": "^3.0.2"
    },
    "devDependencies": {
        "chai": "^4.3.4",
        "eslint": "^8.1.0",
        "http-server": "^14.0.0",
        "mocha": "^9.1.3"
    }
}
```

6. Right-click anywhere in the editor and select Run '<package manager> install' from the context menu.

Procedure: Run tests

1. Create tests according to the instructions from the [Mocha
official website](https://mochajs.org/).

2. Mark the folder where the unit tests are stored as a test source folder, see [Content roots](content-roots.html).

3. Proceed as with local development: run and debug single tests right from the editor or create a run/debug configuration to launch some or all of your tests as described in [Run tests](#ws_mocha_running) and [Debug
tests](#ws_mocha_debugging) above.

## See also

### Reference

[Run/Debug Configuration: Mocha](run-debug-configuration-mocha.html)

