1 00:00:06,570 --> 00:00:09,960 - Jest has several matches, which allow us to check 2 00:00:09,960 --> 00:00:12,690 if a number is in a certain range. 3 00:00:12,690 --> 00:00:14,160 They're quite straightforward. 4 00:00:14,160 --> 00:00:17,460 You can check if a number is less than another 5 00:00:17,460 --> 00:00:20,096 or less than or equal or greater than, 6 00:00:20,096 --> 00:00:22,920 greater than or equal, or close to. 7 00:00:22,920 --> 00:00:25,590 Close to is useful when you have a fraction 8 00:00:25,590 --> 00:00:28,710 and you wanna specify the level 9 00:00:28,710 --> 00:00:31,320 of accuracy in your comparison. 10 00:00:31,320 --> 00:00:34,380 So I have examples that show these matches. 11 00:00:34,380 --> 00:00:36,090 They're quite straightforward. 12 00:00:36,090 --> 00:00:40,710 All of the examples are in this suite, Matches for range. 13 00:00:40,710 --> 00:00:44,730 This suite is in this file in this folder. 14 00:00:44,730 --> 00:00:47,220 So let me just quickly show you where you can get this code 15 00:00:47,220 --> 00:00:49,110 if you want to try it for yourself. 16 00:00:49,110 --> 00:00:50,910 Okay. So we're in lesson two. 17 00:00:50,910 --> 00:00:54,510 We are looking to look at the standard Jest matches folder. 18 00:00:54,510 --> 00:00:57,960 And if you open that folder in the code editor, 19 00:00:57,960 --> 00:01:01,770 you find that we have example test in there, 20 00:01:01,770 --> 00:01:04,380 contains all the code for this lesson. 21 00:01:04,380 --> 00:01:06,150 Most of the stuff for this lesson. 22 00:01:06,150 --> 00:01:09,180 And I have a suite here, Matches for range, 23 00:01:09,180 --> 00:01:13,011 and I have five tests in that suite as it happens. 24 00:01:13,011 --> 00:01:17,610 One for each of the methods that I just mentioned. 25 00:01:17,610 --> 00:01:18,443 Okay. 26 00:01:18,443 --> 00:01:23,010 So a test for less than, a test for less than or equal, 27 00:01:23,010 --> 00:01:26,883 a test for greater than, greater than or equal, or close to. 28 00:01:27,720 --> 00:01:29,970 So I'm gonna go through the code for those in a moment. 29 00:01:29,970 --> 00:01:32,913 It is quite straightforward but worth doing I think. 30 00:01:33,810 --> 00:01:35,110 I think probably the first thing we should do 31 00:01:35,110 --> 00:01:38,250 is to actually make sure that those tests actually work. 32 00:01:38,250 --> 00:01:39,840 So to run those tests 33 00:01:39,840 --> 00:01:44,040 you can say either yarn test or NPM run test. 34 00:01:44,040 --> 00:01:46,590 So I'm gonna do that now, open a command window, 35 00:01:46,590 --> 00:01:48,450 and I'm gonna run these tests just to make sure 36 00:01:48,450 --> 00:01:49,560 that they do actually work 37 00:01:49,560 --> 00:01:52,020 before we have a look at the code 38 00:01:52,020 --> 00:01:54,570 NPM run test 39 00:01:54,570 --> 00:01:59,190 and parameters into the test command minus T range. 40 00:01:59,190 --> 00:02:01,843 Okay. So basically it'll run any test suite 41 00:02:01,843 --> 00:02:06,180 that has the range name in the selector. 42 00:02:06,180 --> 00:02:10,230 So our suite, it'll run the five tests 43 00:02:10,230 --> 00:02:13,833 that I've just mentioned and they should all work fine. 44 00:02:16,530 --> 00:02:18,030 Okay. Here comes. 45 00:02:18,030 --> 00:02:19,230 Yeah, there we are. 46 00:02:19,230 --> 00:02:22,500 So it ran, it skipped most of the tests, 47 00:02:22,500 --> 00:02:25,500 and it just ran the tests in my range suite. 48 00:02:25,500 --> 00:02:29,250 And as you can see, all of those tests have succeeded 49 00:02:29,250 --> 00:02:30,690 so we're good to go. 50 00:02:30,690 --> 00:02:32,310 Let's have a look at the code. 51 00:02:32,310 --> 00:02:34,830 So first of all, toBeLessThan, 52 00:02:34,830 --> 00:02:37,080 obviously it uses the less than operator 53 00:02:37,080 --> 00:02:39,000 and it works with regular numbers and 54 00:02:39,000 --> 00:02:39,953 with BigInt. 55 00:02:39,953 --> 00:02:41,310 BigInt, I'm not sure if you've seen this 56 00:02:41,310 --> 00:02:42,360 in JavaScript. 57 00:02:42,360 --> 00:02:43,863 Obviously it allows you to store very big numbers, 58 00:02:43,863 --> 00:02:45,150 very big integers. 59 00:02:45,150 --> 00:02:47,147 So here's my first test. 60 00:02:47,147 --> 00:02:49,170 I have "a" and "b". 61 00:02:49,170 --> 00:02:50,865 I'm expecting "a" to be less than "b", yes. 62 00:02:50,865 --> 00:02:53,680 1.5 is less than 3.5 63 00:02:54,637 --> 00:02:58,147 "C" and "d" are BigInts. 64 00:02:58,147 --> 00:03:03,147 "C" is that big integer and "c" is less than "d". 65 00:03:03,750 --> 00:03:06,570 Okay. So it works fine with big integers, 66 00:03:06,570 --> 00:03:07,403 which is quite nice. 67 00:03:07,403 --> 00:03:10,260 Not that we use BigInt much, but it's reassuring to know 68 00:03:10,260 --> 00:03:12,423 that it does actually work with BigInt. 69 00:03:13,440 --> 00:03:17,031 Okay, so that's toBeLessThan, toBeLessThanOrEqual, 70 00:03:17,031 --> 00:03:18,750 exactly the same idea. 71 00:03:18,750 --> 00:03:20,707 So have a look at this code. 72 00:03:20,707 --> 00:03:22,447 "A" is 1.5. 73 00:03:22,447 --> 00:03:25,860 "A" I'm expecting to be less than or equal to 1.6. 74 00:03:25,860 --> 00:03:27,903 Yes, "a" is less than 1.6. 75 00:03:29,280 --> 00:03:32,490 I'm expecting "a", to be less than or equal to 1.5. 76 00:03:32,490 --> 00:03:35,043 Okay, it's actually equal, but that's fine. 77 00:03:35,970 --> 00:03:38,340 So "a" is a regular number here. 78 00:03:38,340 --> 00:03:41,070 In this case, "b" is a BigInt. 79 00:03:41,070 --> 00:03:44,190 I'm expecting this BigInt to be less than or equal to that. 80 00:03:44,190 --> 00:03:45,990 And it is, it's less than. 81 00:03:45,990 --> 00:03:48,480 I'm expecting this to be less than or equal to that. 82 00:03:48,480 --> 00:03:51,393 And it is, it's equal, but that's fine. 83 00:03:52,890 --> 00:03:53,723 Right. 84 00:03:53,723 --> 00:03:56,700 Well, similar idea with the greater than tests, 85 00:03:56,700 --> 00:03:59,831 you can check if a number is greater than another 86 00:03:59,831 --> 00:04:01,050 or equal. 87 00:04:01,050 --> 00:04:04,971 So this first of all, is "a" greater than "b"? 88 00:04:04,971 --> 00:04:08,250 Is "a" greater than "b", yes it is. 89 00:04:08,250 --> 00:04:11,880 Is "c", is that BigInt greater than that BigInt? 90 00:04:11,880 --> 00:04:15,150 Yes "c" is greater than "d". 91 00:04:15,150 --> 00:04:17,820 Okay, so that's what you would've imagined. 92 00:04:17,820 --> 00:04:21,873 toBeGreaterThanOrEqual, similar idea. 93 00:04:22,770 --> 00:04:26,610 So I'm expecting "a" to be greater than or equal to that, 94 00:04:26,610 --> 00:04:29,766 it's greater than, greater than or equal to that, 95 00:04:29,766 --> 00:04:32,310 it's equal to, but that's fine. 96 00:04:32,310 --> 00:04:35,700 And then the same kind of test using BigInt. 97 00:04:35,700 --> 00:04:37,770 So here's a big integer. 98 00:04:37,770 --> 00:04:41,370 It should be greater than or equal to that and it is. 99 00:04:41,370 --> 00:04:45,420 It should be greater than or equal to that and it is. 100 00:04:45,420 --> 00:04:48,000 All right so, very straightforward. 101 00:04:48,000 --> 00:04:51,510 I guess the only thing that is worth particularly mentioning 102 00:04:51,510 --> 00:04:52,860 is the fact that it does actually work 103 00:04:52,860 --> 00:04:55,593 with these big integers as well as regular numbers. 104 00:04:56,550 --> 00:04:59,520 Now, if you have a fraction, you need to be a bit 105 00:04:59,520 --> 00:05:01,429 careful when you are comparing floats, because 106 00:05:01,429 --> 00:05:06,003 there's an inherent inaccuracy with a floating point number. 107 00:05:06,990 --> 00:05:10,320 So if you are checking or comparing or testing 108 00:05:10,320 --> 00:05:13,710 floating point numbers, you should use toBeCloseTo. 109 00:05:13,710 --> 00:05:15,801 If you want, rather than checking for equality. 110 00:05:15,801 --> 00:05:17,113 If you want to check for equality, 111 00:05:17,113 --> 00:05:20,171 you should check for toBeCloseTo. 112 00:05:20,171 --> 00:05:22,710 The way it works, it takes two numbers. 113 00:05:22,710 --> 00:05:27,030 Obviously you say the number that you're looking for, 114 00:05:27,030 --> 00:05:30,210 and then, when you say to be close to you say, 115 00:05:30,210 --> 00:05:32,070 this is the number you want to compare against. 116 00:05:32,070 --> 00:05:33,600 That's the first parameter. 117 00:05:33,600 --> 00:05:37,350 And the second parameter is the level of accuracy. 118 00:05:37,350 --> 00:05:39,403 It's the number of digits after the decimal point 119 00:05:39,403 --> 00:05:41,820 that you want to take into account. 120 00:05:41,820 --> 00:05:44,778 And the default number of places is two digits 121 00:05:44,778 --> 00:05:47,070 after the decimal point. 122 00:05:47,070 --> 00:05:51,483 Have a look at this example, "a" and "b" are quite similar. 123 00:05:52,440 --> 00:05:54,881 If I check if "a" is close to "b", 124 00:05:54,881 --> 00:05:59,806 the toBeCloseTo function ideally takes two parameters, 125 00:05:59,806 --> 00:06:04,680 but the second parameter defaults to two decimal places. 126 00:06:04,680 --> 00:06:07,170 So here I'm comparing "a" and "b" 127 00:06:07,170 --> 00:06:10,590 to two decimal places after the decimal point. 128 00:06:10,590 --> 00:06:15,590 So after the decimal point, they both say 10.12, 10.12. 129 00:06:16,290 --> 00:06:20,157 Okay, so to two decimal places "a" is close to "b". 130 00:06:21,240 --> 00:06:25,350 If you want to pass in an actual number of decimal places. 131 00:06:25,350 --> 00:06:26,970 So four digits will now be taken 132 00:06:26,970 --> 00:06:29,520 into account after the decimal point. 133 00:06:29,520 --> 00:06:31,800 So is "a" close to "b"? 134 00:06:31,800 --> 00:06:35,400 If we look at four decimal places, one, two, three, 135 00:06:35,400 --> 00:06:38,263 yes the first four digits after the decimal place there 136 00:06:38,263 --> 00:06:39,930 are the same there. 137 00:06:39,930 --> 00:06:42,630 It basically ignores the last digit in that case. 138 00:06:42,630 --> 00:06:44,220 It only looks at the four first ones. 139 00:06:44,220 --> 00:06:45,900 It doesn't round up or round down. 140 00:06:45,900 --> 00:06:49,170 It just looks at those first four digits. 141 00:06:49,170 --> 00:06:52,170 And the first four digits are the same. 142 00:06:52,170 --> 00:06:54,150 So "a" is close to "b", 143 00:06:54,150 --> 00:06:55,872 if we just look at four digits. 144 00:06:55,872 --> 00:06:58,770 However, if you look at five digits, 145 00:06:58,770 --> 00:07:01,597 then you can see that "a" and "b" are not. 146 00:07:01,597 --> 00:07:04,770 "A" is not close enough to "b" if you look at five digits. 147 00:07:04,770 --> 00:07:07,591 So I would expect "a" not to be close to "b" 148 00:07:07,591 --> 00:07:09,960 if you look at five digits, okay, 149 00:07:09,960 --> 00:07:11,370 they're different to each other, 150 00:07:11,370 --> 00:07:14,070 if you take five digits into account. 151 00:07:14,070 --> 00:07:14,903 So there we go. 152 00:07:14,903 --> 00:07:16,530 Quite straightforward. 153 00:07:16,530 --> 00:07:18,840 You just need to be a little bit careful I guess 154 00:07:18,840 --> 00:07:21,100 when you're dealing with floating point numbers that you, 155 00:07:21,100 --> 00:07:24,120 never check for absolute quality with floats. 156 00:07:24,120 --> 00:07:27,213 You always use a proximity check like we've just seen.