1 00:00:00,150 --> 00:00:07,230 In this lesson we're going to focus on setting up our test suite to allow us to test our tasks as well. 2 00:00:07,230 --> 00:00:11,490 Now you might say what is there to set up everything's already configured. 3 00:00:11,490 --> 00:00:18,140 We could just add another test case right here for a task specific endpoint and you'd be right. 4 00:00:18,150 --> 00:00:23,220 The only problem with the current approach is that this is the user test suite. 5 00:00:23,250 --> 00:00:27,140 We want to create a separate test suite for tasks. 6 00:00:27,300 --> 00:00:34,320 We don't want all of the test cases for our entire project to live in the exact same file and as we 7 00:00:34,320 --> 00:00:37,330 do a bit of refactoring with our test suites. 8 00:00:37,470 --> 00:00:44,090 We'll also take some time to pull other code out of this user test suite such as the code up above. 9 00:00:44,190 --> 00:00:49,080 Responsible for resetting the database to a nice starting point. 10 00:00:49,080 --> 00:00:55,290 Now the reason we're doing that is right now this code is only running for the user test suite and we're 11 00:00:55,290 --> 00:00:59,380 going to want to run this code for our task tests as well. 12 00:00:59,430 --> 00:01:07,000 So let's go ahead and kick things off by first deleting math dot J.S. and math dot test dot J s two 13 00:01:07,020 --> 00:01:12,600 files we're no longer going to need all remove math dot J.S. from the source directory. 14 00:01:12,600 --> 00:01:16,320 Let's start there and then down below from the tests folder. 15 00:01:16,320 --> 00:01:23,100 We're going to remove math dot test dot J S and once both of those are gone our test suite should be 16 00:01:23,100 --> 00:01:25,470 back to its expected working state. 17 00:01:25,530 --> 00:01:32,520 And right here it is we have ten passing tests and they all exist in this one test suite user dot test 18 00:01:32,580 --> 00:01:37,450 dot J S now from here we're going to create a second test suite. 19 00:01:37,650 --> 00:01:42,120 That means we'll be adding a new file to our tests directory. 20 00:01:42,120 --> 00:01:50,010 Right here we're gonna call this one task dot test dot J S and we're actually going to kick things off 21 00:01:50,250 --> 00:01:56,720 by creating a test case that makes sure tasks can be created as we would expect. 22 00:01:56,820 --> 00:02:02,850 Now down below we can see our new test suite is failing and that's because there is no test cases inside 23 00:02:02,850 --> 00:02:03,570 of here. 24 00:02:03,630 --> 00:02:08,560 Once we actually create that first test things will start to work once again. 25 00:02:08,610 --> 00:02:12,390 Now to kick things off what we're gonna do is load in some stuff we'll need. 26 00:02:12,390 --> 00:02:20,190 I'll start by creating the concert request so I can load in Super Test by requiring it and once we have 27 00:02:20,190 --> 00:02:26,340 that in place I'm also going to load in the task model something we'll be using in this file. 28 00:02:26,340 --> 00:02:27,980 So right here const. 29 00:02:28,260 --> 00:02:32,650 Now we want to grab the task model defined over in source models. 30 00:02:32,670 --> 00:02:36,410 Task dot J S and down below it is our export. 31 00:02:36,690 --> 00:02:42,090 That means right here we can create a constant called task and require it. 32 00:02:42,240 --> 00:02:49,740 That is dot dot to get out of the tests directory forward slash source then forward slash models forward 33 00:02:49,740 --> 00:02:51,600 slash task. 34 00:02:51,660 --> 00:02:57,890 Now with this in place let's go ahead and create our very first test case for this one right here. 35 00:02:57,900 --> 00:03:00,630 We'll go ahead and call test. 36 00:03:00,630 --> 00:03:07,740 I'll provide a name for this one should create task for user and then we'll go ahead and set up the 37 00:03:07,740 --> 00:03:10,100 actual function down below. 38 00:03:10,140 --> 00:03:16,050 This is where we're gonna go through the process of authenticating as a given user and actually firing 39 00:03:16,050 --> 00:03:19,410 off the correct request to create that task. 40 00:03:19,410 --> 00:03:21,240 Now what's the problem here. 41 00:03:21,270 --> 00:03:27,840 The problem is that we don't have access to our test user that is created and manipulated in our other 42 00:03:27,840 --> 00:03:29,280 test suite. 43 00:03:29,280 --> 00:03:34,920 We want to make sure that both of these files can get access to that test data for the moment. 44 00:03:34,920 --> 00:03:37,140 We just have that one test user. 45 00:03:37,260 --> 00:03:44,190 But in order to test other task related endpoints we'll have to create some tasks in that database initially 46 00:03:44,190 --> 00:03:52,200 as well so we can test that things like deleting a task or fetching your tasks actually work as expected. 47 00:03:52,200 --> 00:03:55,890 So for the moment that means we can actually fill out this test case. 48 00:03:55,890 --> 00:04:02,230 We're gonna do a bit of refactoring related to how we set up that test data to get started I'm going 49 00:04:02,240 --> 00:04:04,020 to save this test suite. 50 00:04:04,020 --> 00:04:10,800 I'm gonna close down index an app dot J S and we're going to create a brand new file for setting up 51 00:04:10,820 --> 00:04:15,740 the database that's going to live in the fixtures directory in our tests folder. 52 00:04:15,870 --> 00:04:23,220 So inside of here I'll create a new file called DB dot J S and this is gonna contain all of the code 53 00:04:23,220 --> 00:04:26,560 necessary to set that database up. 54 00:04:26,560 --> 00:04:31,560 Now to kick things off what we want to do is make sure we create the same user data that we're currently 55 00:04:31,560 --> 00:04:39,330 creating over in the user test suite over in user dot test dot J S I'm gonna grab this code right here 56 00:04:39,330 --> 00:04:42,480 which is responsible for generating that user. 57 00:04:42,600 --> 00:04:43,920 I'm going to take it. 58 00:04:43,950 --> 00:04:49,820 Cut it out of its current location and bring it over to D.B. dot J S pasting it in. 59 00:04:49,890 --> 00:04:52,750 Now we do need some stuff that we don't have in this file. 60 00:04:52,770 --> 00:04:57,090 For example I need JWT and I also need a mongoose. 61 00:04:57,120 --> 00:05:00,180 So right here I'll go ahead and load both of those in. 62 00:05:00,240 --> 00:05:07,650 First stop all require Mongoose storing it on a mongoose variable then down below I'll load in the JWT 63 00:05:07,650 --> 00:05:17,470 library by using require with Jason Webb token now we can go ahead and move back to user test dot J 64 00:05:17,480 --> 00:05:22,520 S and remove both of those modules because they're no longer used inside of this file. 65 00:05:22,580 --> 00:05:26,330 All we need is request app and user. 66 00:05:26,330 --> 00:05:30,790 Now down below this makes some problems for what we already have right here. 67 00:05:30,860 --> 00:05:37,580 We are trying to create that user and then down below we use the two variables defined over here quite 68 00:05:37,580 --> 00:05:38,360 a bit. 69 00:05:38,360 --> 00:05:41,540 So how are we going to remedy this situation. 70 00:05:41,540 --> 00:05:46,760 Well what we're gonna do is first create a function and this function will get called to set up our 71 00:05:46,760 --> 00:05:47,430 data. 72 00:05:47,480 --> 00:05:51,440 So this will eventually be used with before each. 73 00:05:51,440 --> 00:05:52,250 Right here. 74 00:05:52,250 --> 00:05:54,330 Let's go ahead and start with that. 75 00:05:54,410 --> 00:06:00,440 I'm going to create a constant and I'll call this something like setup database or configure database 76 00:06:00,440 --> 00:06:04,380 or populate database or whatever name you want to choose. 77 00:06:04,430 --> 00:06:11,210 Now we're gonna set this up as an async function and for the moment the contents of this function is 78 00:06:11,210 --> 00:06:14,990 gonna be exactly the same as the contents of before each. 79 00:06:15,020 --> 00:06:16,100 Over here. 80 00:06:16,100 --> 00:06:22,340 That means I can just take these two lines I can cut them out of their current location and bring them 81 00:06:22,370 --> 00:06:26,690 over to D.B. J S and we're ready to go. 82 00:06:26,690 --> 00:06:33,020 Now the next thing we need to do is export some key values from this file so our test suites can actually 83 00:06:33,020 --> 00:06:40,610 use them right here module dot exports is gonna get set equal to an object and I'll be adding these 84 00:06:40,610 --> 00:06:43,370 three variables on as properties. 85 00:06:43,370 --> 00:06:47,840 I'll start with user 1 I.D. though the order isn't important then. 86 00:06:47,840 --> 00:06:53,240 User 1 followed by last but not least set up database. 87 00:06:53,240 --> 00:07:00,620 So at this point we have this new file and it's exporting these stuff that user dot test dot J.S. needs. 88 00:07:00,620 --> 00:07:06,530 So all we have to do to get the test suite back to its former working glory is to load those things 89 00:07:06,530 --> 00:07:08,360 in right over here. 90 00:07:08,390 --> 00:07:09,530 What does that mean for us. 91 00:07:09,530 --> 00:07:16,610 It means we're gonna grab a few things using some object D structuring I'm going to grab user 1 I.D. 92 00:07:17,630 --> 00:07:20,600 user 1 and setup database. 93 00:07:21,050 --> 00:07:24,070 And we're gonna grab those from the file we just created. 94 00:07:24,080 --> 00:07:30,230 So all require dot forward slash of fixtures forward slash D.B.. 95 00:07:30,350 --> 00:07:37,280 Now the two things user 1 I.D. and user 1 those are already being used by all of the code down below. 96 00:07:37,400 --> 00:07:40,530 There's no need to make any changes to any of that. 97 00:07:40,610 --> 00:07:46,790 All we have to do to get before each working once again is to remove the function we're currently passing 98 00:07:46,790 --> 00:07:51,000 in and pass in a setup database instead. 99 00:07:51,020 --> 00:07:56,270 Now once again the advantage of doing this is that I can simply use this one line in it. 100 00:07:56,260 --> 00:08:02,170 Task dot test dot J S to get access to all of these same features over there. 101 00:08:02,180 --> 00:08:09,200 Now the only other change we need to make is over in D.B. J S we just added that new code which uses 102 00:08:09,200 --> 00:08:11,890 the User model that's sitting right here. 103 00:08:11,990 --> 00:08:14,920 So we need to make sure we load that in as well. 104 00:08:14,930 --> 00:08:21,500 Right here const user equals will use require with the following path that is dot dot to get out of 105 00:08:21,500 --> 00:08:28,440 the fixtures directory but word slash dot dot to get out of tests than it is source models. 106 00:08:28,580 --> 00:08:29,600 User. 107 00:08:29,600 --> 00:08:36,320 Now if we go ahead and save D.B. dot J s and our user test suite I would expect us to be back to that 108 00:08:36,320 --> 00:08:38,010 working test suite. 109 00:08:38,120 --> 00:08:41,510 Right here it looks like everything is working as expected. 110 00:08:41,510 --> 00:08:44,820 We have all of our test cases passing which is great. 111 00:08:44,960 --> 00:08:50,540 Now we can actually take these same two lines exactly as they currently are. 112 00:08:50,720 --> 00:08:56,120 Copy them to the clipboard bring them over to task dot test dot J S. 113 00:08:56,150 --> 00:08:57,480 Paste them in. 114 00:08:57,650 --> 00:09:04,390 And now this file has access to all of that same test data now from a user dot test done J.S.. 115 00:09:04,400 --> 00:09:08,690 We also want to grab this line right here that imports the app. 116 00:09:08,690 --> 00:09:15,350 We could manually type that out again or we could just copy and paste it over to the new test suite. 117 00:09:15,350 --> 00:09:19,180 And with this in place our test suite is now good to go. 118 00:09:19,190 --> 00:09:25,510 It's currently not doing much testing but everything is set up to at least work in the long term. 119 00:09:25,520 --> 00:09:32,330 Now when we have multiple test suites each interacting with the database there's a chance for conflict 120 00:09:32,360 --> 00:09:39,620 because of how just runs things just will try to run our tests in separate test suites at the same time. 121 00:09:39,950 --> 00:09:45,650 And since we're depending on certain changes to the database we can get into situations where tests 122 00:09:45,680 --> 00:09:53,900 fail incorrectly for example this sign up test might fail because in between it creating the user and 123 00:09:53,930 --> 00:10:00,830 it finding the user a test in a different file messes up the user may be doing something like resetting 124 00:10:00,830 --> 00:10:02,390 the database. 125 00:10:02,390 --> 00:10:07,880 So it's important to write real test cases but it's also important to recognize that our test cases 126 00:10:07,880 --> 00:10:10,780 can interfere with each other if we let them. 127 00:10:10,850 --> 00:10:14,160 We can fix this with a simple change in package Dot. 128 00:10:14,170 --> 00:10:21,800 Jason All we need to do is add a new option onto the just command along side of the watch option. 129 00:10:21,800 --> 00:10:28,500 This is hyphen hyphen run in band that is capital I capital B. 130 00:10:28,550 --> 00:10:35,330 This is going to run your tests in series making sure that there's no overlap or chance of conflict. 131 00:10:35,330 --> 00:10:42,680 Now when we change the command we will need to use control C to manually shut down just then we can 132 00:10:42,680 --> 00:10:46,900 start it up once again using up and enter and we're good to go. 133 00:10:47,030 --> 00:10:54,500 Now at this point we can actually write a test case ensuring that a given user can indeed create a task 134 00:10:54,530 --> 00:10:57,530 and that is exactly what we're going to do right now. 135 00:10:57,530 --> 00:11:05,030 Down below I can see the test suite did restart and we still have eleven passing tests which is excellent. 136 00:11:05,030 --> 00:11:08,600 Right here we'll kick things off what they call to request. 137 00:11:08,690 --> 00:11:15,080 Let's start by creating a constant called response and I'm gonna go ahead and use a wait with my call 138 00:11:15,110 --> 00:11:22,370 to request will pass in our Express application and in this case I'm trying to make an H TTP a post 139 00:11:22,370 --> 00:11:27,890 request to the following endpoint that is a forward slash tasks. 140 00:11:27,890 --> 00:11:34,250 Now from here we'll start by going ahead and setting up the authentication header so that is dot set 141 00:11:34,310 --> 00:11:37,130 to call the set method right here. 142 00:11:37,130 --> 00:11:41,840 Authorization providing the value as well. 143 00:11:41,850 --> 00:11:44,140 Bearer followed by that token. 144 00:11:44,160 --> 00:11:49,600 Now we still have access to this because of how we set up the DB fixtures file. 145 00:11:49,710 --> 00:11:52,680 We have user 1 I.D. and user 1. 146 00:11:52,830 --> 00:11:56,310 And right here like we've done before it is user 1. 147 00:11:56,490 --> 00:12:02,660 Dot tokens grabbing the first one and getting the JWT off of that object. 148 00:12:02,670 --> 00:12:08,620 Now with this in place the next thing we're going to do is use send to send the necessary data. 149 00:12:08,670 --> 00:12:11,330 So I'll call send providing an object. 150 00:12:11,340 --> 00:12:16,660 Now we know for tasks the only thing we need to provide is a description. 151 00:12:16,680 --> 00:12:24,320 So right here I'll do just that description equals I can use whatever I'd like. 152 00:12:24,330 --> 00:12:32,550 I'll use something like from my test and then down below we can go ahead and use expect to expect the 153 00:12:32,550 --> 00:12:35,800 following status code a 2 0 1. 154 00:12:35,820 --> 00:12:41,610 Now we'll go ahead and add more to this test case in just a moment before we run it though let's make 155 00:12:41,610 --> 00:12:47,470 sure to mark this as an async function since we are using a weight right inside. 156 00:12:47,490 --> 00:12:51,930 Now let's run the test case and make sure it's at least working in its current state. 157 00:12:52,260 --> 00:12:59,390 If this is at least working we'll expand on it adding other assertions and down below it is indeed functional. 158 00:12:59,400 --> 00:13:06,540 Now what we're gonna do is usually task a model to find that new task by its I.D. and make sure it was 159 00:13:06,540 --> 00:13:10,620 actually saved to the database and that it looks correct. 160 00:13:10,620 --> 00:13:14,100 So we have the I.D. on the response down below. 161 00:13:14,370 --> 00:13:23,190 I'll create a new constant lower case t task I'll use a weight with the following task dot find by I.D. 162 00:13:23,610 --> 00:13:32,210 like we had done plenty of times for user and we have response dot body dot underscore I.D.. 163 00:13:32,370 --> 00:13:37,690 Now with this in place let's go ahead and assert that we actually got something back. 164 00:13:37,770 --> 00:13:39,030 I'll use expect for that. 165 00:13:39,510 --> 00:13:41,910 And like we had done before with users. 166 00:13:41,940 --> 00:13:45,290 I'll expect that the task is not null. 167 00:13:45,300 --> 00:13:50,450 I'll start with not then the assertion to be no. 168 00:13:50,520 --> 00:13:51,620 Excellent. 169 00:13:51,630 --> 00:13:57,480 Now we can go ahead and run things in its current state although we could expand on our assertions even 170 00:13:57,480 --> 00:13:58,380 further. 171 00:13:58,380 --> 00:14:01,140 Now that we have their response and we have the task. 172 00:14:01,140 --> 00:14:07,270 For example I could check that the descriptions line up I could check that this user is the owner. 173 00:14:07,350 --> 00:14:12,050 I could check if the completed value was correctly set to false. 174 00:14:12,060 --> 00:14:16,920 I could check all sorts of things depending on what I'm worried about failing right here. 175 00:14:16,950 --> 00:14:23,970 Let's go ahead and just make sure that these completed property on this new task is indeed false. 176 00:14:23,970 --> 00:14:30,690 So right here what I'll do is expect something about the task we found in the database. 177 00:14:30,810 --> 00:14:31,970 I'm going to expect. 178 00:14:31,980 --> 00:14:35,870 Task dot completed to be false. 179 00:14:35,880 --> 00:14:41,490 So right here I can use to be or to equal and I can pass false in. 180 00:14:41,760 --> 00:14:47,850 Now once again if I save things the test should still be passing as that is the default value set up 181 00:14:47,850 --> 00:14:52,830 for that specific field and that is exactly what we end up getting down below. 182 00:14:52,860 --> 00:14:59,010 That's where I'd like to stop for this one in the next lesson we'll actually set up our setup database 183 00:14:59,070 --> 00:15:05,760 function and to wipe existing tasks and create some test ones as well so we can write more interesting 184 00:15:05,760 --> 00:15:06,900 test cases. 185 00:15:06,900 --> 00:15:08,350 I'm excited to get to that. 186 00:15:08,430 --> 00:15:09,690 Let's jump right in.