1 00:00:00,180 --> 00:00:01,300 In the last lesson. 2 00:00:01,320 --> 00:00:06,660 You set up a test case that actually tests an express API endpoint. 3 00:00:06,660 --> 00:00:13,440 Now it worked the first time around but it failed the second time around a second time around it failed 4 00:00:13,470 --> 00:00:17,910 because the email was already registered from the first time it ran. 5 00:00:17,910 --> 00:00:25,290 In this lesson you'll learn how to use just lifecycle methods to run some code just before or after 6 00:00:25,290 --> 00:00:32,160 a test case that will allow you to set up some code that clears the database so your test cases can 7 00:00:32,160 --> 00:00:35,400 consistently execute as expected. 8 00:00:35,400 --> 00:00:42,390 Now you can learn more about this over in the just documentation in the sidebar under introduction. 9 00:00:42,390 --> 00:00:48,510 There's a section called setup and tear down this outlines everything we're going to talk about in this 10 00:00:48,510 --> 00:00:55,560 lesson we'll be using various lifecycle methods such as those shown below to actually configure our 11 00:00:55,560 --> 00:00:56,730 project. 12 00:00:56,730 --> 00:01:03,610 Let's go ahead and do just that over here inside of the user dot test dot J.S. file. 13 00:01:03,660 --> 00:01:10,590 We're going to call a couple of functions that just provides us as Global's similar to how we have access 14 00:01:10,590 --> 00:01:14,140 to expect and to test right here. 15 00:01:14,190 --> 00:01:16,680 One of those is before each. 16 00:01:16,710 --> 00:01:21,600 I'll go ahead and call before each passing in a single argument. 17 00:01:21,600 --> 00:01:28,710 This is a function to run this function runs before each test case in this test suite. 18 00:01:28,710 --> 00:01:33,890 Now in this case we only have one test case so it would only run a single time. 19 00:01:33,990 --> 00:01:39,150 Let's go ahead and add a simple console dot log call inside of here so we can see when exactly this 20 00:01:39,150 --> 00:01:44,700 runs and I'll just log out the message before each down below. 21 00:01:44,700 --> 00:01:51,570 We'll also call after each which runs a single time after each of your test cases in the test suite 22 00:01:51,930 --> 00:01:52,460 right here. 23 00:01:52,470 --> 00:02:00,120 Once again we passed to the function to run and we'll just use console log to print a message after 24 00:02:00,420 --> 00:02:01,380 each. 25 00:02:01,380 --> 00:02:05,390 Now with this in place we can go ahead and save our test suite. 26 00:02:05,430 --> 00:02:10,410 It's still going to fail because we're not actually manipulating the database just yet. 27 00:02:10,530 --> 00:02:17,670 But if I scroll up in the output I should be able to see before in each print and then after each print 28 00:02:17,940 --> 00:02:25,080 afterwards as long as you're seeing before each and after each then everything is actually running correctly. 29 00:02:25,080 --> 00:02:30,390 Now the next step we need to take is to actually do something meaningful and we're going to go ahead 30 00:02:30,390 --> 00:02:34,110 and do something meaningful in before each. 31 00:02:34,110 --> 00:02:40,580 Now we don't need after each for this particular project I did want to let you know that it exists though. 32 00:02:40,710 --> 00:02:46,500 We'll go ahead and remove that from our test suite and we'll just use before each. 33 00:02:46,500 --> 00:02:53,670 Now you also have access to before all and after all which will run just a single time before your test 34 00:02:53,670 --> 00:02:55,620 cases or after them. 35 00:02:55,710 --> 00:03:01,260 In this case though before each is the correct one because we want to make sure that every single test 36 00:03:01,290 --> 00:03:08,310 that runs runs in the same environment with the same test data in the database now for the moment all 37 00:03:08,310 --> 00:03:11,910 we're trying to do is wipe all of the users inside of there. 38 00:03:12,060 --> 00:03:18,090 So our test can consistently run correctly which means we need access to that user model. 39 00:03:18,120 --> 00:03:24,900 So right here I'm going to go ahead and load it in concert uppercase U user I'll use require followed 40 00:03:24,900 --> 00:03:33,360 by dot dot forward slash source forward slash models a forward slash user to load in that Mongoose model 41 00:03:33,900 --> 00:03:39,240 then all we're going to do is use some of the methods we have access to down below to delete all of 42 00:03:39,240 --> 00:03:40,260 the records. 43 00:03:40,260 --> 00:03:47,280 So what I'll actually be doing is removing the console log call and using user dot right here. 44 00:03:47,280 --> 00:03:54,660 Delete many now if we call delete many it's going to delete every single user since we haven't provided 45 00:03:54,690 --> 00:03:56,670 any sort of search criteria. 46 00:03:56,670 --> 00:03:57,600 And that's what we want. 47 00:03:57,630 --> 00:04:00,680 We want to start off with a nice clean slate. 48 00:04:00,870 --> 00:04:07,020 Now currently nothing's going to work as expected because before each has asynchronous code inside of 49 00:04:07,020 --> 00:04:07,570 here. 50 00:04:07,650 --> 00:04:12,000 But we're not doing anything to tell just that it is asynchronous. 51 00:04:12,000 --> 00:04:16,380 So once again we have the same options we hand below with our test case. 52 00:04:16,380 --> 00:04:22,230 I could go ahead and provide the done parameter calling it when I'm done or I could go ahead and set 53 00:04:22,230 --> 00:04:28,500 this up as an async function which is the approach that I'll take down below we just use a wait with 54 00:04:28,500 --> 00:04:29,550 our promise. 55 00:04:29,550 --> 00:04:36,630 This is gonna make sure that the users are deleted before just considers this done once it considers 56 00:04:36,630 --> 00:04:39,290 this done it will move on to the test cases. 57 00:04:39,320 --> 00:04:44,790 So we definitely want to make sure the users are gone before the test runs. 58 00:04:44,790 --> 00:04:50,730 So with this in place we've solved the problem we had before we added four lines of code to the user 59 00:04:50,760 --> 00:04:58,770 test suite but down below our test cases are gonna pass and they will consistently pass over time because 60 00:04:58,770 --> 00:05:04,460 we're always starts off with an empty database which is a good thing we want to make sure that we have 61 00:05:04,520 --> 00:05:08,750 a consistent set of data for our tests to work with. 62 00:05:08,750 --> 00:05:14,480 Now wiping all of the data is a good start but there are also test cases that are going to test features 63 00:05:14,480 --> 00:05:21,380 like logging in and in that case it would be nice to have some very specific data in the database that 64 00:05:21,380 --> 00:05:23,300 we can use when testing. 65 00:05:23,300 --> 00:05:26,920 So we are going to start by deleting all of the users. 66 00:05:27,050 --> 00:05:33,470 Then we'll go ahead and add a user in that could be used with test cases such as test cases for testing 67 00:05:33,470 --> 00:05:36,780 log in which we'll be doing in just a minute or two. 68 00:05:36,800 --> 00:05:42,800 So what we're gonna do is create a new user right here in before each just after we go ahead and call 69 00:05:42,810 --> 00:05:47,330 delete many and I'll define that data up above and then we'll pass it in. 70 00:05:47,330 --> 00:05:52,340 Down below right here let's define a constant which I'll call User 1. 71 00:05:52,340 --> 00:05:58,940 Now I'm calling it User 1 as opposed to just user to leave the door open for other test users later 72 00:05:58,940 --> 00:06:00,340 on if we needed them. 73 00:06:00,500 --> 00:06:04,580 And this is just going to be an object with those basic properties on there. 74 00:06:04,580 --> 00:06:10,100 So right here as an example I will specify the name for our test user. 75 00:06:10,100 --> 00:06:16,070 I'm going to go ahead and use something like Mike and as well I'll provide the email for this test user 76 00:06:16,100 --> 00:06:20,930 Mike at example dot com and then I'll provide a password as well. 77 00:06:22,220 --> 00:06:29,570 I'll go ahead and use a 5 6 watt exclamation mark exclamation mark as my password. 78 00:06:29,570 --> 00:06:30,320 And there we go. 79 00:06:30,350 --> 00:06:34,640 We have all of the data we need to create this test user. 80 00:06:34,640 --> 00:06:40,400 Now down below we actually have to save them to the database and we'll do that just after calling delete 81 00:06:40,400 --> 00:06:41,260 many. 82 00:06:41,300 --> 00:06:45,560 So right here what I'm gonna do is use a wait with the following. 83 00:06:45,560 --> 00:06:51,920 I'll use the new operator with user like we've done so many times before and I'll provide the user one 84 00:06:51,980 --> 00:06:52,860 object. 85 00:06:53,030 --> 00:06:57,800 Then right there I'll just chain on a call to save to save things. 86 00:06:57,830 --> 00:07:04,340 So we're doing everything in one shot as opposed to first storing this in a variable and then referencing 87 00:07:04,340 --> 00:07:07,850 it on the next line though we could do that as well. 88 00:07:07,850 --> 00:07:10,670 For example I can cut this out. 89 00:07:10,760 --> 00:07:15,500 I could have a variable called user which I create up above like this. 90 00:07:15,500 --> 00:07:18,130 These two solutions are identical. 91 00:07:18,170 --> 00:07:23,300 We're gonna go ahead and go with the first solution since everything fits on one line and it works pretty 92 00:07:23,300 --> 00:07:25,640 well for our purposes. 93 00:07:25,640 --> 00:07:29,840 So with this in place our existing test case should still pass. 94 00:07:30,230 --> 00:07:32,050 We have our test user in there. 95 00:07:32,060 --> 00:07:35,680 Yes but the emails don't conflict and that's a good thing. 96 00:07:35,900 --> 00:07:42,800 Right here I'm gonna go ahead and save user dot test dot J S that's going to rerun everything and we 97 00:07:42,800 --> 00:07:49,280 can see we have a passing test suite now the whole point of having this test data in the database is 98 00:07:49,280 --> 00:07:55,700 to test those other endpoints like the ability to log in and that is exactly what we're going to do 99 00:07:55,730 --> 00:07:56,880 down below. 100 00:07:56,900 --> 00:08:02,990 Now don't worry we will expand on our test cases such as this one once we learn more about just but 101 00:08:02,990 --> 00:08:07,920 we're going to go one step at a time so we can keep all of this actually comprehensible. 102 00:08:07,970 --> 00:08:14,120 Right here what we're gonna do is call the test function once again we're gonna pass to it the name 103 00:08:14,120 --> 00:08:14,890 of our test. 104 00:08:14,900 --> 00:08:19,650 Something along the lines of should log in existing user. 105 00:08:19,820 --> 00:08:26,270 Then we'll set up our async function and we can go ahead and actually use super test to test things 106 00:08:26,270 --> 00:08:27,020 out. 107 00:08:27,020 --> 00:08:33,290 So once again I'll use a way to calling request and passing in our Express application exactly like 108 00:08:33,290 --> 00:08:34,710 we've done up above. 109 00:08:34,790 --> 00:08:40,940 Now in this case we're also trying to access an endpoint that uses post so I'll be using post right 110 00:08:40,940 --> 00:08:41,750 here. 111 00:08:41,930 --> 00:08:49,850 Then we'll go ahead and provide the you are well that is forward slash users forward slash log in from 112 00:08:49,850 --> 00:08:50,380 here. 113 00:08:50,420 --> 00:08:53,660 We're gonna go ahead and send a cross good data. 114 00:08:53,690 --> 00:09:00,910 So in this case I want to provide the correct data the data that our 1 test user has been created with. 115 00:09:01,010 --> 00:09:01,840 So down below. 116 00:09:01,880 --> 00:09:03,780 Let's go ahead and do just that. 117 00:09:04,070 --> 00:09:08,330 I call send to provide the data with the object like we did up above. 118 00:09:08,540 --> 00:09:11,770 And I need to provide my email and password to log in. 119 00:09:12,020 --> 00:09:15,400 So I'll go ahead and set both of those up right here. 120 00:09:15,590 --> 00:09:21,590 Now the current approach would be to just type out the correct email and password inside of quotes. 121 00:09:21,590 --> 00:09:27,090 You could also just reference these ones on this object up above and that's what I'm going to do. 122 00:09:27,170 --> 00:09:31,090 So right here the email is not going to be a new String. 123 00:09:31,170 --> 00:09:37,550 We'll just grab the existing one from user one dot email and then the same thing for the password down 124 00:09:37,550 --> 00:09:38,480 below. 125 00:09:38,480 --> 00:09:41,180 User 1 dot password. 126 00:09:41,180 --> 00:09:47,300 Now that we've sent the correct data off we can expect something about the response and once again we're 127 00:09:47,300 --> 00:09:51,050 just going to stick to the status code right here. 128 00:09:51,050 --> 00:09:57,760 We will expect a 200 which is exactly what we should get now that we have this in place. 129 00:09:57,770 --> 00:10:01,830 Let's go ahead and fire off the request and see if it works as expected. 130 00:10:01,880 --> 00:10:07,790 Right here the test suite is rerunning and in a couple of seconds we should see all passing and that 131 00:10:07,790 --> 00:10:09,470 is exactly what we have. 132 00:10:09,920 --> 00:10:15,930 So in this case we are setting up test data for our test cases to interact with. 133 00:10:15,950 --> 00:10:21,680 Now you might say why not just create the user right here and then go ahead and try to log in. 134 00:10:21,680 --> 00:10:27,980 Well the problem is that a lot of our test cases are going to need a user to already exist things for 135 00:10:27,980 --> 00:10:33,700 logging in adding an avatar updating a profile creating a task and others. 136 00:10:33,740 --> 00:10:35,720 So it's best to just do it once. 137 00:10:35,750 --> 00:10:42,730 Up above to set up the database and then use it over and over again in your test cases. 138 00:10:42,740 --> 00:10:48,770 Now it's challenge time you're going to create a test case that makes sure when these credentials are 139 00:10:48,770 --> 00:10:51,410 bad things fail as expected. 140 00:10:51,410 --> 00:10:55,140 So right here here is what I'd like you to do test do log in. 141 00:10:55,130 --> 00:11:02,470 Failure you're going to create a new test case should not log in nonexistent user. 142 00:11:02,690 --> 00:11:08,330 Then you're going to send off the request with bad credentials and by bad credentials. 143 00:11:08,390 --> 00:11:14,690 I mean anything but what we have right up here then you're going to go ahead and expect the correct 144 00:11:14,690 --> 00:11:16,840 status code in the response. 145 00:11:16,850 --> 00:11:23,960 Feel free to refer to the application code or run the request and postman if you forget what that was. 146 00:11:23,960 --> 00:11:31,160 And finally test your work save the test suite and you should go from eight passing test cases to nine 147 00:11:31,460 --> 00:11:36,370 if the test case fails try to use the error message to figure out why. 148 00:11:36,380 --> 00:11:36,910 All right. 149 00:11:36,920 --> 00:11:41,270 Paul is the video knocked that out when you're done come back and click play 150 00:11:44,780 --> 00:11:45,650 how'd that go. 151 00:11:45,650 --> 00:11:48,340 Let's get to it by defining the new test case. 152 00:11:48,350 --> 00:11:50,590 I'll be calling test one more time. 153 00:11:50,600 --> 00:11:56,000 Right up above or down below the order of your tests is not particularly important. 154 00:11:56,000 --> 00:12:03,190 And then we'll go ahead and set up the name should not log in non existent user. 155 00:12:03,200 --> 00:12:04,090 Perfect. 156 00:12:04,190 --> 00:12:06,980 We'll set up our function as an async function. 157 00:12:06,980 --> 00:12:12,320 We'll be using async await and almost all of our test cases since pretty much everything we're doing 158 00:12:12,320 --> 00:12:14,030 is indeed asynchronous. 159 00:12:14,210 --> 00:12:18,140 And right here we're going to send off the request with bad credentials. 160 00:12:18,140 --> 00:12:20,870 So it starts off exactly like what we had above. 161 00:12:21,260 --> 00:12:29,300 I use a wait I call request passing an app then I call post with the same path forward slash users forward 162 00:12:29,300 --> 00:12:33,040 slash log in and I still want to send some data. 163 00:12:33,110 --> 00:12:36,750 I'm just going to send bad data so I could leave something off. 164 00:12:36,770 --> 00:12:40,430 Or I could just have a bad password as an example. 165 00:12:40,430 --> 00:12:45,240 So I'll set email equal to the following user 1 dot email. 166 00:12:45,290 --> 00:12:46,870 You did not need to do this. 167 00:12:46,870 --> 00:12:53,000 You could have used any sort of set of credentials and then I'll set password equal to something else. 168 00:12:53,000 --> 00:12:56,990 This is not my pass or anything else would have worked. 169 00:12:56,990 --> 00:13:01,910 Once again it's not important that you had the exact same data just something that's going to cause 170 00:13:01,910 --> 00:13:03,260 the failure. 171 00:13:03,320 --> 00:13:06,450 Next up we want to expect something about the status code. 172 00:13:06,470 --> 00:13:10,740 So we call the expect method that super test provides. 173 00:13:10,790 --> 00:13:16,520 And in this case if you looked at the code you'd remember it's a four hundred four when someone tries 174 00:13:16,520 --> 00:13:18,800 to log in but it fails. 175 00:13:18,800 --> 00:13:22,970 Now with this in place the only thing left to do is to test our work. 176 00:13:22,970 --> 00:13:24,280 That's step number four. 177 00:13:24,740 --> 00:13:27,610 So I'll go ahead and remove the challenge comments. 178 00:13:27,740 --> 00:13:31,600 I'm going to save the test suite then we'll see what we get down below. 179 00:13:31,610 --> 00:13:37,160 Hopefully we have 9 passing test cases and that is exactly what we're getting. 180 00:13:37,580 --> 00:13:38,710 So there we go. 181 00:13:38,720 --> 00:13:45,620 We now have test cases that create data and test cases that work with existing data and we were able 182 00:13:45,620 --> 00:13:53,000 to do all of that by using before each and Mongoose methods that we've already worked with earlier in 183 00:13:53,000 --> 00:13:54,050 the class. 184 00:13:54,050 --> 00:14:00,290 So when we use jest it gives us everything we need to set up our test suite without having to write 185 00:14:00,290 --> 00:14:02,330 a whole bunch of code ourselves. 186 00:14:02,330 --> 00:14:07,880 We're able to get up and running really quickly even with more advanced testing things like testing 187 00:14:08,090 --> 00:14:09,950 and express application. 188 00:14:09,950 --> 00:14:15,270 Here we have a test suite with three tests and just 36 lines of code. 189 00:14:15,290 --> 00:14:21,740 Now yes we're gonna add more but this is a great place to start in the next lesson we'll continue on 190 00:14:21,750 --> 00:14:23,060 writing test cases. 191 00:14:23,060 --> 00:14:24,580 I'm excited to get to it. 192 00:14:24,650 --> 00:14:26,980 So let's jump right into the next lesson.