A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.jetbrains.com/webstorm/help/running-unit-tests-on-jest.html below:

Jest | WebStorm Documentation

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 WebStorm. 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 Install and configure Jest

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

Run tests

With WebStorm, 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 more information about creating Vitest tests for JavaScript and TypeScript code, refer to Vitest features on the Vitest official website.

Run a single test from the editor Run all tests in a folder from the Project tool window Create a Jest run configuration
  1. Open the Run/Debug Configuration dialog ( in the main menu), click in the left-hand pane, and select Jest from the list. The Run/Debug Configuration: Jest dialog opens.

    Alternatively, select a test file in the Project tool window Alt+1 and select Create <filename> from the context menu.

  2. Specify the Node.js interpreter to use.

    If you choose the Project alias, WebStorm will automatically use the project default interpreter from the Node interpreter field on the Node.js page . In most cases, WebStorm detects the project default interpreter and fills in the field itself.

    You can also choose another configured local or remote interpreter or click and configure a new one.

    For more information, refer to Configuring remote Node.js interpreters, Configuring a local Node.js interpreter, and Using 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.

  5. Specify the tests to run. This can be a specific test or suite, an entire test file, or a folder with test files.

  6. Optionally specify the jest.config.js or jest.config.ts file to use: select the relevant file from the list, or click and select it in the dialog that opens, or just type the path in the field.

    If the field is empty, WebStorm 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.

  7. Optionally:

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

    You can also turn the watch mode on later by pressing the Watch for Changes toggle button in the Run tool window during a test session, refer to Rerun test automatically on changes (watch mode) below.

  8. Optionally:

    Specify the environment variables for executing commands. WebStorm will show these variables in completion lists for process.env.

  9. In the Node options field, optionally type the Node.js-specific command-line options to be passed to the Node.js executable file. The acceptable options are:

Run tests via a run configuration
  1. Select the Jest run/debug configuration from the list of configurations and click   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.

Rerun failed tests

For more information, refer to Rerunning tests.

Rerun tests automatically on changes (watch mode)

WebStorm supports the watch mode in which a test is rerun automatically as soon as any changes are made to the test-related source file. As a result, you can just make changes to your code without having to rerun tests manually or restart the Jest run/debug configuration.

Navigation

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

With WebStorm, you can jump between a file and the related test file and between a test or suite definition and its result in the Test Runner Tab.

Such navigation works only if the test file follows popular naming conventions, for example, has a .test., .spec., or _spec. suffix and is located either next to the source file or in a test folder.

Snapshot testing

WebStorm 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 in the gutter next to the test or select the required snapshot from the context menu of the .toMatchSnapshot() method.

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 WebStorm built-in Diff Viewer via the Click to see difference link in the right-hand pane of the Test Runner tab.

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

Debug tests

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

In the Debug tool window that opens, proceed as usual: step through the program, stop and resume program execution, examine it when suspended, explore the call stack and variables, set watches, evaluate variables, view actual HTML DOM, and so on.

Monitor code coverage

With WebStorm, you can also monitor how much of your code is covered with Jest tests. WebStorm 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 Alt+1.

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  to the right of the list.

    Alternatively, quickly run a specific suite or a test with coverage from the editor: click or in the gutter and select 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:

Run Jest tests with Node.js inside a Docker container

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

Before you start
  1. Make sure the Node.js, Node.js Remote Interpreter, and Docker required plugins are enabled on the Settings | Plugins page, tab Installed. For more information, refer to Managing plugins.

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

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

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

  5. 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: 22 November 2024


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4