1 00:00:00,840 --> 00:00:03,670 It's now time for us to start to write out an actual test. 2 00:00:03,690 --> 00:00:08,940 So in this video we're going to write a very simple test around our sign up root handler because we 3 00:00:08,940 --> 00:00:12,120 are trying to write a test around these sign up root handler. 4 00:00:12,120 --> 00:00:18,030 We're going to create a folder inside of the roots directory so new folder called underscore underscore 5 00:00:18,090 --> 00:00:23,920 test underscore underscore when we make use of jests we follow the convention that if we are trying 6 00:00:23,920 --> 00:00:28,870 to test a given file we make a folder inside the same directory called underscore underscore test like 7 00:00:28,870 --> 00:00:34,330 that right there and then inside that directory we'll make a new file that has the name of the file 8 00:00:34,330 --> 00:00:35,710 that we are trying to test. 9 00:00:35,710 --> 00:00:41,980 So sign up then dot test dot to yes it's going to follow the same convention for testing everything 10 00:00:42,280 --> 00:00:44,340 whenever we want to test something inside of a file. 11 00:00:44,500 --> 00:00:48,910 Make a folder in the same directory call it underscore underscore test underscore underscore make a 12 00:00:48,910 --> 00:00:52,970 file inside there with the same name as the original file but with test dot. 13 00:00:53,000 --> 00:01:00,060 Yes at the very end then inside this file we're gonna write out like I said a very simple test and figure 14 00:01:00,060 --> 00:01:01,680 out how to run it. 15 00:01:01,710 --> 00:01:08,520 So at the very top we're going to first import requests from two protests so remember Super Test is 16 00:01:08,520 --> 00:01:12,710 a thing that's going to allow us to fake a request to the Express application. 17 00:01:12,760 --> 00:01:18,640 Well then add in an import statement to get the app that we had declared inside of the abductees file. 18 00:01:18,710 --> 00:01:22,300 So we will get app from up directory up 19 00:01:25,860 --> 00:01:28,260 and then we can write start to write out a test statement. 20 00:01:28,350 --> 00:01:33,090 So we will write out it and then a description for this test with this very first test that we are going 21 00:01:33,090 --> 00:01:33,690 to put together. 22 00:01:33,690 --> 00:01:39,780 We're just gonna make sure that we can send in a post request to this sign up or out right here with 23 00:01:39,780 --> 00:01:43,860 a valid email and password and get some kind of response. 24 00:01:43,890 --> 00:01:49,190 So I just wanted to send in a post request to that route with an email a password in the body. 25 00:01:49,350 --> 00:01:53,330 And we're going to make sure that we get a response that has a status of 2 a 1. 26 00:01:53,370 --> 00:01:58,470 That's all we're gonna do for this very first test so inside the string right here I'm going to put 27 00:01:58,470 --> 00:02:07,940 in a description of it returns a to a one on successful sign up then as a second argument we'll put 28 00:02:07,940 --> 00:02:13,160 in an async function all the different tests that we gonna put together with super tests are going to 29 00:02:13,160 --> 00:02:15,050 follow a very similar structure. 30 00:02:15,050 --> 00:02:20,130 We are going to return request. 31 00:02:20,320 --> 00:02:26,220 We're going to pass in app and then we're going to chain on a number of different methods to it. 32 00:02:26,230 --> 00:02:31,680 So the first you're going to chain on is to tell this thing where we want to make the request to and 33 00:02:31,680 --> 00:02:33,960 what method we want to use for the request. 34 00:02:33,960 --> 00:02:41,160 So in our case we want to make a post request and we want to make the post request to a root of API 35 00:02:41,220 --> 00:02:42,710 users sign up. 36 00:02:42,870 --> 00:02:51,710 So we'll do flash API flash users flash sign up we will then send off the request. 37 00:02:52,100 --> 00:02:56,640 And when we do so we're going to specify some information to include in the body of the request. 38 00:02:56,660 --> 00:03:01,040 So this right here is going to be provided as the body as I mentioned for this first test we want to 39 00:03:01,040 --> 00:03:05,500 make sure we send along a valid email and develop password to sign up with. 40 00:03:05,530 --> 00:03:11,990 So I going to specify an email of test at test dot com and a password of 41 00:03:16,040 --> 00:03:18,030 so this is going to actually send the request off. 42 00:03:18,140 --> 00:03:23,360 If we then want to make some assertions about the response we'll chain them onto the very end. 43 00:03:23,360 --> 00:03:28,820 So for example we can write in the dot expect and then put in the status code that we expect to receive 44 00:03:28,850 --> 00:03:30,140 in the response. 45 00:03:30,140 --> 00:03:34,220 In this case we expect to get a tax code of two a one back and that's it. 46 00:03:34,230 --> 00:03:38,330 That's everything we need to write out for a very basic and straightforward test. 47 00:03:38,350 --> 00:03:42,370 Now you will notice that I marked this function as async even though we did not use the awake keyword 48 00:03:42,400 --> 00:03:43,530 inside of here. 49 00:03:43,570 --> 00:03:45,150 I'm just doing that out of habit. 50 00:03:45,190 --> 00:03:50,290 We will eventually attempt to make multiple requests inside of a single test when we do we're going 51 00:03:50,290 --> 00:03:52,300 to have to start to use the await keyword. 52 00:03:52,300 --> 00:03:57,160 And so I found that it's easier to just add on async from the get go and not have to worry about adding 53 00:03:57,160 --> 00:04:01,600 on adding it on it's something in the future or forgetting to add it on and being given some kind of 54 00:04:01,600 --> 00:04:09,260 error will then say this file and to run it we will go over to our terminal and run NPM run test inside 55 00:04:09,290 --> 00:04:17,290 or off directory so I'll change into off and then NPM run test and just you know the first time we run 56 00:04:17,290 --> 00:04:23,080 this we are going to probably see an error which is OK but let's run it anyways. 57 00:04:23,320 --> 00:04:26,810 So it looks like the test is running. 58 00:04:27,060 --> 00:04:32,620 Now the very first test we run is going to take a little bit to actually start up and it looks like 59 00:04:32,800 --> 00:04:34,720 we'll come back to why it took so long just a moment. 60 00:04:34,720 --> 00:04:38,520 What's more relevant is that we got a 400 bad request back. 61 00:04:38,890 --> 00:04:40,510 So why is that. 62 00:04:40,510 --> 00:04:45,940 Well you might recall if we take a look at the implementation of our sign up root handler here sign 63 00:04:45,940 --> 00:04:53,730 up dot to yes right here instead of here at some point in time we attempt to create a new date on a 64 00:04:53,730 --> 00:04:57,600 Web token when we attempt to create that Jason web token. 65 00:04:57,610 --> 00:05:03,940 We try to access environment variable of JWT underscore key we had previously been making a check to 66 00:05:03,940 --> 00:05:07,720 ensure that this thing was defined inside of our index not t as file. 67 00:05:07,720 --> 00:05:09,970 But right now we have split up our index not. 68 00:05:09,970 --> 00:05:14,020 Yes an apt t as files into two separate files. 69 00:05:14,020 --> 00:05:18,550 So we are no longer making sure that this enviroment variable exists in this matter fact it doesn't 70 00:05:18,550 --> 00:05:21,860 exist right now when we are running all of our code inside this test environment. 71 00:05:22,150 --> 00:05:27,640 This environment variable only gets defined when we run our code inside of a pod as remember we had 72 00:05:27,730 --> 00:05:33,970 specified for this enviroment variable to be defined inside of our deployment config file so to get 73 00:05:33,970 --> 00:05:37,930 this error to go away all we need to do is to make sure that we define this enviroment variable at some 74 00:05:37,930 --> 00:05:39,820 point in time. 75 00:05:39,880 --> 00:05:44,300 There are many different ways to define a environment variable in a kind of test environment. 76 00:05:44,350 --> 00:05:48,250 It's going to take just a very simple very direct way it's not necessarily the best way of doing it 77 00:05:48,280 --> 00:05:51,000 but it's going to get that error to go away right away. 78 00:05:51,050 --> 00:05:56,260 I got to go back to the setup dot t s file I'm going to find you before all statements and inside there 79 00:05:56,340 --> 00:06:02,390 we're just going to directly set the enviroment variable with a process EMV data to underscore key and 80 00:06:02,410 --> 00:06:05,020 we'll set it equal to whatever string. 81 00:06:05,020 --> 00:06:08,540 Again not the best way of handling this but it's going to work for right now. 82 00:06:08,830 --> 00:06:13,480 I'm gonna say this I should be able to then go back over to my terminal and see that my test is now 83 00:06:13,480 --> 00:06:15,460 passing awesome. 84 00:06:15,460 --> 00:06:19,840 So we've gone through the process of writing out one full test we're going to start to write out a lot 85 00:06:19,840 --> 00:06:21,270 more tests very quickly. 86 00:06:21,270 --> 00:06:23,590 So we'll take a quick pause right here and continue in just a moment.