JetBrains Rider 2019.1 Help

Mocha

Mocha is a JavaScript test framework that is especially helpful for executing asynchronous test scenarios. JetBrains Rider integrates with Mocha so you can use it from outside the IDE. 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

  1. Make sure you have Node.js on your computer.

  2. Install and enable the NodeJS repository plugin on the Plugins page as described in Managing plugins.

Installing Mocha

Running tests

With JetBrains Rider, 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 the Run button or the Rerun button in the left gutter and choose 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 ws_icon_test_status.png in the left gutter.

To create a Mocha 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 Mocha from the list. The Run/Debug Configuration: Mocha dialog opens.

  2. Specify the Node interpreter 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 or choose a previously used folder from the list.

  4. Optionally configure rerunning the 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 the matching files.

  6. Choose the interface used in the test to run.

To run tests via a run configuration

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

  2. The test server starts automatically without any steps from your side. View and analyze messages from the test server in the Run tool window.

  3. Monitor test execution in the Test Runner tab of the Run tool window.

To rerun failed tests

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

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

Navigation

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

  • Open the file in the editor and select Go To | Test or Go To | Test Subject from the context menu, or just press N/A.

  • To jump from a test result to the test, select the test name in the Test Runner tab and choose Jump to Source from the context menu. The test file opens in the editor with the cursor placed at the test definition.

  • For failed tests, JetBrains Rider 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 JetBrains Rider, 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 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 Mocha run/debug configuration as described above.

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

  3. In the Debug window that opens, proceed as usual: step through the tests, stop and resume test execution, examine the test when suspended, and so on.

Monitoring code coverage

With JetBrains Rider, you can also monitor how much of your code is covered with Mocha tests. JetBrains Rider 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

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

    npm install --save-dev nyc

To run tests with coverage

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

  2. Select the Mocha run/debug configuration from the list on the main toolbar and click Run with Coverage to the right of the list.
    Alternatively, quickly run a specific suite or a test with coverage from the editor: click the Run buttonun or the Rerun button in the left gutter and choose Run <test_name> with Coverage from the list.

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

    ws_mocha-coverage-report.png

Last modified: 17 July 2019

See Also