1 00:00:06,570 --> 00:00:08,980 - So far, the examples we've been looking at 2 00:00:08,980 --> 00:00:10,609 have been quite simple. 3 00:00:10,609 --> 00:00:12,541 We've been using the toBe () function 4 00:00:12,541 --> 00:00:15,030 to test the result of an operation. 5 00:00:15,030 --> 00:00:19,182 So for example, if we had an add function, from before, 6 00:00:19,182 --> 00:00:22,080 we import the add function in our test. 7 00:00:22,080 --> 00:00:24,900 We call the add function with some parameters, 8 00:00:24,900 --> 00:00:29,250 and we say that we expect the result to be three. 9 00:00:29,250 --> 00:00:32,190 So what we're gonna do in this lesson is take a closer look 10 00:00:32,190 --> 00:00:36,090 at these matches toBe () as an example 11 00:00:36,090 --> 00:00:39,691 of a Jest matcher special function predefined 12 00:00:39,691 --> 00:00:43,200 in the Jest library to do an equality check. 13 00:00:43,200 --> 00:00:46,802 So as it happens, when you call toBe (), it takes the result 14 00:00:46,802 --> 00:00:50,231 of the expect, you know, whatever you put in here. 15 00:00:50,231 --> 00:00:52,770 And it checks that result using 16 00:00:52,770 --> 00:00:56,310 the equality operator triple equals to check 17 00:00:56,310 --> 00:00:59,670 if that result is identical to that value. 18 00:00:59,670 --> 00:01:02,160 Okay. So that's obviously a very simple matcher. 19 00:01:02,160 --> 00:01:04,980 ToBe () is one of the most widely used matchers, 20 00:01:04,980 --> 00:01:06,571 but it's just one of many. 21 00:01:06,571 --> 00:01:10,220 So as it turns out, there are quite a rich set 22 00:01:10,220 --> 00:01:15,000 of different types of tests you can perform in a Jest test. 23 00:01:15,000 --> 00:01:18,180 So the, you can test equality as we've just seen. 24 00:01:18,180 --> 00:01:20,880 You can test if a result is in a certain range. 25 00:01:20,880 --> 00:01:24,600 So for example, is a number between five and 10. 26 00:01:24,600 --> 00:01:27,420 You can test if a result from a function is null, 27 00:01:27,420 --> 00:01:30,300 or undefined, or not a number. 28 00:01:30,300 --> 00:01:32,250 You can check for a bullying result. 29 00:01:32,250 --> 00:01:34,110 If you call a function, you can test 30 00:01:34,110 --> 00:01:36,870 if the result is true or false. 31 00:01:36,870 --> 00:01:39,063 You can test the properties in an object. 32 00:01:39,063 --> 00:01:40,980 You can test for collections. 33 00:01:40,980 --> 00:01:43,533 You can say, does the collection have a length of five, 34 00:01:43,533 --> 00:01:48,030 or does the collection contain item X, Y, Z? 35 00:01:48,030 --> 00:01:50,340 And you can test for errors sometimes. 36 00:01:50,340 --> 00:01:51,609 You know, when you call a function, 37 00:01:51,609 --> 00:01:54,302 if you pass an invalid parameter into a function, 38 00:01:54,302 --> 00:01:56,351 it should raise an error, 39 00:01:56,351 --> 00:01:59,460 and you can test that it does raise an error when expected. 40 00:01:59,460 --> 00:02:01,764 So basically you are testing that your code is robust enough 41 00:02:01,764 --> 00:02:04,620 to raise an error when it should do, okay? 42 00:02:04,620 --> 00:02:06,210 So that's important as well. 43 00:02:06,210 --> 00:02:08,575 So these are all standard matches. 44 00:02:08,575 --> 00:02:10,800 You can also define custom matches 45 00:02:10,800 --> 00:02:13,625 as well as the built in tests, which are fairly useful 46 00:02:13,625 --> 00:02:16,463 but also quite quite technical. 47 00:02:16,463 --> 00:02:20,020 You can define a more semantic custom matcher 48 00:02:20,020 --> 00:02:22,260 for a domain specific condition. 49 00:02:22,260 --> 00:02:26,253 So for example, something, if you're working in a, 50 00:02:27,450 --> 00:02:31,882 if you're working in a school examination system, 51 00:02:31,882 --> 00:02:35,160 you could check that an exam mark is valid. 52 00:02:35,160 --> 00:02:37,470 Is it between zero and a hundred? 53 00:02:37,470 --> 00:02:39,480 If you are doing, you know, physics, 54 00:02:39,480 --> 00:02:41,130 you can check that the temperature is 55 00:02:41,130 --> 00:02:45,044 between absolute zero and 150 million degrees. 56 00:02:45,044 --> 00:02:49,067 If you're interested in performing nuclear fusion in a lab. 57 00:02:49,067 --> 00:02:52,290 So you can provide a custom matcher 58 00:02:52,290 --> 00:02:55,095 which is more semantically rich 59 00:02:55,095 --> 00:02:58,860 and potentially reusable across multiple tests. 60 00:02:58,860 --> 00:03:01,650 You can use it again and again in multiple tests. 61 00:03:01,650 --> 00:03:02,610 We'll see how to do that 62 00:03:02,610 --> 00:03:05,617 towards the end of the, of the lesson. 63 00:03:05,617 --> 00:03:06,630 Okay. 64 00:03:06,630 --> 00:03:09,273 So, what we'll do at the beginning of the, of the lesson 65 00:03:09,273 --> 00:03:12,508 in the first two sections is to take a detailed look 66 00:03:12,508 --> 00:03:17,220 at the standard matches provided in Jest 67 00:03:17,220 --> 00:03:18,360 so that you understand. 68 00:03:18,360 --> 00:03:20,343 It's really important that you understand 69 00:03:20,343 --> 00:03:22,740 what matches are available, 70 00:03:22,740 --> 00:03:24,660 and that you use them properly because you, 71 00:03:24,660 --> 00:03:27,203 this is the bed lock of your test in code. 72 00:03:27,203 --> 00:03:28,170 Okay. 73 00:03:28,170 --> 00:03:32,364 So what I've done in the demo folder, within lesson two, 74 00:03:32,364 --> 00:03:35,233 I've got two different sets of demos. 75 00:03:35,233 --> 00:03:39,255 The first folder I'm gonna look at, Standard Jest Matches, 76 00:03:39,255 --> 00:03:42,684 takes a fairly detailed look at the standard matches I've 77 00:03:42,684 --> 00:03:44,370 just been explaining. 78 00:03:44,370 --> 00:03:45,780 Towards the end of the lesson, 79 00:03:45,780 --> 00:03:47,575 we'll see how to define a custom matcher 80 00:03:47,575 --> 00:03:50,038 for something a little bit more adventurous. 81 00:03:50,038 --> 00:03:53,465 But for now, we'll concentrate on the standard Jest matches. 82 00:03:53,465 --> 00:03:57,603 If I open this folder up in the code editor, 83 00:03:58,440 --> 00:04:01,911 so it has, I've got a simple test file 84 00:04:01,911 --> 00:04:04,530 which just shows how to use the various matches. 85 00:04:04,530 --> 00:04:06,240 And we'll go through that in detail 86 00:04:06,240 --> 00:04:08,560 during the rest of this lesson. 87 00:04:08,560 --> 00:04:10,110 Okay. So I just open that file. 88 00:04:11,070 --> 00:04:13,740 As you can see, I have a test suite 89 00:04:13,740 --> 00:04:17,250 that contains various tests that use, 90 00:04:17,250 --> 00:04:18,990 that do a quality checking. 91 00:04:18,990 --> 00:04:21,293 If I just collapse this down, I've got another suite 92 00:04:21,293 --> 00:04:24,660 that matches, shows how to match for range, 93 00:04:24,660 --> 00:04:28,200 another suite that matches for null or undefined 94 00:04:28,200 --> 00:04:31,680 or not a number, a test suite that shows how to match 95 00:04:31,680 --> 00:04:34,860 for truth or falsehood, a test suite that shows how 96 00:04:34,860 --> 00:04:37,500 to match object properties, a test suite 97 00:04:37,500 --> 00:04:39,885 that matches collections, and a test suite 98 00:04:39,885 --> 00:04:41,640 that matches for errors. 99 00:04:41,640 --> 00:04:44,507 So as we go through the lesson, we're going to dig in 100 00:04:44,507 --> 00:04:48,840 to each of these suites and explore the matches 101 00:04:48,840 --> 00:04:50,250 in some detail. 102 00:04:50,250 --> 00:04:51,630 Okay. So that's what it looks like 103 00:04:51,630 --> 00:04:53,970 in my Standard Jest Matchers folder. 104 00:04:53,970 --> 00:04:57,090 In my example test file, we have one, two, three, 105 00:04:57,090 --> 00:05:02,090 a bunch of different suites, which illustrate the matches 106 00:05:02,393 --> 00:05:05,583 in sufficient detail so that you can be productive. 107 00:05:06,510 --> 00:05:09,750 Now, if you wanted to you could run all of these tests 108 00:05:09,750 --> 00:05:11,340 all at once. 109 00:05:11,340 --> 00:05:12,330 That would be great. 110 00:05:12,330 --> 00:05:15,240 You're also gonna get overwhelmed with test results. 111 00:05:15,240 --> 00:05:17,583 So probably what we, what a best, a better approach would be 112 00:05:17,583 --> 00:05:21,111 would be to just run tests in an individual suite. 113 00:05:21,111 --> 00:05:24,030 And that's the approach that we are gonna take. 114 00:05:24,030 --> 00:05:26,340 Okay. So, oh, and also we'll see how 115 00:05:26,340 --> 00:05:31,320 to define custom matches later on in the in this lesson. 116 00:05:31,320 --> 00:05:34,092 So if we want to run tests for a particular suite, 117 00:05:34,092 --> 00:05:35,850 we've seen this before. 118 00:05:35,850 --> 00:05:39,459 You would do something like this. 119 00:05:39,459 --> 00:05:42,695 Either yarn test, and then you give it minus T 120 00:05:42,695 --> 00:05:45,510 to narrow down the tests you want to run, 121 00:05:45,510 --> 00:05:48,164 and then you give it like a partial string 122 00:05:48,164 --> 00:05:50,760 to match the name of your suite. 123 00:05:50,760 --> 00:05:53,400 Okay. So this would run all tests in a suite 124 00:05:53,400 --> 00:05:56,370 that contains equality in the suite name. 125 00:05:56,370 --> 00:05:59,924 Well, as it happens, that's going to run all the tests 126 00:05:59,924 --> 00:06:02,644 in this suite. 127 00:06:02,644 --> 00:06:05,610 We'll have a look at those in detail in the next section. 128 00:06:05,610 --> 00:06:07,860 Okay. There are several of them. 129 00:06:07,860 --> 00:06:12,030 So, when I run the test, it'll run all the tests 130 00:06:12,030 --> 00:06:15,330 in this suite here, just that suite. 131 00:06:15,330 --> 00:06:18,657 If you prefer, you can run NPM instead of yarn. 132 00:06:18,657 --> 00:06:23,657 NPM run test and then give it the, the name 133 00:06:23,880 --> 00:06:27,840 of the test suite to, to execute. 134 00:06:27,840 --> 00:06:28,673 So, I'm gonna do that. 135 00:06:28,673 --> 00:06:31,830 Let's open a command window and run this command 136 00:06:31,830 --> 00:06:33,900 to run the equality suite. 137 00:06:33,900 --> 00:06:37,875 So, I'm gonna open a folder, gonna open a command window 138 00:06:37,875 --> 00:06:39,993 in that folder. 139 00:06:43,191 --> 00:06:46,877 And in this folder, let's do an NPM run test 140 00:06:49,740 --> 00:06:52,423 and then pass in parameters into the test command, 141 00:06:52,423 --> 00:06:55,590 the minus T option, and then the, the name 142 00:06:55,590 --> 00:06:58,270 or the partial name for the suite that we want to run. 143 00:06:58,270 --> 00:07:00,900 And make sure I spell it correctly. 144 00:07:00,900 --> 00:07:02,430 So that will run the equality suite, 145 00:07:02,430 --> 00:07:04,890 and it'll run all the tests in that suite. 146 00:07:04,890 --> 00:07:08,823 So, we'll just give that a moment to, to get going. 147 00:07:10,080 --> 00:07:12,960 Okay. So it's past the minus T equality parameters 148 00:07:12,960 --> 00:07:13,863 into Jest. 149 00:07:14,850 --> 00:07:17,966 It found my one and only test file. 150 00:07:17,966 --> 00:07:21,900 In that test file, there are a bunch of suites. 151 00:07:21,900 --> 00:07:24,060 It ran the tests in this suite. 152 00:07:24,060 --> 00:07:25,968 You can see the whole past. Great. 153 00:07:25,968 --> 00:07:30,300 It detected the other suites as well, but it skipped all 154 00:07:30,300 --> 00:07:34,263 of those tests because I didn't want to run these suites. 155 00:07:35,250 --> 00:07:37,620 I wasn't interested in running every single test. 156 00:07:37,620 --> 00:07:39,745 Life is too short for that. 157 00:07:39,745 --> 00:07:42,720 So, it skipped all of these suites. 158 00:07:42,720 --> 00:07:45,420 The only suite that it actually executed was the suite 159 00:07:45,420 --> 00:07:48,423 that contained the match for my parameter. 160 00:07:49,620 --> 00:07:52,440 Okay. So what we're gonna do in the rest of the lesson 161 00:07:52,440 --> 00:07:54,540 or for most of the rest of the lesson, is to dig 162 00:07:54,540 --> 00:07:58,350 into these different matches and to see how they work. 163 00:07:58,350 --> 00:07:59,955 You know, there are some subtle differences here, 164 00:07:59,955 --> 00:08:03,780 and it is important that you understand how these all work. 165 00:08:03,780 --> 00:08:06,450 So, that's the, that's the mission for the rest 166 00:08:06,450 --> 00:08:07,283 of this lesson.