1 00:00:06,570 --> 00:00:09,510 - In this section, we're going to see how you can test 2 00:00:09,510 --> 00:00:12,360 if a particular error condition has occurred. 3 00:00:12,360 --> 00:00:13,860 Obviously, this is important, 4 00:00:13,860 --> 00:00:17,130 if you have industrial-strength applications. 5 00:00:17,130 --> 00:00:18,690 If you call a function, 6 00:00:18,690 --> 00:00:21,060 you wanna test that it's robust enough 7 00:00:21,060 --> 00:00:23,433 to report an error when it should. 8 00:00:24,570 --> 00:00:27,000 So, there are two different matchers you can use, 9 00:00:27,000 --> 00:00:28,770 they're actually equivalent to each other. 10 00:00:28,770 --> 00:00:30,870 You can either say toThrow, you know, 11 00:00:30,870 --> 00:00:32,160 when I call this function, 12 00:00:32,160 --> 00:00:35,130 I'm expecting it to throw an error. 13 00:00:35,130 --> 00:00:38,040 So the toThrow matcher, you can use. 14 00:00:38,040 --> 00:00:41,310 Also, you can use toThrowError, these are equivalent. 15 00:00:41,310 --> 00:00:44,010 You can either use toThrow, or toThrow error, 16 00:00:44,010 --> 00:00:45,660 they mean exactly the same thing. 17 00:00:46,530 --> 00:00:48,570 It's quite straightforward. 18 00:00:48,570 --> 00:00:50,700 We've got some examples, usual place, 19 00:00:50,700 --> 00:00:55,200 in the standard jest matchers folder, exampletest.js. 20 00:00:55,200 --> 00:00:59,400 So in the demo folder, lesson two, standard jest matchers. 21 00:00:59,400 --> 00:01:03,270 And if you open that in your code editor, 22 00:01:03,270 --> 00:01:04,740 it looks like this. 23 00:01:04,740 --> 00:01:07,380 And here's my example test file. 24 00:01:07,380 --> 00:01:09,030 It contains the various test suites 25 00:01:09,030 --> 00:01:11,850 that we've been lookin' at so far in the lesson. 26 00:01:11,850 --> 00:01:12,960 And then at the end, 27 00:01:12,960 --> 00:01:15,960 there's a suite that matchers for errors. 28 00:01:15,960 --> 00:01:18,780 Quite a simple example at the moment, 29 00:01:18,780 --> 00:01:23,280 it just has a single test, that checks if errors occur. 30 00:01:23,280 --> 00:01:25,710 So as before, the first thing I'm going to do 31 00:01:25,710 --> 00:01:28,260 is to run that suite 32 00:01:28,260 --> 00:01:30,870 to make sure that I get back the correct result. 33 00:01:30,870 --> 00:01:34,890 So in the standard jest matchers folder, in here. 34 00:01:34,890 --> 00:01:39,890 NPM run test parameters minus T errors. 35 00:01:41,700 --> 00:01:46,350 And it'll find my test suite, my error-related test suite. 36 00:01:46,350 --> 00:01:49,140 It'll run the one and only test in there. 37 00:01:49,140 --> 00:01:52,230 And that should report back successfully. 38 00:01:52,230 --> 00:01:55,260 There we go, so, matchers for errors. 39 00:01:55,260 --> 00:01:58,140 The one test has passed. 40 00:01:58,140 --> 00:02:01,170 Okay, so that's good, let's have a look at that example. 41 00:02:01,170 --> 00:02:03,270 So we ran it like so. 42 00:02:03,270 --> 00:02:08,270 And here's the code, in the test, using toThrow, 43 00:02:08,670 --> 00:02:13,560 you can test whether a block of code has thrown an error. 44 00:02:13,560 --> 00:02:15,510 So what you do is you have to, 45 00:02:15,510 --> 00:02:16,710 the code that you want to test, 46 00:02:16,710 --> 00:02:18,870 you have to enclose it in a function, 47 00:02:18,870 --> 00:02:21,270 otherwise toThrow won't work. 48 00:02:21,270 --> 00:02:23,520 Okay, so the code that you test 49 00:02:23,520 --> 00:02:26,370 then must be wrapped up inside a function. 50 00:02:26,370 --> 00:02:27,750 And here's my example. 51 00:02:27,750 --> 00:02:29,430 I've got two functions, actually. 52 00:02:29,430 --> 00:02:32,523 In fact, they're two lambdas, or arrow functions. 53 00:02:33,870 --> 00:02:35,013 I've got a badFunc. 54 00:02:36,030 --> 00:02:39,600 Here's my arrow function, or a lambda. 55 00:02:39,600 --> 00:02:42,840 If I call this function, it'll fail, 56 00:02:42,840 --> 00:02:46,113 because this is an undefined variable. 57 00:02:47,054 --> 00:02:48,240 Okay, so I get an undefined, 58 00:02:48,240 --> 00:02:51,030 it'll basically raise an exception, or throw an error, 59 00:02:51,030 --> 00:02:53,250 because this is an undefined variable. 60 00:02:53,250 --> 00:02:55,320 So that's a bad function. 61 00:02:55,320 --> 00:02:59,220 When I call that function, when you specify the function, 62 00:02:59,220 --> 00:03:03,723 that function, when it's invoked, will throw an error. 63 00:03:04,740 --> 00:03:07,950 When I call the good function, this is a good function. 64 00:03:07,950 --> 00:03:09,300 When that function gets invoked, 65 00:03:09,300 --> 00:03:12,150 I'm expecting it not to throw an error. 66 00:03:12,150 --> 00:03:13,920 Okay, so this is simple usage. 67 00:03:13,920 --> 00:03:16,150 A simple way to check whether a function 68 00:03:17,520 --> 00:03:20,403 does throw or doesn't throw an error. 69 00:03:21,480 --> 00:03:24,003 So, it turns out that when you call toThrow, 70 00:03:25,410 --> 00:03:27,390 you can actually pass an optional argument. 71 00:03:27,390 --> 00:03:29,910 We didn't do that on the previous example. 72 00:03:29,910 --> 00:03:32,370 But you can pass an optional argument. 73 00:03:32,370 --> 00:03:34,650 And that allows you to be more rigorous 74 00:03:34,650 --> 00:03:36,558 in your error checkin'. 75 00:03:36,558 --> 00:03:41,340 So for example, you can say, this error that occurred. 76 00:03:41,340 --> 00:03:44,370 Is the error object a particular kind of object? 77 00:03:44,370 --> 00:03:48,750 Okay, so was it a bank balance error, 78 00:03:48,750 --> 00:03:51,180 or was it a customer authentication error? 79 00:03:51,180 --> 00:03:54,573 You can actually check what type of error object was thrown. 80 00:03:55,860 --> 00:03:57,990 You can also check the error message. 81 00:03:57,990 --> 00:04:00,330 So when you throw an error in JavaScript, 82 00:04:00,330 --> 00:04:02,070 you specify an error message. 83 00:04:02,070 --> 00:04:04,920 You can say, does the error message match this string, 84 00:04:04,920 --> 00:04:06,240 insufficient funds? 85 00:04:06,240 --> 00:04:07,860 You know, that kinda thing. 86 00:04:07,860 --> 00:04:09,330 You can check if the error message 87 00:04:09,330 --> 00:04:11,190 matches a regular expression. 88 00:04:11,190 --> 00:04:15,450 So, does the error message include this piece of text, 89 00:04:15,450 --> 00:04:16,650 plus other stuff, 90 00:04:16,650 --> 00:04:18,960 but does it match a particular subset 91 00:04:18,960 --> 00:04:20,313 of a regular expression? 92 00:04:21,420 --> 00:04:23,100 Does it have a substring? 93 00:04:23,100 --> 00:04:26,040 These techniques are quite straightforward to do, 94 00:04:26,040 --> 00:04:29,313 we'll have a look at those in more detail in Lesson Three.