1 00:00:06,540 --> 00:00:09,210 - In this section, we're going to see how to test 2 00:00:09,210 --> 00:00:12,330 for truth and falsehood in Jest. 3 00:00:12,330 --> 00:00:13,530 So we have two matchers: 4 00:00:14,580 --> 00:00:16,890 toBeTruthy and toBeFalsy. 5 00:00:16,890 --> 00:00:20,280 So, if you are quite new to JavaScript, 6 00:00:20,280 --> 00:00:21,450 you'd be quite surprised 7 00:00:21,450 --> 00:00:24,840 at how JavaScript interprets truth and falsehood. 8 00:00:24,840 --> 00:00:29,640 It has... a rather... vague definition 9 00:00:29,640 --> 00:00:31,380 or a rather loose definition 10 00:00:31,380 --> 00:00:33,750 of what's true and what's false. 11 00:00:33,750 --> 00:00:36,450 So, the key point with these tests 12 00:00:36,450 --> 00:00:39,060 is to know what does truth actually mean, 13 00:00:39,060 --> 00:00:41,520 and falsehood, in JavaScript. 14 00:00:41,520 --> 00:00:42,353 Okay. 15 00:00:42,353 --> 00:00:45,573 So, I have a test suite we're gonna look at. 16 00:00:46,530 --> 00:00:47,880 Similar to the other sections, 17 00:00:47,880 --> 00:00:51,870 it's going to be in the StandardJestMatchers folder. 18 00:00:51,870 --> 00:00:53,670 example.test.js. 19 00:00:53,670 --> 00:00:57,090 I've got a test suite that contains my tests 20 00:00:57,090 --> 00:00:59,850 that match for truth or falsehood. 21 00:00:59,850 --> 00:01:02,973 So, let's have a quick look in the file system. 22 00:01:04,020 --> 00:01:06,690 Okay, so in my file system, in Lesson02, 23 00:01:06,690 --> 00:01:09,450 in the StandardJestMatchers folder. 24 00:01:09,450 --> 00:01:11,880 Open that folder in the code editor. 25 00:01:11,880 --> 00:01:13,620 And it looks like this. 26 00:01:13,620 --> 00:01:16,770 We have our tests in here, example.test.js. 27 00:01:16,770 --> 00:01:20,550 The suite we are looking at for this section is this one: 28 00:01:20,550 --> 00:01:22,950 describe matchers for truth and falsehood. 29 00:01:22,950 --> 00:01:25,560 And in there I have two tests. 30 00:01:25,560 --> 00:01:26,850 Would you believe it? 31 00:01:26,850 --> 00:01:30,843 I have a test that exercises the toBeTruthy matcher. 32 00:01:31,710 --> 00:01:36,090 And then I have a test exercises the toBeFalsy matcher. 33 00:01:36,090 --> 00:01:38,760 Okay, so truthy and falsy 34 00:01:38,760 --> 00:01:42,093 are definitely a concept in JavaScript. 35 00:01:43,140 --> 00:01:44,880 Okay, so... 36 00:01:44,880 --> 00:01:45,900 I guess the first thing we should do 37 00:01:45,900 --> 00:01:47,700 is to actually run those tests. 38 00:01:47,700 --> 00:01:48,533 First of all, to make sure 39 00:01:48,533 --> 00:01:50,730 that they all actually work successfully. 40 00:01:50,730 --> 00:01:52,440 So let's do that first. 41 00:01:52,440 --> 00:01:57,440 So, npm run test -- -t truth. 42 00:01:57,870 --> 00:02:02,333 Okay, so that'll select my truth and falsehood suite. 43 00:02:04,200 --> 00:02:06,780 And it'll run the two tests in that suite. 44 00:02:06,780 --> 00:02:09,483 Let's just verify that they actually work properly. 45 00:02:11,130 --> 00:02:13,500 And then we'll have a look at the code. 46 00:02:13,500 --> 00:02:17,010 So you have to be patient when you're running your tests. 47 00:02:17,010 --> 00:02:21,360 And it's located my truth or falsehood suite. 48 00:02:21,360 --> 00:02:22,560 And it's run those two tests, 49 00:02:22,560 --> 00:02:24,120 and they've both worked. 50 00:02:24,120 --> 00:02:25,020 Very good. 51 00:02:25,020 --> 00:02:26,550 Let's have a look at the code. 52 00:02:26,550 --> 00:02:29,220 So first of all, toBeTruthy, 53 00:02:29,220 --> 00:02:30,750 it tests for a truthy value. 54 00:02:30,750 --> 00:02:32,640 So, the key point here, 55 00:02:32,640 --> 00:02:37,080 what is a truthy value actually in JavaScript? 56 00:02:37,080 --> 00:02:39,300 Okay, so basically... (snickers) 57 00:02:39,300 --> 00:02:42,363 Basically, anything is truthy if it's not falsy. 58 00:02:43,260 --> 00:02:45,930 Something is falsy if it is either... 59 00:02:45,930 --> 00:02:47,520 So these are the falsy values: 60 00:02:47,520 --> 00:02:49,110 False. 61 00:02:49,110 --> 00:02:50,850 Any zero number. 62 00:02:50,850 --> 00:02:52,470 An empty string. 63 00:02:52,470 --> 00:02:53,760 A null value. 64 00:02:53,760 --> 00:02:55,140 An undefined value. 65 00:02:55,140 --> 00:02:56,610 Or not a number. 66 00:02:56,610 --> 00:02:59,223 Any of these are considered to be falsy. 67 00:03:00,090 --> 00:03:02,010 Truthy is everything else. 68 00:03:02,010 --> 00:03:03,930 Everything apart from these values 69 00:03:03,930 --> 00:03:05,610 is considered to be truthy. 70 00:03:05,610 --> 00:03:07,530 Have a look at this example here. 71 00:03:07,530 --> 00:03:10,140 I've declared a bunch of variables. 72 00:03:10,140 --> 00:03:13,200 All of these variables are example of truthy values. 73 00:03:13,200 --> 00:03:16,890 So, obviously, the value true is truthy. 74 00:03:16,890 --> 00:03:19,740 A non-zero number is truthy. 75 00:03:19,740 --> 00:03:21,930 A non empty string is truthy. 76 00:03:21,930 --> 00:03:26,400 Now, interestingly, an empty object is actually true. 77 00:03:26,400 --> 00:03:28,290 You might expect that to be false 78 00:03:28,290 --> 00:03:32,400 in the same way that an empty string is false, but no. 79 00:03:32,400 --> 00:03:37,400 An empty object is actually a true value, truthy. 80 00:03:37,470 --> 00:03:39,750 As is an empty array. 81 00:03:39,750 --> 00:03:41,670 Okay, so that's an interesting difference. 82 00:03:41,670 --> 00:03:44,490 An empty string is considered false, 83 00:03:44,490 --> 00:03:48,960 but an empty object or an empty array is considered true. 84 00:03:48,960 --> 00:03:51,960 So if I check a, b, c, d, e, f, g. 85 00:03:51,960 --> 00:03:54,750 All of these will give me a truthy value. 86 00:03:54,750 --> 00:03:57,480 All of these are considered to be true values 87 00:03:57,480 --> 00:04:00,420 in the JavaScript view of truth. 88 00:04:00,420 --> 00:04:03,810 And then, toBeFalsy is the opposite. 89 00:04:03,810 --> 00:04:07,320 A falsy value is any value which is false, 90 00:04:07,320 --> 00:04:10,083 any zero-based number, an empty string, 91 00:04:11,040 --> 00:04:14,910 either null, undefined, or not a number. 92 00:04:14,910 --> 00:04:16,143 So let's have a look. 93 00:04:17,010 --> 00:04:22,010 So a, false, is obviously a falsy value. 94 00:04:22,020 --> 00:04:24,420 Any zero-based number is false. 95 00:04:24,420 --> 00:04:26,550 Whether it's a 0, 96 00:04:26,550 --> 00:04:28,260 or -0, 97 00:04:28,260 --> 00:04:30,480 or a bigint 0, 98 00:04:30,480 --> 00:04:32,700 or a bigint of -0, 99 00:04:32,700 --> 00:04:34,200 or a float. 100 00:04:34,200 --> 00:04:38,250 Okay, so any zero-based number is false. 101 00:04:38,250 --> 00:04:40,710 An empty string is false. 102 00:04:40,710 --> 00:04:44,940 Null, undefined and not a number are also false outcomes. 103 00:04:44,940 --> 00:04:47,220 So if you wrote these in a statement, 104 00:04:47,220 --> 00:04:48,690 they'd give you a false outcome. 105 00:04:48,690 --> 00:04:51,480 So when I check a, b, c, d, e, f, g here, 106 00:04:51,480 --> 00:04:53,368 they are all falsy. 107 00:04:53,368 --> 00:04:57,570 Okay, so, toBeTruthy and toBeFalsy, straightforward. 108 00:04:57,570 --> 00:04:59,220 You just need to be careful to understand 109 00:04:59,220 --> 00:05:02,220 what does truth and falsehood actually mean 110 00:05:02,220 --> 00:05:04,143 in the JavaScript universe.