PyCharm 2024.3 Help

Testing TypeScript

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

PyCharm provides out-of-the-box configuration for the ts-node and tsx loaders, so you can run and debug your TypeScript tests without previously compiling them into JavaScript. Alternatively, you can install a custom loader and configure it as described in Create a Node.js Test Runner run/debug configuration.

You can run and debug tests right from the editor or launch a run/debug configuration of the type Node.js Test Runner.

Before you start

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

  2. Install a loader. In the embedded Terminal (Alt+F12) , type:

    • npm install --save-dev ts-node to install ts-node

    • npm install --save-dev tsx to install tsx

Run tests from the editor

  • To run a test, click the Run test icon in the gutter next to it and then select Run '<test name>' from the list.

    Run tests from the editor with Node.js built-in test runner
  • To run all test in a suite, click the Run All tests icon in the gutter next to it and select Run '<suite name>' from the list.

    Run suites from the editor with Node.js built-in test runner

Run tests via a run configuration

Running tests via a run/debug configuration is helpful when you need to run tests from several test files. Another advantage of using a run/debug configuration is the option to configure a custom loader.

Node.js Test Runner run/debug configuration

You can create a run/debug configuration yourself or save, edit, and reuse a temporary run/debug configuration of the type Node.js Test Runner that PyCharm creates when you run a test or a suite from the editor.

  1. Go to Run | Edit Configurations. Alternatively, select Edit Configurations from the Run widget on the toolbar.

    Open the Edit Configurations dialog

    Alternatively, select a previously generated temporary run/debug configuration.

  2. In the Edit Configurations dialog that opens, click the Add button (the Add button) on the toolbar and select Node.js Test Runner from the list.

    The Run/Debug Configurations: Node.js Test Runner dialog opens.

  3. Specify the Node.js interpreter to use, the working directory, and select the range of tests to run.

  4. From the Language list, select TypeScript.

  5. From the Loader list, select the loader to use. By default, integration with tsx and ts -node is provided out-of-the-box.

    Alternatively, install and configure a custom loader. For example, install jiti and type -r jiti/register in the Node options field.

    TypeScript tests: configure a custom loader
  6. Click Run.

    Alternatively, from the Run widget, select an existing run/debug configuration and click the Debug icon next to it.

Examine test results

Test results are shown in the Run tool window.

Test results

Learn more from Explore test results.

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 and select Run '<test name>' from the list.

    Rerun a single test from the gutter
  • To rerun the whole suite, click the gutter icon next to the suite and select Run '<suite name>' from the list.

    Rerun suite from the gutter
  • To rerun all the tests from the previous session, click The Rerun '<suite name>' icon on the toolbar of the Run tool window.

    Rerun test suite

Debug tests

  1. Set the breakpoints as necessary.

  2. Start debugging:

    • To debug a test, click the Run test icon in the gutter next to the test and then select Debug '<test name>' from the list.

    • To debug all tests in a suite, click the Run test icon in the gutter next to the suite and then select Debug '<suite name>' from the list.

      TypeScript testing: debug a suite from the editor
    • Alternatively, create a run/debug configuration of the type Node.js Test Runner as described above, select it from the Run widget, and click the Debug icon next to it.

      TypeScript testing: debug test via a run/debug configuration
Last modified: 31 July 2024