This course is no longer being maintained. We recommend the testing section of the Intermediate React course instead. We now recommend you take the Intermediate React, v5 course.

Check out a free preview of the full Testing React Applications, v2 course:
The "Jest Watch Mode" Lesson is part of the full, Testing React Applications, v2 course featured in this preview video. Here's what you'd learn in this lesson:

When configuring Jest watch mode, which allows runs tests related to only the files that have changed, Kent shows that watch mode also allows the running of all the tests matching a specific pattern by the file name or the test name. Kent takes questions from students.

Get Unlimited Access Now

Transcript from the "Jest Watch Mode" Lesson

>> Kent C. Dodds: Okay, the last thing is jest watch mode. It's amazing, it's one of my favorite features of jest. So I add test:watch, it's pretty common to do something like that, just add jest --watch.
>> Kent C. Dodds: And then we can run npm run test:watch.
>> Kent C. Dodds: And this will run all of our tests.

[00:00:31] But if you read the output, it's actually kind of interesting. It says, Ran all test suites related to changed files. This is one of my favorite features of js watch mode. So saying related change files, I'm gonna go ahead and add this stuff and commit.
>> Kent C. Dodds: Stuff no verify.

[00:00:54] Okay, so now my git is clean and if I run my test again,
>> Kent C. Dodds: Now it's not going to run any files. So there's no test found related to files changed since last commit. Press A to run all tests or run JEST with watch all. So this is an amazing feature.

[00:01:14] It is able to determine which tests are relevant for the files that have been changed. And so if I go ahead and make a change to, probably a good example versus the utils and I can say like any chain, that's the wrong key. Any change at all there and it will say hey git, I noticed that you changed a file, let me find the tests that are related to that.

[00:01:45] Or that are using that file and run those tests. It's pretty neat. So let's change the calculator, or sorry let's change the auto scaling text. They and now, its running our auto scanning test. So pretty nifty capability there. Thank you, jest. Okay. Also with this watch usage, you can press A to run all the tests.

[00:02:19] Here actually, once it's finished doing all of its test run it'll say press w to show your watch usage. You can press f to run only the failed test. So if you have specific tests that are failing, you are trying to fix those. It'll take a snapshot of which tests failed.

[00:02:37] And then keep running those until you get them all fixed or whatever. Or you change the mode that you're in. And then you can run them all and make sure they're all still working. Yes, question?
>> Speaker 2: I still have a question. Would you ever wanna combine the coverage with the watch mode, or is there a reason you-

>> Kent C. Dodds: Yeah, I'm glad you asked that. Cuz I thought about that a second ago, and I thought I was gonna forget. So yeah, so the question is can I update coverage during watch mode? And not today, I think, but a couple of weeks ago I saw a pull request for that.

[00:03:12] So it actually might be released, but I haven't looked into the feature yet. But yeah, the idea is iterative coverage or what is the word I'm looking for? It'll just update the coverage of the tests that were run. Yeah, it's crazy. It's very, very cool. So that is coming soon.

[00:03:33] Good question. Also you can press p to filter by file name Regex pattern. So I'll just copy this, call this metric pattern hit p. Auto scanning text now it will only run that one. You can press t to filter by a specific test name so if I had multiple tests in here this would be more impressive but I only got one anyway.

[00:03:56] But I press T, say M-A-T and that's the [LAUGH] T-M-A-or M-L mounts so it only runs the tests that match that. So you can still do test.only to only run certain tests but with this feature you don't have to change any of your code to run a very specific test.

[00:04:23] And it also filters down to the test that you're currently watching. But if we do all and then I hit tee, then it will run all of the tests that match, in all of the files that match that title, reg-x. So very, very powerful capabilities here, and I love it.

[00:04:43] Another thing that you might not notice, but it does for you, is. If you have a big test suites, running a hundred tests whenever you make a change, and one of those tests fail, the next time it runs, it will run that test first. So that you get that feedback immediately on whether your change has fixed that test.

[00:05:03] Jest is like your favorite friend. Jest is like my favorite friend. I just love Jest.