IntelliJ IDEA 2017.2 Help

Keymap:

Testing JavaScript with Mocha

This feature is supported in the Ultimate edition only.

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.

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.

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 Go To | Test or Go To | Test Subject on the context menu, or just press Ctrl+Shift+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. The test file opens in the editor with the cursor placed at the test definition.

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.

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.

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: