1 00:00:00,180 --> 00:00:06,210 In this lesson you're going to learn how to write test cases that interact with our Express REST API 2 00:00:06,270 --> 00:00:10,970 so our test cases will actually make requests to the various endpoints. 3 00:00:11,010 --> 00:00:14,740 Then we can write assertions to make sure they worked as expected. 4 00:00:15,030 --> 00:00:21,030 So if I want to write a test case that logs in successfully I could expect I get a two hundred back 5 00:00:21,030 --> 00:00:24,010 with an authentication token in the response. 6 00:00:24,060 --> 00:00:30,880 I could also write a test case that makes sure when the credentials are invalid I don't get logged in. 7 00:00:31,050 --> 00:00:34,430 So we'll learn how to do that in a moment before we can get to that. 8 00:00:34,560 --> 00:00:40,560 We have to remember that our test cases they're about to start loading in files in the source directory 9 00:00:40,800 --> 00:00:47,820 related to the task manager app now currently that would be a big problem because those files depend 10 00:00:47,820 --> 00:00:55,740 on certain environment variables existing and they don't we only load in that Dev dot EMV config file 11 00:00:56,010 --> 00:00:58,740 when we run the dev script in package. 12 00:00:58,800 --> 00:00:59,550 Jason. 13 00:00:59,640 --> 00:01:05,490 Currently we're not loading in any configuration for the test script so we need to make sure that we 14 00:01:05,490 --> 00:01:10,470 do supply a set of environment variables to jest when it runs. 15 00:01:10,500 --> 00:01:17,910 So our test cases can actually start up the task manager server and to make requests to it. 16 00:01:17,910 --> 00:01:21,960 Now to do that what we're going to do is create a separate environment. 17 00:01:21,960 --> 00:01:29,010 We're going to create a new one called test so we'll create the following file test dot EMV to set up 18 00:01:29,070 --> 00:01:30,780 those environment variables. 19 00:01:30,780 --> 00:01:32,820 Now what needs to get defined in here. 20 00:01:32,850 --> 00:01:38,840 Well everything that's defined in dev dot EMV so Port send grid API key. 21 00:01:38,850 --> 00:01:43,320 The Mongo D.B. U.R.L. and our JWT secret. 22 00:01:43,320 --> 00:01:47,150 Now you might be wondering why create a new environment at all. 23 00:01:47,160 --> 00:01:51,280 Why not just load in dev dot EMV for our test cases. 24 00:01:51,360 --> 00:01:53,860 And the answer has to do with the database. 25 00:01:53,940 --> 00:01:56,100 So the value for Mongo DB. 26 00:01:56,150 --> 00:02:03,280 You are all currently the dev environment is interacting with this database on our local Mongo DB server. 27 00:02:03,360 --> 00:02:09,660 We don't want the test environment to also use that because the test environment it's going to populate 28 00:02:09,690 --> 00:02:11,700 the database with seed data. 29 00:02:11,850 --> 00:02:18,480 So dummy data that the test cases can use to do things like test log in and it's gonna run test cases 30 00:02:18,540 --> 00:02:19,950 over and over again. 31 00:02:20,010 --> 00:02:26,010 So it'll be wiping the database setting up that C data and running a test case and if it's doing that 32 00:02:26,010 --> 00:02:32,940 for our dev database it's gonna make it hard for us to actually use the API in postman as we're setting 33 00:02:32,940 --> 00:02:34,200 up new routes. 34 00:02:34,200 --> 00:02:42,510 So all we're gonna do is take the dev dot EMV file copy it over to test dot EMV and add onto the end 35 00:02:42,540 --> 00:02:47,340 of our Mongo DB connection string Python test. 36 00:02:47,340 --> 00:02:54,150 So now our test environment is using a one database on our local Mongo DB server and the dev environment 37 00:02:54,180 --> 00:02:59,190 is using another making sure they're not interacting with each other's data which is going to cause 38 00:02:59,190 --> 00:03:00,570 problems. 39 00:03:00,570 --> 00:03:06,990 Now all we need to do is make sure that when we run that test script the config file is loaded so we'll 40 00:03:06,990 --> 00:03:14,730 be adding EMV BMD to the test command just like we did for the dev command up above and the path to 41 00:03:14,730 --> 00:03:22,260 the EMV file dot forward slash config forward slash test dot e and the perfect. 42 00:03:22,260 --> 00:03:28,380 So now we have a modified version of our test script that is going to load in the correct environment 43 00:03:28,470 --> 00:03:35,610 variables and that means our test suite is now ready to actually start the process of starting up the 44 00:03:35,610 --> 00:03:38,630 task manager app and interacting with it. 45 00:03:38,730 --> 00:03:44,160 Now before we get to that and there is one more thing we're going to configure related to jest and that's 46 00:03:44,160 --> 00:03:46,890 gonna happen right here in package dot Jason. 47 00:03:47,010 --> 00:03:51,270 What we're gonna do is add a new root property and package done Jason. 48 00:03:51,300 --> 00:03:53,760 So right here we can add it anywhere we like. 49 00:03:53,760 --> 00:03:59,640 I'll add it just below scripts for example and this one is gonna be called Gest and this is going to 50 00:03:59,640 --> 00:04:01,850 store our just configuration. 51 00:04:01,860 --> 00:04:05,430 It's an object itself and we'll put a comma at the end. 52 00:04:05,430 --> 00:04:11,130 And right here we can provide the various things we want to configure now for our purposes working with 53 00:04:11,130 --> 00:04:16,890 node j s the only thing we really need to set is what's called test environment. 54 00:04:16,890 --> 00:04:17,660 This is Jason. 55 00:04:17,660 --> 00:04:22,620 So make sure to put your property names and values in double quotes right here. 56 00:04:22,620 --> 00:04:29,490 That is test capital E environment and the value for this one is note. 57 00:04:29,690 --> 00:04:34,710 Now if you want to learn more about how you can configure just the documentation is going to guide you 58 00:04:34,710 --> 00:04:35,720 through that. 59 00:04:35,740 --> 00:04:42,730 So over here in the sidebar if we scroll down to API docs once again we have configuring jest. 60 00:04:42,840 --> 00:04:45,410 This talks about exactly what we just did. 61 00:04:45,480 --> 00:04:50,370 Setting up a just property in your package dot Jason File to configure it. 62 00:04:50,370 --> 00:04:54,460 Although there are other ways creating separate files which you could also use. 63 00:04:54,720 --> 00:04:59,220 And on the right hand side bar they have all of the different things you can configure and there's a 64 00:04:59,220 --> 00:05:00,390 lot. 65 00:05:00,390 --> 00:05:04,610 Now the only thing we need is test environment which we already used. 66 00:05:04,950 --> 00:05:10,500 And if we pull that up by clicking it we'll see the default value is J.S. Dom. 67 00:05:10,500 --> 00:05:15,070 This is a javascript environment similar to what you'd see in the browser. 68 00:05:15,300 --> 00:05:22,230 So by default Gest is assuming your testing browser based javascript though it also supports node as 69 00:05:22,230 --> 00:05:23,710 we can see down below. 70 00:05:23,790 --> 00:05:29,530 We set the value to node to use a node environment for our test cases instead. 71 00:05:29,850 --> 00:05:35,970 And this is definitely something we want to do especially as we start to load in all of those node scripts 72 00:05:35,970 --> 00:05:38,690 inside of the source directory. 73 00:05:38,730 --> 00:05:45,120 So now that we have this in place just is all set up and it's ready to actually go ahead and start up 74 00:05:45,180 --> 00:05:51,370 our app and connect to it making queries to the API from our test cases. 75 00:05:51,480 --> 00:05:53,130 That's what we'll do in the next lesson. 76 00:05:53,130 --> 00:05:54,530 So let's jump right in.