PhpStorm 2022.2 Help

Jest

Jest is a testing platform for client-side JavaScript applications and React applications specifically. Learn more about the platform from the Jest official website.

You can run and debug tests with Jest right in PhpStorm. 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

Download and install Node.js.

Installing and configuring Jest

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

    npm install --save-dev jest

Learn more from Getting Started and Configuring Jest on the Jest official website.

Running tests

With PhpStorm, you can quickly run a single Jest test right from the editor or create a run/debug configuration to execute some or all of your tests.

For information on how to create Jest tests for JavaScript and TypeScript code, see Getting started on the Jest official website.

To run a single test from the editor

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

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

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

  2. Specify the Node.js interpreter to use. This can be a local Node.js interpreter or a Node.js on Windows Subsystem for Linux.

  3. Specify the location of the jest, react-scripts, react-script-ts, react-super-scripts, or react-awesome-scripts package.

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

  5. Optionally specify the jest.config file to use: select the relevant file from the list, or click the Browse button and select it in the dialog that opens, or just type the path in the field. If the field is empty, PhpStorm looks for a package.json file with a jest key. The search is performed in the file system upwards from the working directory. If no appropriate package.json file is found, then a Jest default configuration is generated on the fly.

  6. Optionally configure rerunning the tests automatically on changes in the related source files. To do that, add the --watch flag in the Jest options field.

To run tests via a run configuration

  1. Select the Jest run/debug configuration from the list on the main toolbar and click the Run icon  to the right of the list.

    ws_select_run_configuration_jest.png
  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 and analyze test results in the Test Runner tab of the Run tool window, see Explore test results for details.

To rerun failed tests

  • In the Test Runner tab, click Rerun Failed Tests button on the toolbar. PhpStorm 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.

See Rerunning tests for details.

Navigation

With PhpStorm, 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 cursor placed at the test definition.

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

    Jump to the failure line from a failed Jest test

Snapshot testing

PhpStorm integration with Jest supports such a great feature as snapshot testing.

When you run a test with a .toMatchSnapshot() method, Jest creates a snapshot file in the __snapshots__ folder. To jump from a test to its related snapshot, click the Go to snapshot (camera) icon in the gutter next to the test or select the required snapshot from the context menu of the .toMatchSnapshot() method.

Jest snapshot testing: jump from a test file to the corresponding snapshot

If a snapshot does not match the rendered application, the test fails. This indicates that either some changes in your code have caused this mismatch or the snapshot is outdated and needs to be updated.

To see what caused this mismatch, open the PhpStorm built-in Difference Viewer via the Click to see difference link in the right-hand pane of the Test Runner tab.

Jest snapshots: compare the expected and actual snapshots in the PhpStorm Difference Viewer

You can update outdated snapshots right from the Test Runner tab of the Run tool window.

  • To update the snapshot for a specific test, use the Click to update snapshot link next to the test name.

  • To update all outdated snapshots for the tests from a file, use the Click to update failed snapshots next to the test filename.

Jest snapshot testing: update snapshots

Debugging tests

With PhpStorm, you can quickly start debugging a single Jest 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 icon in the gutter and select Debug <test_name> from the list.

  • To launch test debugging via a run/debug configuration, create a Jest run/debug configuration as described above. Then select the Jest run/debug configuration from the list on the main toolbar and click the Debug icon  to the right of the list.

    ws_select_run_configuration_jest.png

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

Monitoring code coverage

With PhpStorm, you can also monitor how much of your code is covered with Jest tests. PhpStorm 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 run tests with coverage

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

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

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

    Run Jest tests with coverage from the editor
  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:

    Jest Coverage report

Run Jest tests with Node.js inside a Docker container

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

Before you start

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

  2. Make sure the Node.js and Docker required plugins are enabled on the Settings/Preferences | Plugins page, tab Installed, see Managing plugins for details.

  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 Jest 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": { "eslint": "^8.1.0", "http-server": "^14.0.0", "jest": "^27.3.1" } }
  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 Jest 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: 23 May 2022