1 00:00:06,600 --> 00:00:08,190 - In this section, we are going to see 2 00:00:08,190 --> 00:00:11,970 how to organize multiple tests into suites. 3 00:00:11,970 --> 00:00:14,280 A suite is a collection of tests. 4 00:00:14,280 --> 00:00:16,680 It helps you organize your thought process 5 00:00:16,680 --> 00:00:18,600 when you have a large code base. 6 00:00:18,600 --> 00:00:20,070 The examples we're going to look at 7 00:00:20,070 --> 00:00:22,950 in this section are example two. 8 00:00:22,950 --> 00:00:27,510 So here we are, we are in lesson one, example two. 9 00:00:27,510 --> 00:00:31,263 I have some JavaScript code that contains several functions. 10 00:00:32,340 --> 00:00:36,900 I have a test file, which organizes tests into suites, 11 00:00:36,900 --> 00:00:40,320 collections of tests, and a package.json file 12 00:00:40,320 --> 00:00:44,820 that just defines some handy scripts to run the tests. 13 00:00:44,820 --> 00:00:49,820 If I open this folder in Visual Studio Code, here we are. 14 00:00:50,190 --> 00:00:51,450 We have the test file, 15 00:00:51,450 --> 00:00:54,330 which we'll look at in detail in a moment. 16 00:00:54,330 --> 00:00:57,475 We have the code, and as you can see here, 17 00:00:57,475 --> 00:01:00,134 I've got some simple functions 18 00:01:00,134 --> 00:01:03,870 to add and subtract two numbers, to multiply and divide, 19 00:01:03,870 --> 00:01:05,970 and I've exported all of them. 20 00:01:05,970 --> 00:01:08,247 This is how you export multiple symbols 21 00:01:08,247 --> 00:01:10,449 in a common JS module. 22 00:01:10,449 --> 00:01:14,152 You set the module exports property to be an object. 23 00:01:14,152 --> 00:01:17,762 In that object, you specify the names of the functions 24 00:01:17,762 --> 00:01:19,230 that you want to export. 25 00:01:19,230 --> 00:01:22,470 So effectively, an object is being exported 26 00:01:22,470 --> 00:01:26,340 and that object has an add function, a subtract function, 27 00:01:26,340 --> 00:01:28,230 a multiply and divide. 28 00:01:28,230 --> 00:01:31,470 Okay, so here we go then. 29 00:01:31,470 --> 00:01:32,400 In our example, 30 00:01:32,400 --> 00:01:35,460 here's a code that we're going to test. 31 00:01:35,460 --> 00:01:38,280 We have an add function, subtract, multiply, divide, 32 00:01:38,280 --> 00:01:40,140 and we export from this module, 33 00:01:40,140 --> 00:01:43,486 from the script, we export all those four functions. 34 00:01:43,486 --> 00:01:44,730 Fair enough. 35 00:01:44,730 --> 00:01:47,160 Okay, so in our test file 36 00:01:47,160 --> 00:01:49,470 we are going to have two different suites. 37 00:01:49,470 --> 00:01:51,450 A suite as a group of tests. 38 00:01:51,450 --> 00:01:54,990 You define a suite using the describe function. 39 00:01:54,990 --> 00:01:58,140 The describe function creates a suite 40 00:01:58,140 --> 00:01:59,880 and it takes two parameters. 41 00:01:59,880 --> 00:02:02,880 The first parameter is the name of the suite. 42 00:02:02,880 --> 00:02:04,740 It's just a free format to text in. 43 00:02:04,740 --> 00:02:08,681 It describes the purpose of the tests in this first suite 44 00:02:08,681 --> 00:02:12,180 and the tests in this second suite. 45 00:02:12,180 --> 00:02:14,850 And the second parameter to the described function 46 00:02:14,850 --> 00:02:19,850 is a Lam function or Lambda or arrow function. 47 00:02:20,100 --> 00:02:24,210 And in here, you define all the tests 48 00:02:24,210 --> 00:02:25,680 for additive operations. 49 00:02:25,680 --> 00:02:28,320 So I'm going to have a bunch of tests in this suite 50 00:02:28,320 --> 00:02:30,982 to test the add and subtract functions. 51 00:02:30,982 --> 00:02:33,570 I've got another suite here which defines 52 00:02:33,570 --> 00:02:35,220 my multiplicative functions. 53 00:02:35,220 --> 00:02:38,460 So in this Lambda, I'll implement my tests 54 00:02:38,460 --> 00:02:41,160 for the multiply and divide operations. 55 00:02:41,160 --> 00:02:44,280 Let's take a look at that file, "example.test.js". 56 00:02:44,280 --> 00:02:45,398 Let's have a look at that. 57 00:02:45,398 --> 00:02:46,976 So here it is. 58 00:02:46,976 --> 00:02:50,670 First of all, I need to import those functions. 59 00:02:50,670 --> 00:02:55,061 So I'm going to import from my example.js file, from here. 60 00:02:55,061 --> 00:03:00,061 Remember, in my example JS, I exported an object. 61 00:03:00,660 --> 00:03:02,220 Okay, so bear that in mind. 62 00:03:02,220 --> 00:03:06,180 What I then import, this basically is an object 63 00:03:06,180 --> 00:03:07,320 that contains a property 64 00:03:07,320 --> 00:03:11,820 called add, subtract, multiply, and divide, right? 65 00:03:11,820 --> 00:03:13,502 So that's an object. 66 00:03:13,502 --> 00:03:15,870 Okay, so here's my first suite. 67 00:03:15,870 --> 00:03:16,860 In this first suite, 68 00:03:16,860 --> 00:03:19,230 which has this textual descriptor, 69 00:03:19,230 --> 00:03:23,902 I have the body of my suite contains two tests. 70 00:03:23,902 --> 00:03:26,595 The first test for the add function, 71 00:03:26,595 --> 00:03:31,020 and the second test for the subtract function. 72 00:03:31,020 --> 00:03:34,500 So just one thing that's worth noting here. 73 00:03:34,500 --> 00:03:38,820 This is an object that contains the imported stuff. 74 00:03:38,820 --> 00:03:41,550 So in that object, I called form that object, 75 00:03:41,550 --> 00:03:42,690 I invoke the add function. 76 00:03:42,690 --> 00:03:46,080 So that is basically invoking the add function 77 00:03:46,080 --> 00:03:47,760 that was exported here. 78 00:03:47,760 --> 00:03:50,880 Okay, so whatever object name you give it there, 79 00:03:50,880 --> 00:03:52,200 you would use that as the prefix. 80 00:03:52,200 --> 00:03:55,260 So I'm calling the add function to add two numbers. 81 00:03:55,260 --> 00:03:58,740 My math suggests that the results should be 30, 82 00:03:58,740 --> 00:04:00,780 and then I'm gonna call the subtract function. 83 00:04:00,780 --> 00:04:04,710 So these are functions, the add and subtract functions. 84 00:04:04,710 --> 00:04:07,620 I've put them under this banner. 85 00:04:07,620 --> 00:04:10,350 That's my suite of additive functions. 86 00:04:10,350 --> 00:04:13,140 One of the reasons for grouping tests 87 00:04:13,140 --> 00:04:16,320 into a suite is that you can run suites separately. 88 00:04:16,320 --> 00:04:17,370 We'll see that in a moment. 89 00:04:17,370 --> 00:04:21,150 You could just run all the functions in this suite 90 00:04:21,150 --> 00:04:23,013 or you can just run all the functions in this suite. 91 00:04:23,013 --> 00:04:26,730 It's a way of collectively deciding what tests to run. 92 00:04:26,730 --> 00:04:30,155 In my second suite, so it has a textual descriptor, 93 00:04:30,155 --> 00:04:33,060 "multiplicative functions." 94 00:04:33,060 --> 00:04:37,500 The parameter here is just a function. 95 00:04:37,500 --> 00:04:41,220 It's just a grouping mechanism, really, inside that group. 96 00:04:41,220 --> 00:04:43,410 I've got another two functions to test, 97 00:04:43,410 --> 00:04:45,870 that the multiply function works, 98 00:04:45,870 --> 00:04:49,050 and that the divide function also works. 99 00:04:49,050 --> 00:04:49,920 Okay, so there we go. 100 00:04:49,920 --> 00:04:52,680 So what's new here is the described function. 101 00:04:52,680 --> 00:04:53,790 This creates a suite. 102 00:04:53,790 --> 00:04:56,389 I've got one suite for my additive functions, 103 00:04:56,389 --> 00:05:00,780 and another suite for my multiplicative functions. 104 00:05:00,780 --> 00:05:03,690 Okay, so that's fair enough. 105 00:05:03,690 --> 00:05:06,810 Now, if you want to, of course, you can run all the tests. 106 00:05:06,810 --> 00:05:10,680 You can say "yarn test," or you can just say "npm run test" 107 00:05:10,680 --> 00:05:12,840 and that will run all the tests, 108 00:05:12,840 --> 00:05:16,620 in all the suites, in all the files in the current folder. 109 00:05:16,620 --> 00:05:18,060 So we'll do that first. 110 00:05:18,060 --> 00:05:21,513 Got a command window here opened into example two. 111 00:05:22,470 --> 00:05:27,470 Okay, so if I just do an npm run test, 112 00:05:28,860 --> 00:05:30,210 it'll run all the tests. 113 00:05:30,210 --> 00:05:32,280 So there were four tests altogether, 114 00:05:32,280 --> 00:05:34,470 two tests in the additive suite 115 00:05:34,470 --> 00:05:36,750 and two tests in the multiplicative suite. 116 00:05:36,750 --> 00:05:38,650 And it runs them all, as we shall see. 117 00:05:42,180 --> 00:05:44,790 Right, so it's detected that file 118 00:05:44,790 --> 00:05:47,737 and it displays the name of the first suite, 119 00:05:47,737 --> 00:05:48,960 "additive functions." 120 00:05:48,960 --> 00:05:51,540 In that suite, it found those two functions. 121 00:05:51,540 --> 00:05:54,300 So remember, what we see being displayed here 122 00:05:54,300 --> 00:05:55,833 is the name of the suite, 123 00:05:56,850 --> 00:05:58,710 and what we see being displayed here, 124 00:05:58,710 --> 00:06:01,650 the test that passed, "adds numbers correctly," 125 00:06:01,650 --> 00:06:05,340 that's the descriptor for the first test, and it worked. 126 00:06:05,340 --> 00:06:07,477 And the descriptor for the second test, 127 00:06:07,477 --> 00:06:09,362 "subtracts numbers correctly," 128 00:06:09,362 --> 00:06:13,650 obviously, that's this function here. 129 00:06:13,650 --> 00:06:16,410 And it basically executed all the tests in the file. 130 00:06:16,410 --> 00:06:20,400 So the tests in the additive suite all ran correctly 131 00:06:20,400 --> 00:06:22,860 and they have a green tick there. 132 00:06:22,860 --> 00:06:26,460 And the tests in the multiplicative suite also all ran. 133 00:06:26,460 --> 00:06:31,460 So all together, we had four tests that succeeded. 134 00:06:32,160 --> 00:06:34,860 Okay, so that's marvelous. 135 00:06:34,860 --> 00:06:37,080 But you know, if you have a large code base, 136 00:06:37,080 --> 00:06:40,560 you might have hundreds of tests and dozens of suites, 137 00:06:40,560 --> 00:06:42,270 and you don't want to run all the tests every time, 138 00:06:42,270 --> 00:06:43,680 because it might take too long. 139 00:06:43,680 --> 00:06:44,513 So if you want to, 140 00:06:44,513 --> 00:06:47,100 you can just choose to run tests from a particular suite. 141 00:06:47,100 --> 00:06:48,930 And that's the purpose of suites. 142 00:06:48,930 --> 00:06:51,180 You can choose which tests to run 143 00:06:51,180 --> 00:06:52,830 instead of running the whole thing. 144 00:06:52,830 --> 00:06:55,680 So if you want to run just one suite, you can do this. 145 00:06:55,680 --> 00:06:57,780 You can say "yarn test" 146 00:06:57,780 --> 00:07:02,780 and the -t option followed by a partial suite descriptor. 147 00:07:03,300 --> 00:07:06,840 This is part of the name of my first suite. 148 00:07:06,840 --> 00:07:09,780 Okay, so I could run that command, yarn test -t. 149 00:07:09,780 --> 00:07:12,780 And it looks for any suites that have a name 150 00:07:12,780 --> 00:07:14,970 that partially matches that. 151 00:07:14,970 --> 00:07:17,092 And it'll run the tests in that suite. 152 00:07:17,092 --> 00:07:19,440 You can do the same using npm 153 00:07:19,440 --> 00:07:21,300 and you have to be a bit more careful. 154 00:07:21,300 --> 00:07:24,641 When you say npm run, to run the tests, 155 00:07:24,641 --> 00:07:26,580 you have to have a double dash. 156 00:07:26,580 --> 00:07:29,153 The double dash syntax, when using npm, 157 00:07:29,153 --> 00:07:33,240 it indicates that these parameters here, -t additive, 158 00:07:33,240 --> 00:07:37,230 these parameters are destined for the test command. 159 00:07:37,230 --> 00:07:39,510 Okay? If you didn't put the -- 160 00:07:39,510 --> 00:07:42,813 then these parameters would be passed into npm run. 161 00:07:43,890 --> 00:07:45,600 But the -- is like a separator. 162 00:07:45,600 --> 00:07:47,430 It says, no, no, these parameters 163 00:07:47,430 --> 00:07:49,410 aren't part of an npm thing. 164 00:07:49,410 --> 00:07:52,170 These parameters are part of the test command, 165 00:07:52,170 --> 00:07:55,339 so that these parameters will be passed into the test. 166 00:07:55,339 --> 00:07:58,380 And the test will just run the additive suite. 167 00:07:58,380 --> 00:08:01,110 So I'm gonna do that now. 168 00:08:01,110 --> 00:08:05,850 So npm run, I'm gonna run the test script 169 00:08:05,850 --> 00:08:09,030 as defined in the package.json file, remember? 170 00:08:09,030 --> 00:08:11,640 And then, parameters into the test command. 171 00:08:11,640 --> 00:08:13,380 I'm going to pass the -t option 172 00:08:13,380 --> 00:08:15,660 to say which tests I want to run. 173 00:08:15,660 --> 00:08:20,040 All tests in any suite that partially matches 174 00:08:20,040 --> 00:08:22,920 this name, "additive." 175 00:08:22,920 --> 00:08:25,710 Okay, so let's see what that displays. 176 00:08:25,710 --> 00:08:27,300 Quite exciting, isn't it? 177 00:08:27,300 --> 00:08:30,663 So it detected my test file. 178 00:08:32,070 --> 00:08:36,720 It realized that this suite matched that partial name. 179 00:08:36,720 --> 00:08:39,840 So it ran the tests in that suite. 180 00:08:39,840 --> 00:08:42,990 Notice, it also detected the other suite, 181 00:08:42,990 --> 00:08:44,550 but it skipped those tests 182 00:08:44,550 --> 00:08:47,790 because we told it just to run the additive tests. 183 00:08:47,790 --> 00:08:52,290 Okay, so define the tests, grouping tests in suites, 184 00:08:52,290 --> 00:08:53,970 it helps you organizationally 185 00:08:53,970 --> 00:08:57,000 but it also helps you when you're running the tests. 186 00:08:57,000 --> 00:08:59,160 You can choose which tests to run 187 00:08:59,160 --> 00:09:01,560 so that you can get your work done more quickly.