GoLand 2018.2 Help

Jest

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

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

Installing Jest

Running tests

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

To run a single test from the editor

  • Click icons toolwindows toolWindowRun svg or icons actions rerun in the left gutter and choose Run <test_name> from the pop-up 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 Jest run configuration

  1. Select a test file in the Project tool window and choose Create <file name> on the context menu. The Run/Debug Configuration: Jest dialog box 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 and 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 specify the jest.config file to use: choose the relevant file from the drop-down list, or click browseButton and choose it in the dialog that opens, or just type the path in the text box. If the field is empty, GoLand 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.

  5. 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 icons toolwindows toolWindowRun svg 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. GoLand 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 GoLand, you can jump between a file and the related test file. Navigation from a test result in the Test Runner Tab to the test is also supported.

To jump between a file and the related test file

  • Open the file in the editor and choose Go To | Test or Go To | Test Subject on the context menu, or just press Ctrl+Shift+T.

To jump from a test result to the test

  • Select the test name in the Test Runner tab and choose Jump to Source on the context menu.

    ws_test_jump_to_test
    The test file opens in the editor with the cursor placed at the test definition.

Snapshot testing

GoLand integration with Jest supports such a great feature as snapshot testing. 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. You can see what caused this mismatch in the GoLand built-in Difference Viewer.

To update the snapshot for a failed test

  • Use the Click to update snapshot link in the Test Runner tab of the Run tool window:

    ws_jest_update_snapshot.png

To navigate from a test file to the related snapshot file

  • Click artwork studio icons logcat toolbar snapshot in the left gutter next to the test.

  • With the cursor at toMatchSnapshot(), press Ctrl+Alt+Home and select the required snapshot from the list.

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

To compare the actual output with the expected snapshot

  • Use 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 GoLand Difference Viewer

Debugging tests

With GoLand, 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 icons toolwindows toolWindowRun svg or icons actions rerun in the left gutter and choose Debug <test_name> from the pop-up list.

To launch test debugging via a run/debug configuration

  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 icons actions startDebugger svg to the right of the list.

  3. In the Debug Tool Window that opens, proceed as usual: step through the tests, stop and resume test execution, examine the test when suspended, etc.

Monitoring code coverage

With GoLand, you can also monitor how much of your code is covered with Jest tests. GoLand displays this statistics in a dedicated tool window and marks covered and uncovered lines visually right in the editor.

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 icons general runWithCoverage svg  to the right of the list.
    Alternatively, quickly run a specific suite or a test with coverage from the editor: click icons toolwindows toolWindowRun svg or icons actions rerun in the left gutter and choose Run <test_name> with Coverage from the pop-up 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_jest_coverage_report.png

Last modified: 12 October 2018

See Also