RubyMine 2024.2 Help

Testing Node.js

With RubyMine, you can test Node.js applications using numerous frameworks.

The current page describes testing with the built-in Node.js test runner or with Mocha, which is a JavaScript test framework that is especially helpful for executing asynchronous test scenarios. You can run Mocha tests from outside RubyMine, examine test results arranged in a treeview, and easily navigate to the test source from there. Next to the test, in the editor, RubyMine shows the status of the test with an option to quickly run or debug it.

Before you start

  1. Download and install Node.js.

  2. Make sure the JavaScript and TypeScript and Node.js required plugins are enabled on the Settings | Plugins page, tab Installed. For more information, refer to Managing plugins.

Built-in Node.js test runner

Starting with version 20, Node.js comes shipped with a stable version of the built-in Node.js test runner. RubyMine supports integration with the built-in test runner, so you can run your tests without installing and configuring any third-party frameworks.

Testing TypeScript code is also supported, see Testing TypeScript for details.

  1. Make sure you have Node.js version 20 or later installed on your computer and configured as a local Node.js interpreter.

  2. Create test files as described on the Node.js official website.

  3. In the gutter:

    • Click the Run test icon next to a single test to run it.

    • Click the Run All tests icon next to a test suite to run all tests in it.

    Run tests from the editor with Node.js built-in test runner
  4. Examine the test results in the Run tool window, learn more from Explore test results.

  5. Rerun tests:

    • To rerun a single test, select it in the Run tool window and select Run '<test name>' from its context menu or press Alt+Shift+R.

      Rerun a single test from the Run tool window

      Alternatively, use the gutter icon next to the test to rerun. Click the Run Ignored Test icon, the Rerun Failed Test icon or the Rerun Successful Test icon to rerun an ignored, failed, or successful test.

      Rerun a single test from the gutter
    • To rerun the whole suite, click The Rerun '<suite name>' icon on the toolbar of the Run tool window.

      Rerun test suite

      Alternatively, click the gutter icon next to the suite.

      Rerun suite from the gutter

Test your code with a third-party testing framework

Although you can use any framework, the recommended one is Mocha.

Install Mocha

  1. In the embedded Terminal (Alt+F12) , 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.

    Learn more from Getting Started on the Mocha official website.

  2. Install the Chai expectation library as an efficient replacement for Node.js standard assert function.

    In the embedded Terminal (Alt+F12) , type:

    npm install --save-dev chai

Write Mocha tests

Run Mocha tests

With RubyMine, 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.

Run a single Mocha test from the editor

  • Click the Run button or the Rerun button in the gutter and select Run <test_name> from the list.

    Run a Mocha test from the editor

    You can also see whether a test has passed or failed right in the editor, thanks to the test status icons Test passed and Test failed in the gutter.

    Test status in the gutter

Create a Mocha run configuration

  1. Open the Run/Debug Configuration dialog (Run | Edit Configurations in 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.

  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 test folder, specify the path to this folder in the pattern relative to the working directory, for example, ./folder1/folder2/test/*.test.js.

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

Run tests via a Mocha run configuration

  1. Select the Mocha run/debug configuration from the list of configurations and click the Run icon   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.

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

    Monitor test execution
Mocha: Monitor tests

Rerun failed Mocha tests

  • Click the Rerun Failed Tests icon on the test results toolbar. RubyMine will execute all the tests that failed during the previous session.

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

    Rerun single failed test from the editor: context menu
  • Alternatively, click the Failed Test icon 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

For more information, refer to Rerunning tests.

Navigation

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

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

  • 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. The test file opens in the editor with the caret placed at the test definition.

  • For failed tests, RubyMine 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

If a test fails for unclear reason you can debug the test to find out whether the test is outdated or the changes you made to the source code break the intended behavior of your application.

With RubyMine, 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.

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.

  2. Click the Run button or the Rerun button in the gutter and select Debug <test_name> from the list.

  3. In the Debug tool window, proceed as usual: step through the program, stop and resume the program execution, examine it when suspended, view actual HTML DOM, run JavaScript code snippets in the Console, and so on.

Debug Mocha test

Debug test with a Mocha 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.

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

  3. Select the Mocha run/debug configuration from the list of configurations and click the Debug icon   in the list or on the toolbar.

  4. In the Debug tool window that opens, proceed as usual: step through the tests, stop and resume test execution, examine the test when suspended, run JavaScript code snippets in the Debugger Console, and so on.

Learn more from Debug failed tests.

Monitor code coverage

With RubyMine, you can also monitor how much of your code is covered with Mocha tests. RubyMine 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. To monitor coverage, you need to install nyc, the command-line interface for Istanbul.

Install nyc

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

    npm install --save-dev nyc

Run tests with coverage

  1. Launch the tests:

    • Create a Mocha run/debug configuration as described above, select it from the list on the main toolbar, and click Run with Coverage  to the right of the list.

      Launch Mocha tests with coverage
    • Alternatively, run a specific suite or a test with coverage from the editor: click the Run button or the Rerun button in the left gutter and select Run <test_name> with Coverage from the list.

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

    Mocha tests: coverage report

Run tests with Node.js inside a Docker container

With RubyMine, you can run Mocha tests inside a Docker container just in the same way as you do it locally.

Before you start

  1. Install the Node.js and Node.js Remote Interpreter plugins on the Settings | Plugins page, tab Marketplace, as described in Installing plugins from JetBrains Marketplace.

  2. Make sure the Docker plugin is enabled in the settings. Press Ctrl+Alt+S to open settings and then select Plugins. Click the Installed tab. In the search field, type Docker. For more information about plugins, refer to Managing plugins.

  3. Download, install, and configure Docker as described in Docker

  4. Configure a Node.js remote interpreter in Docker or via Docker Compose and set it as default in your project. Also make sure the package manager associated with this remote interpreter is set as project default.

  5. Open your package.json and make sure the required test framework is listed in the devDependencies section:

    { "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", "concurrently": "^6.3.0", "eslint": "^8.1.0", "http-server": "^14.0.0", "jest": "^27.3.1", "mocha": "^9.1.3", "nyc": "^15.1.0" } }
  6. Right-click anywhere in the editor and select Run '<package manager> install' from the context menu.

Run tests

  1. Create tests according to the instructions from the Mocha official website.

  2. 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 and Debug tests above.

Last modified: 08 October 2024