1 00:00:00,249 --> 00:00:02,429 (silence) 2 00:00:02,429 --> 00:00:04,693 (silence) 3 00:00:04,693 --> 00:00:07,241 (silence) 4 00:00:07,241 --> 00:00:08,910 - In this section we got to see how we can write 5 00:00:08,910 --> 00:00:13,910 jest tests to see if a variable is null, defined, 6 00:00:14,160 --> 00:00:17,190 or undefined, or not a number. 7 00:00:17,190 --> 00:00:20,220 Obviously in professional code, you want to make sure 8 00:00:20,220 --> 00:00:23,520 that your variables are null when they should be 9 00:00:23,520 --> 00:00:26,340 and not null when they shouldn't be. 10 00:00:26,340 --> 00:00:27,900 So undefined and so on. 11 00:00:27,900 --> 00:00:31,620 So the examples for this section are located here 12 00:00:31,620 --> 00:00:34,920 in lesson two in the Standard Jest Matchers folder. 13 00:00:34,920 --> 00:00:39,570 Open up example test .JS and if you do that 14 00:00:39,570 --> 00:00:42,090 you'll find there's a test suite that checks 15 00:00:42,090 --> 00:00:45,030 for null, undefined, and none. 16 00:00:45,030 --> 00:00:46,154 Okay. 17 00:00:46,154 --> 00:00:49,200 So let's just show you where that is on the file system. 18 00:00:49,200 --> 00:00:52,110 So in lesson two, you wanna have look in the 19 00:00:52,110 --> 00:00:55,200 Standard Jest Matchers folder and if you open 20 00:00:55,200 --> 00:00:57,630 that folder in a code editor it looks like this. 21 00:00:57,630 --> 00:01:00,930 We have an example test and in there 22 00:01:00,930 --> 00:01:03,000 there's a test suite here it is. 23 00:01:03,000 --> 00:01:07,140 Matchers for null, undefined, or not a number. 24 00:01:07,140 --> 00:01:12,060 In this test suite I have four tests for null, 25 00:01:12,060 --> 00:01:15,333 defined, undefined, and none. 26 00:01:16,260 --> 00:01:17,093 Okay. 27 00:01:17,093 --> 00:01:17,926 So that's good. 28 00:01:17,926 --> 00:01:20,100 We'll have a look that code in a moment. 29 00:01:20,100 --> 00:01:23,220 To run those tests, usual kind of arrangement 30 00:01:23,220 --> 00:01:26,313 either using yarn or node package manager. 31 00:01:27,547 --> 00:01:32,310 Run the test and look for the null test suite specifier. 32 00:01:32,310 --> 00:01:35,170 So look for the test suite that contains the null keyword 33 00:01:36,508 --> 00:01:39,090 or the null descriptor and it'll run that test suite. 34 00:01:39,090 --> 00:01:40,311 Okay. 35 00:01:40,311 --> 00:01:42,690 So I'll do that initially before we actually look 36 00:01:43,780 --> 00:01:47,340 at the code NPM run test and then options. 37 00:01:47,340 --> 00:01:49,230 I'm gonna pass the minus T option. 38 00:01:49,230 --> 00:01:50,610 I'm looking for the test suite that 39 00:01:50,610 --> 00:01:53,103 contains the null word. 40 00:01:54,450 --> 00:01:56,760 So it should find my test suite and it should run 41 00:01:56,760 --> 00:02:00,693 the four tests in that test suite and they should all pass. 42 00:02:01,980 --> 00:02:03,093 That's the idea. 43 00:02:04,320 --> 00:02:09,150 Okay, so it ran the tests in this test suite 44 00:02:09,150 --> 00:02:12,870 because it matched the null word as I asked it to up here 45 00:02:12,870 --> 00:02:16,050 and those four tests have actually worked fine. 46 00:02:16,050 --> 00:02:18,240 So let's have a look at the code. 47 00:02:18,240 --> 00:02:20,550 So first of all then to be null, checks 48 00:02:20,550 --> 00:02:22,380 if a variable is null quite obvious 49 00:02:22,380 --> 00:02:23,213 really. 50 00:02:23,213 --> 00:02:24,150 Here's an example. 51 00:02:24,150 --> 00:02:28,680 A is null, I'm expecting a to be null. 52 00:02:28,680 --> 00:02:29,513 It is. 53 00:02:29,513 --> 00:02:32,460 I'm expecting B not to be null. 54 00:02:32,460 --> 00:02:33,454 Okay. 55 00:02:33,454 --> 00:02:34,290 So you can check for something to be null 56 00:02:34,290 --> 00:02:36,450 or not to be null like so. 57 00:02:36,450 --> 00:02:39,360 And similar idea if you want to check 58 00:02:39,360 --> 00:02:42,420 if a property is defined, does an object 59 00:02:42,420 --> 00:02:44,070 have a property. 60 00:02:44,070 --> 00:02:45,870 Have a look at this example. 61 00:02:45,870 --> 00:02:48,750 This is, oh, it's me. 62 00:02:48,750 --> 00:02:50,880 I have an object here. 63 00:02:50,880 --> 00:02:52,590 The person's name is Andy. 64 00:02:52,590 --> 00:02:54,870 Oh, the age actually I'm 57 now. 65 00:02:54,870 --> 00:02:57,780 So I should increment my age here that's depressing 66 00:02:57,780 --> 00:02:58,890 isn't it? 67 00:02:58,890 --> 00:03:00,630 - Anyway, my person object has a name 68 00:03:00,630 --> 00:03:02,850 property and an age property. 69 00:03:02,850 --> 00:03:07,680 I'm expecting that the person name property is defined. 70 00:03:07,680 --> 00:03:10,500 Okay, in other words, there is a name property 71 00:03:10,500 --> 00:03:12,840 on the object and it has a value. 72 00:03:12,840 --> 00:03:13,673 Okay. 73 00:03:13,673 --> 00:03:14,880 So that's a good sanity check. 74 00:03:14,880 --> 00:03:17,610 Yes, my personal object does have a name property 75 00:03:17,610 --> 00:03:18,753 and there it is. 76 00:03:19,620 --> 00:03:23,080 The opposite, checking for a missing property 77 00:03:23,970 --> 00:03:28,110 using to be undefined checks if a property is undefined. 78 00:03:28,110 --> 00:03:28,943 So here we go. 79 00:03:28,943 --> 00:03:30,210 Here's the same person again. 80 00:03:30,210 --> 00:03:32,403 How remarkably young that person is. 81 00:03:33,300 --> 00:03:34,710 I'm check taking that person 82 00:03:34,710 --> 00:03:36,480 and I'm checking that, that person's 83 00:03:36,480 --> 00:03:40,440 that person object does not have a company jet property. 84 00:03:40,440 --> 00:03:43,830 As far as I'm aware I don't own a company jet 85 00:03:43,830 --> 00:03:45,900 although that would be quite nice, but I don't have one yet. 86 00:03:45,900 --> 00:03:50,430 So this person object, if we check the company jet property 87 00:03:50,430 --> 00:03:54,750 the company jet property is undefined on that object. 88 00:03:54,750 --> 00:03:55,583 Okay. 89 00:03:55,583 --> 00:03:58,563 So in other words this object does not have that property. 90 00:03:59,400 --> 00:04:00,400 That's quite useful. 91 00:04:01,470 --> 00:04:03,787 One day I might be able to come back 92 00:04:03,787 --> 00:04:06,870 and flip this to be defined property. 93 00:04:06,870 --> 00:04:08,160 Maybe. 94 00:04:08,160 --> 00:04:09,138 Right. 95 00:04:09,138 --> 00:04:09,971 What about not a number? 96 00:04:09,971 --> 00:04:12,960 Not a number occurs sometimes when you're doing 97 00:04:12,960 --> 00:04:15,360 various mathematical operations 98 00:04:15,360 --> 00:04:19,560 it fails basically, and you get back a non-numeric result. 99 00:04:19,560 --> 00:04:20,710 So have a look at this. 100 00:04:21,630 --> 00:04:25,620 If your maths is quite up to date. 101 00:04:25,620 --> 00:04:29,220 If you try to take the square root of a negative number, 102 00:04:29,220 --> 00:04:32,010 you can't take the square root of a negative number. 103 00:04:32,010 --> 00:04:35,130 Not unless you start talking about complex numbers anyway. 104 00:04:35,130 --> 00:04:38,430 So when you call math dot square root 105 00:04:38,430 --> 00:04:40,383 if you give it a negative parameter, 106 00:04:41,340 --> 00:04:42,780 it gives you back none. 107 00:04:42,780 --> 00:04:43,613 Okay. 108 00:04:43,613 --> 00:04:44,790 It's not a real number. 109 00:04:44,790 --> 00:04:47,040 So the square root of minus 25 110 00:04:47,040 --> 00:04:49,473 I'm expecting to be not a number. 111 00:04:50,400 --> 00:04:51,570 If I take the square root 112 00:04:51,570 --> 00:04:54,630 of a regular number, then that's fine. 113 00:04:54,630 --> 00:04:57,600 I'm expecting that not to be not a number. 114 00:04:57,600 --> 00:04:58,752 Okay. 115 00:04:58,752 --> 00:05:02,490 So the square root of minus 25 is effectively invalid. 116 00:05:02,490 --> 00:05:06,420 The square root of plus 25 is not invalid. 117 00:05:06,420 --> 00:05:08,310 In other words is valid. 118 00:05:08,310 --> 00:05:09,600 Okay, so that can be quite useful. 119 00:05:09,600 --> 00:05:10,433 If you wanna check 120 00:05:10,433 --> 00:05:13,803 that your numbers are actually real correct numbers.