Testing with Jest in WebStorm
Jest, the testing platform developed by Facebook, is becoming more and more popular with each day, especially for testing React applications. Jest is fast, easy to get started with, and has lots of features (such as snapshot testing and test coverage) available out of the box.
In WebStorm we wanted to streamline the whole testing workflow and make writing, running, and debugging tests with Jest even smoother and easier. Let’s see how WebStorm can help you test your app with Jest.
- Configure code completion
- Run tests
- Run a single test, test suite or file
- Rerun failed tests
- Run tests in watch mode
- Snapshot testing
- Debug tests
- Test coverage
Configure code completion
devDependencies in project’s package.json.
This happens because Jest defines these methods in the global scope – so you don’t need to import them in each test file. But it also makes it harder for WebStorm to learn about them from the static analysis of the Jest sources – that’s why they are marked by default as unresolved.
Create a run configuration to run all tests in the project
First, let’s run all the tests we have in our project. We need to create a run/debug configuration:
- in the menu Run, select Edit configurations
- then click
+and select Jest from the drop-down list
Usually, you don’t have to change anything in the configuration, but if there’s a Jest configuration file in the project or you need to pass additional flags to Jest, you should do so in this configuration.
For now, we will just select All tests at the bottom of the dialog and click OK to save the configuration. You will immediately see the new configuration at the top right corner of the IDE – now click the green icon next to it to run it.
View test results
In the test tool window that opens, you can track the test progress and see all the test results (you can also see the test result in the editor, right next to the test). All the tests will be listed in a tree view on the left side of the tool window. On the right, you will see a stack trace for the tests that failed.
Double-click the test name in the list to open it in the editor. In WebStorm 2018.3, for failed tests, it will open the line that was at the top of the stack trace.
Use the icons at the top of the tool window to hide all the passed and ignored tests from the results.
If you want to find a particular test in the test results, just start typing its name and then use the up and down arrows to jump between the matched test names.
Run a single test, test suite, or file
If you have lots of tests and you only want to run some of them, you have a bunch of options available.
In the editor, next to each test and test suite you can see an icon – it shows the test status for the tests that you have recently run. If you click it, you can select whether you want to run or debug this particular test or suite.
This way, we can very quickly run or debug a test – there is no need to create a new run/debug configuration yourself. WebStorm will use the template for the new Jest run configuration that can be modified in the menu Run | Edit configurations – Default configurations – Jest.
If you want to run the whole test file with Jest, right-click it and select Run.
Rerun only failed tests
If you have implemented a fix for the failed tests and now want to rerun them to check the fix, click the Rerun failed tests button in the tool window.
Run tests in watch mode
One of the greatest features that Jest has is a watch mode for running tests. In this mode, once you’ve made changes to the test or related files, the tests will be restarted automatically. By default, WebStorm doesn’t enable the watch mode when running all tests, but it’s very easy to enable it.
Open the Jest run/debug configuration that we created earlier and add
--watchAll to the Jest options field, save the configuration, and then run it again.
Now, if you fix our failing test or add a new test and save the changes, Jest will rerun the tests and you will see the new results in the test tool window.
This is particularly handy if you’re doing test-driven development and you first develop the tests and then add the feature they cover – you will see how your tests turn from red to green in the test view without having to restart them every time.
By the way, you can quickly jump from the file to its test file and back using the shortcut Cmd-Shift-T / Ctrl-Shift-T. This navigation works if the files follow one of the popular naming conventions (e.g. are named app.js and app.spec.js or app.test.js).
Note that the individual tests started from the editor will not be run in the watch mode until you add
--watch to the template for the Jest configuration.
Another feature provided by Jest is snapshot testing. Snapshot files describe the DOM elements in a special format, so your tests can check the UI against these snapshots and the IDE will warn you if the UI component changes unexpectedly.
The first time you run the test that has a
.toMatchSnapshot() method, Jest creates a snapshot file in the snapshots folder. You can jump from the test to the related snapshot by clicking the camera icon next to it:
Then, if the tested component has changed and it no longer matches the snapshot, the test will fail.
To make it easier to spot the difference between the actual and expected results, we’ve added a diff view for snapshots. Click the link in the test output next to the stack trace to see the diff.
If the change was actually intentional, you can update the snapshot by clicking another link in the tool window:
To investigate why a test is failing, you can debug it in WebStorm. Click the gutter icon next to the test in the editor, and then select Debug
to debug just one test. Alternatively, click the green debug icon next to the All tests configuration that we created earlier.
The breakpoint can be set either in the test file or in the source code that is tested. Once the breakpoint is hit, you can step through the code, see the call stack and variables, use the console, and so on.
"sourceMap": true or
To make sure that all your source code is well tested, you can build a code coverage report. All you need to do is to click the ‘Run with coverage’ button next to the run/debug configuration. The report will show how many files were covered with tests and what percentage of lines in those files are covered.
From this report, you can jump to the file and see which lines were covered (marked green) and which ones were not (marked red).
That’s it! We hope you find this guide useful and have a great experience testing with Jest in WebStorm. If you run into any problems or would like to share your suggestions, feel free to submit an issue in our tracker.
Your WebStorm Team