1 00:00:06,642 --> 00:00:10,651 - By default, Jest imposes a maximum timeout of 5 seconds 2 00:00:10,651 --> 00:00:13,170 for a test to complete. 3 00:00:13,170 --> 00:00:15,420 Which is reasonable for most tests 4 00:00:15,420 --> 00:00:17,309 in a unit-test environment. 5 00:00:17,309 --> 00:00:19,800 On the other hand, if you do have a test that takes longer 6 00:00:19,800 --> 00:00:24,270 than 5 seconds, you have configure Jest to wait longer 7 00:00:24,270 --> 00:00:26,220 so that it doesn't give up the ghost. 8 00:00:26,220 --> 00:00:28,770 So we're going to see how to do that in this lesson, 9 00:00:28,770 --> 00:00:30,270 in this section. 10 00:00:30,270 --> 00:00:34,110 If you go into the TestingCallbacks_Timeouts folder, 11 00:00:34,110 --> 00:00:37,380 this is what it looks like, in the code listings. 12 00:00:37,380 --> 00:00:40,230 If you open up that folder in the code editor, 13 00:00:40,230 --> 00:00:44,250 you'll see we have test code here. 14 00:00:44,250 --> 00:00:46,290 The code we're gonna actually test here, 15 00:00:46,290 --> 00:00:48,450 let's look at that function first. 16 00:00:48,450 --> 00:00:51,000 Here is a very slow operation. 17 00:00:51,000 --> 00:00:53,970 It waits 8 seconds, and then after 8 seconds, 18 00:00:53,970 --> 00:00:56,880 it calls the callback function. 19 00:00:56,880 --> 00:01:00,330 So, if I try to test that function, it's gonna blow up. 20 00:01:00,330 --> 00:01:02,820 Jest is gonna give up after 5 seconds. 21 00:01:02,820 --> 00:01:05,970 It can't bother to wait beyond that time. 22 00:01:05,970 --> 00:01:10,050 So, we're gonna see how to configure Jest to wait longer. 23 00:01:10,050 --> 00:01:12,030 Okay, so, here's the function we just looked at, 24 00:01:12,030 --> 00:01:16,710 verySlowOperation, it has an 8-second time span 25 00:01:16,710 --> 00:01:17,913 before it completes. 26 00:01:18,870 --> 00:01:22,470 Okay, and here's my test, as you can see here. 27 00:01:22,470 --> 00:01:25,350 By default, Jest will allow 5 seconds 28 00:01:25,350 --> 00:01:26,550 for this to complete. 29 00:01:26,550 --> 00:01:29,520 So, I've got the done prompter here, 30 00:01:29,520 --> 00:01:31,800 I seem to be doing things correctly. 31 00:01:31,800 --> 00:01:35,463 When this test executes, it calls that slow operation. 32 00:01:36,420 --> 00:01:40,110 Ideally, after 8 seconds, the testResult callback will 33 00:01:40,110 --> 00:01:42,630 be evoked with the return value. 34 00:01:42,630 --> 00:01:45,240 And at that point, I will tell Jest, "I'm done here." 35 00:01:45,240 --> 00:01:48,540 The problem is that it's going to take more than 5 seconds 36 00:01:48,540 --> 00:01:50,340 for this callback to occur, 37 00:01:50,340 --> 00:01:52,740 and Jest doesn't wait that long. 38 00:01:52,740 --> 00:01:55,110 So, let's see what happens if we run this test 39 00:01:55,110 --> 00:01:55,983 as it stands. 40 00:01:57,030 --> 00:01:59,430 If I run the test using the default timeout, 41 00:01:59,430 --> 00:02:02,730 that's the name of the function I just showed you, 42 00:02:02,730 --> 00:02:05,700 this is the error that you'll get back. 43 00:02:05,700 --> 00:02:09,450 Basically, it shows that Jest did not exit one second 44 00:02:09,450 --> 00:02:11,730 after the test run completed. 45 00:02:11,730 --> 00:02:15,540 It ran all test suites with test matching that timer, 46 00:02:15,540 --> 00:02:17,760 but it failed, okay. 47 00:02:17,760 --> 00:02:20,400 It says that there was an exceeded timeout, 48 00:02:20,400 --> 00:02:23,845 there was a 5 a second timeout of 5000 milliseconds, 49 00:02:23,845 --> 00:02:27,150 due in which that test didn't complete. 50 00:02:27,150 --> 00:02:28,350 You could actually run that test, 51 00:02:28,350 --> 00:02:30,390 so you can see the 5 seconds elapse 52 00:02:30,390 --> 00:02:32,790 before the error message actually appears. 53 00:02:32,790 --> 00:02:34,080 Here's my command window. 54 00:02:34,080 --> 00:02:35,760 So I'll run Jest, 55 00:02:35,760 --> 00:02:37,050 and as an option, 56 00:02:37,050 --> 00:02:39,997 I'll pass in the name of my test: 57 00:02:39,997 --> 00:02:44,997 "test using default timeout." 58 00:02:47,790 --> 00:02:50,163 Okay, so, it'll call my test. 59 00:02:51,000 --> 00:02:53,370 It'll give my test up to 5 seconds, 60 00:02:53,370 --> 00:02:56,460 thinking "surely, that's going to be long enough." 61 00:02:56,460 --> 00:02:58,230 And, well, it isn't long enough, 62 00:02:58,230 --> 00:03:00,360 and "stop calling me Shirley," 63 00:03:00,360 --> 00:03:02,340 as they say in the airplane movie. 64 00:03:02,340 --> 00:03:03,173 There we go. 65 00:03:03,173 --> 00:03:06,840 So, it exceeded the timeout of 5000 milliseconds 66 00:03:06,840 --> 00:03:08,163 for that test to complete. 67 00:03:09,300 --> 00:03:10,770 Okay, so, now the problem is 68 00:03:10,770 --> 00:03:13,260 if the function takes longer than 5 seconds, 69 00:03:13,260 --> 00:03:14,940 then it takes longer than 5 seconds. 70 00:03:14,940 --> 00:03:18,630 We have to tell Jest to wait longer. 71 00:03:18,630 --> 00:03:19,890 So, the way you can do that, 72 00:03:19,890 --> 00:03:22,380 is you can actually pass in a third parameter 73 00:03:22,380 --> 00:03:23,910 to the test function. 74 00:03:23,910 --> 00:03:26,373 The first parameter is the name of the test, 75 00:03:27,210 --> 00:03:30,360 and the second parameter is basically your lambda, 76 00:03:30,360 --> 00:03:32,880 you know, the function that defines your test, 77 00:03:32,880 --> 00:03:34,530 and here's an extra third parameter, 78 00:03:34,530 --> 00:03:35,910 we haven't seen this before. 79 00:03:35,910 --> 00:03:38,340 This is the number of milliseconds 80 00:03:38,340 --> 00:03:40,260 that for this particular test 81 00:03:40,260 --> 00:03:42,423 that Jest will allow for it to complete. 82 00:03:43,260 --> 00:03:46,590 Okay, so, this test here "test using specific timeout," 83 00:03:46,590 --> 00:03:49,740 it'll allow 10 seconds for that test to complete, 84 00:03:49,740 --> 00:03:51,630 and that should be enough. 85 00:03:51,630 --> 00:03:54,067 So, if I ran that test function now, 86 00:03:54,067 --> 00:03:56,310 "testing using specific timeout," 87 00:03:56,310 --> 00:03:58,830 then it should complete successfully. 88 00:03:58,830 --> 00:04:00,960 I'm just gonna actually do that live, 89 00:04:00,960 --> 00:04:02,193 just to prove the point. 90 00:04:03,090 --> 00:04:06,457 Okay, so, "test using," this test function was called 91 00:04:06,457 --> 00:04:11,187 "test using specific timeout." 92 00:04:12,060 --> 00:04:15,180 So, it'll allow up to 10 seconds. 93 00:04:15,180 --> 00:04:17,670 My function actually takes 8 seconds, 94 00:04:17,670 --> 00:04:19,770 at which point, it calls back, 95 00:04:19,770 --> 00:04:21,930 and then I tell Jest, "we're done now." 96 00:04:21,930 --> 00:04:25,680 So, in 8 seconds, I'm in the process of transpiring, 97 00:04:25,680 --> 00:04:27,150 and then it finished. 98 00:04:27,150 --> 00:04:30,690 Okay, so, there we are on a function-by-function basis, 99 00:04:30,690 --> 00:04:32,430 you can specify the timeout you want 100 00:04:32,430 --> 00:04:34,860 for an individual function to occur. 101 00:04:34,860 --> 00:04:37,440 Now, if you have lots of tests, 102 00:04:37,440 --> 00:04:39,960 and they all need to have an extended timeout, 103 00:04:39,960 --> 00:04:42,450 then you can define kind of like a global timeout, 104 00:04:42,450 --> 00:04:44,010 like this. 105 00:04:44,010 --> 00:04:49,010 In some kind of setup code, you'd call "jest.setTimeout," 106 00:04:49,230 --> 00:04:51,780 and this would then apply for all functions, 107 00:04:51,780 --> 00:04:53,250 all test functions by default. 108 00:04:53,250 --> 00:04:57,210 This is the new default for all my test functions. 109 00:04:57,210 --> 00:05:00,210 Okay, so, make that function call somewhere 110 00:05:00,210 --> 00:05:01,830 in your test code. 111 00:05:01,830 --> 00:05:04,410 I put in the file called "setupTests.js," 112 00:05:04,410 --> 00:05:06,780 there's nothing special about that file name, 113 00:05:06,780 --> 00:05:09,570 you do have to tell Jest about that file name, 114 00:05:09,570 --> 00:05:11,580 and we've seen before, 115 00:05:11,580 --> 00:05:14,730 in your Jest config file- that is important, 116 00:05:14,730 --> 00:05:16,387 that file name- you say: 117 00:05:16,387 --> 00:05:20,310 "this is the code I want to run to configure Jest." 118 00:05:20,310 --> 00:05:23,010 So, you give it the name of your setupTests file- 119 00:05:23,010 --> 00:05:25,110 or multiple files if you want to- 120 00:05:25,110 --> 00:05:28,392 it'll then configure that test, that timeout for all tests, 121 00:05:28,392 --> 00:05:31,050 and we're in business. 122 00:05:31,050 --> 00:05:33,870 When I have done that, in the demos, 123 00:05:33,870 --> 00:05:35,040 you might be wondering why 124 00:05:35,040 --> 00:05:38,640 that test timeout didn't actually kick in. 125 00:05:38,640 --> 00:05:42,480 I do have a setupTests, but it's commented out. 126 00:05:42,480 --> 00:05:45,930 For the purposes of the demo, that statement was commented. 127 00:05:45,930 --> 00:05:48,060 So I'll uncomment it. 128 00:05:48,060 --> 00:05:51,060 Okay, so, that now applies by default 129 00:05:51,060 --> 00:05:53,100 for all tests. 130 00:05:53,100 --> 00:05:56,520 Don't forget you have to tell Jest about that file. 131 00:05:56,520 --> 00:05:57,960 So in your Jest config, 132 00:05:57,960 --> 00:05:59,520 you do have to point it to that file, 133 00:05:59,520 --> 00:06:00,567 like I've done. 134 00:06:00,567 --> 00:06:03,572 And now, all tests will be subjected to set 135 00:06:03,572 --> 00:06:05,730 10-second timeout. 136 00:06:05,730 --> 00:06:09,390 So, in my tests, this first test, 137 00:06:09,390 --> 00:06:10,800 that didn't have timeout, 138 00:06:10,800 --> 00:06:12,720 will now default to ten. 139 00:06:12,720 --> 00:06:13,620 Okay. 140 00:06:13,620 --> 00:06:16,770 And that one has 10 specified anyway. 141 00:06:16,770 --> 00:06:19,740 And this test here doesn't specify a timeout, 142 00:06:19,740 --> 00:06:22,260 it will also default to 10 seconds. 143 00:06:22,260 --> 00:06:25,593 All tests, now, will have a default of 10 seconds. 144 00:06:26,430 --> 00:06:27,990 So, I can run all the tests, 145 00:06:27,990 --> 00:06:29,790 and they should all work. 146 00:06:29,790 --> 00:06:31,835 That's the theory, so I run all the tests like this, 147 00:06:31,835 --> 00:06:36,720 and we should find that all of those 3 tests work fine. 148 00:06:36,720 --> 00:06:38,751 The proof of the pudding is in the eating, so, 149 00:06:38,751 --> 00:06:40,380 let's actually try that. 150 00:06:40,380 --> 00:06:41,820 So, "jest." 151 00:06:41,820 --> 00:06:44,880 So, it's going to run, it's going to allow 10 seconds 152 00:06:44,880 --> 00:06:48,120 for each test by default, so this is gonna take 153 00:06:48,120 --> 00:06:50,370 probably around 24 seconds, 154 00:06:50,370 --> 00:06:53,640 by the time it allows 8 seconds for the first test, 155 00:06:53,640 --> 00:06:55,440 8 seconds for the second one, 156 00:06:55,440 --> 00:06:56,850 8 seconds for the third one, 157 00:06:56,850 --> 00:06:59,287 and then after 24 seconds, it should say, 158 00:06:59,287 --> 00:07:00,637 "everything's worked fine." 159 00:07:01,800 --> 00:07:03,540 Okay, so, I think we must getting quite close 160 00:07:03,540 --> 00:07:08,013 to the 24 seconds now, the excitement is growing. 161 00:07:09,060 --> 00:07:11,220 24 seconds seems like an awful long time 162 00:07:11,220 --> 00:07:13,470 when you're waiting for the finish. 163 00:07:13,470 --> 00:07:14,550 Oh, there we go. 164 00:07:14,550 --> 00:07:17,460 Okay, so, you can either set a global timeout, 165 00:07:17,460 --> 00:07:19,770 or you can set a timeout on a test-by-test basis. 166 00:07:19,770 --> 00:07:22,920 You might find that some tests need longer, 167 00:07:22,920 --> 00:07:24,810 and some tests need shorter. 168 00:07:24,810 --> 00:07:28,170 So, either pass in a third parameter -t test function 169 00:07:28,170 --> 00:07:31,560 on a case-by-case basis, or set a global timeout 170 00:07:31,560 --> 00:07:33,410 like we've seen in this last example.