1 00:00:01,630 --> 00:00:04,980 In this video we're going to take a look at some the different libraries and processes that we're going 2 00:00:04,970 --> 00:00:07,410 to go through to implement all this testing stuff. 3 00:00:07,480 --> 00:00:13,060 Now as a reminder we're going to really be focusing on this testing goal number one or hour off service. 4 00:00:13,190 --> 00:00:17,710 So we're going to write out some tests that are going to try to send a request off to our service and 5 00:00:17,710 --> 00:00:23,250 then make some assertion about the response or make sure that some data was written into Mongo DB. 6 00:00:23,290 --> 00:00:28,540 We are not going to be worried right now about writing some tests or on models or event admitting and 7 00:00:28,540 --> 00:00:29,650 receiving. 8 00:00:29,690 --> 00:00:29,950 OK. 9 00:00:29,980 --> 00:00:34,650 So with all that in mind let's take a look at how we're going to wire up all this testing stuff. 10 00:00:34,810 --> 00:00:38,050 We're going to add in a new script to our package not just on file. 11 00:00:38,050 --> 00:00:43,580 Whenever we run the command and run test we are going to start up a test runner called jest. 12 00:00:43,780 --> 00:00:49,570 So just as the library that we're going to use to actually execute tests inside of our project then 13 00:00:49,660 --> 00:00:55,220 on the right hand side this is pretty much what we're going to have just do the wind just starts running. 14 00:00:55,270 --> 00:01:00,690 We're going to tell just to do the following things you see over here first off we're gonna start up 15 00:01:00,750 --> 00:01:02,980 in in memory copy of Mongo DB. 16 00:01:03,540 --> 00:01:06,720 So this is gonna be a copy of Mongo that's running in memory. 17 00:01:06,720 --> 00:01:11,250 In other words it will not be you do not have to install Margo directly onto your machine. 18 00:01:11,250 --> 00:01:14,930 Now even if you already have Mongo installed on your local machine we are still going to use this in 19 00:01:14,930 --> 00:01:15,970 memory copy. 20 00:01:15,990 --> 00:01:20,250 There's a couple of reasons for that and we'll go through them as we start to wire up the in-memory 21 00:01:20,250 --> 00:01:28,010 copy after that we're gonna start up our Express app and then we'll use a library called super test 22 00:01:28,160 --> 00:01:33,090 to make fake requests to our Express application and then after that we will run some assertions to 23 00:01:33,090 --> 00:01:36,000 make sure that the requested exactly what we expected it to do. 24 00:01:36,060 --> 00:01:40,860 So maybe respond respond to with some particular data or wrote some particular data to Mongo DB. 25 00:01:42,260 --> 00:01:44,510 Now I want to focus on this third step right here. 26 00:01:44,540 --> 00:01:50,630 The Super Test library in order to get Super Test working well we're going to do a little refactor to 27 00:01:50,630 --> 00:01:51,640 our project. 28 00:01:51,710 --> 00:01:56,630 So I want to show you some documentation around Super Test and help you understand why we're gonna do 29 00:01:56,630 --> 00:01:59,670 this refactor inside of a new browser tab. 30 00:01:59,700 --> 00:02:05,530 I'm going to open up the documentation for Super Test by going to NPM J ask.com and then doing a search 31 00:02:05,530 --> 00:02:06,850 for super test 32 00:02:09,770 --> 00:02:12,220 and here it is right here. 33 00:02:12,390 --> 00:02:17,570 I'm going to scroll down just a little bit and find the example section inside this example section. 34 00:02:17,620 --> 00:02:19,280 They've got a little bit of code right here. 35 00:02:19,480 --> 00:02:23,300 Now I took a screenshot of this code and put a diagram together around it. 36 00:02:23,380 --> 00:02:27,070 So I'm gonna go back over and take a look at that screenshot. 37 00:02:27,120 --> 00:02:31,830 So here's that little bit of code that's inside the read me and there's just something very important 38 00:02:31,830 --> 00:02:37,870 inside there that I want to point out right about in this section right here. 39 00:02:37,900 --> 00:02:41,400 They have some code to create an express server. 40 00:02:41,530 --> 00:02:45,050 Now at present you and I are doing that inside of our index not just file. 41 00:02:45,160 --> 00:02:50,350 Yes technically we create root handlers inside those other files and then we associate them all together 42 00:02:50,350 --> 00:02:56,290 inside the index such as file but we actually create our Express app like that express thing right there 43 00:02:56,470 --> 00:02:58,870 inside the index such as file. 44 00:02:58,870 --> 00:03:00,600 Why is that relevant. 45 00:03:00,610 --> 00:03:06,010 Well down here is how we use super tests to actually test our Express server. 46 00:03:06,100 --> 00:03:12,640 We have to get access to the Express application variable that is defined inside that index such as 47 00:03:12,640 --> 00:03:13,640 file. 48 00:03:13,900 --> 00:03:19,060 We're going to pass that express app into this request function that is coming from the Super Test library 49 00:03:19,770 --> 00:03:26,360 and then we can make a request and write out some expectations around it the whole thing I want to focus 50 00:03:26,360 --> 00:03:31,190 on here the whole big point I'm trying to make is that we are creating our Express application inside 51 00:03:31,190 --> 00:03:32,840 the index such as file. 52 00:03:32,840 --> 00:03:37,430 So at some point in time we're going to have to write out some code that's going to look a little bit 53 00:03:37,430 --> 00:03:38,320 like this. 54 00:03:38,360 --> 00:03:44,780 We're going to have to try to require the Express application from that index such as file over to some 55 00:03:44,840 --> 00:03:52,460 testing file just so we can get X access to that app variable there's a little problem with that inside 56 00:03:52,460 --> 00:03:53,760 of our index such as file. 57 00:03:53,780 --> 00:03:55,790 We have a lot of startup code. 58 00:03:55,790 --> 00:03:59,720 We have some code to tell the Express app to start listening on some given port. 59 00:03:59,720 --> 00:04:05,630 We also have some hard coded stuff to say tell mango or Mongoose to connect to our Mongo DB database 60 00:04:05,690 --> 00:04:08,020 on some particular U.R.L.. 61 00:04:08,180 --> 00:04:14,450 So there are some very particular very hard coded startup logic inside of index dot J.S. but most in 62 00:04:14,450 --> 00:04:19,220 particular I want to focus on the fact that inside of index such s we automatically start listening 63 00:04:19,310 --> 00:04:21,770 on port three thousand inside there. 64 00:04:21,770 --> 00:04:23,210 Why is that relevant. 65 00:04:23,210 --> 00:04:28,430 Well at some point time you and I might want to be able to run multiple different tests on the same 66 00:04:28,430 --> 00:04:29,330 machine. 67 00:04:29,330 --> 00:04:36,360 In other words we might want to run some tests around our off service at the same time that we are running 68 00:04:36,360 --> 00:04:42,090 some tests around our order service the OSS service and the order service might both be trying to listen 69 00:04:42,140 --> 00:04:43,890 on port three thousand. 70 00:04:44,070 --> 00:04:50,760 So if we try to require in our Express app from that index not just file and both the off service and 71 00:04:50,760 --> 00:04:55,320 the ticket service or whatever else are listening on the same port we're gonna very quickly end up with 72 00:04:55,320 --> 00:05:00,120 an error and we will not be able to run tests from different services on the same machine at the same 73 00:05:00,120 --> 00:05:07,310 time just because they are both listening on the same port so to get around this and to allow us to 74 00:05:07,310 --> 00:05:10,550 run tests from multiple services on the same machine at the same time. 75 00:05:10,640 --> 00:05:16,340 We're going to rely upon some default behavior or some nice functionality inside a Super Test to back 76 00:05:16,340 --> 00:05:18,350 over here at the example section. 77 00:05:18,350 --> 00:05:22,910 You'll notice that it says if this server is not listening for connections then it will automatically 78 00:05:22,910 --> 00:05:25,670 start to listen on and ephemeral ports. 79 00:05:25,710 --> 00:05:30,590 A federal port right here just means that super test is going to pick a random available port on your 80 00:05:30,590 --> 00:05:34,440 computer and have the express app start to listen on that port. 81 00:05:34,640 --> 00:05:39,560 And so that feature right there will allow us to run tests from multiple different services at the same 82 00:05:39,560 --> 00:05:46,350 time and not have to worry about this port issue however if we want to take use of that functionality 83 00:05:46,380 --> 00:05:51,150 that means that inside of our index not just file we can not have any code that's going to have the 84 00:05:51,150 --> 00:05:54,900 express app automatically start to listen on port three thousand. 85 00:05:54,990 --> 00:05:56,700 So how are we going to fix this all up. 86 00:05:56,700 --> 00:05:57,690 Well here's what we're gonna do. 87 00:05:57,690 --> 00:06:02,850 Here's the refactor we're going to go through in the next video we're going to start to split out our 88 00:06:02,850 --> 00:06:06,580 index not just file into two different files. 89 00:06:06,720 --> 00:06:12,780 We're gonna create a new file called app J.S. inside about us we're going to create our Express application 90 00:06:12,990 --> 00:06:18,990 and export it the Express app that we create inside this file is not going to be listening on any port. 91 00:06:18,990 --> 00:06:26,160 So in other words we are not going to have any code inside of app J.S. that looks like this right here. 92 00:06:26,980 --> 00:06:31,550 We are not going to have anything inside this new app has filed it's going to have our app start listening 93 00:06:31,550 --> 00:06:33,920 on port three thousand or anything else. 94 00:06:34,010 --> 00:06:38,780 Instead we're just going to export the app after we have wired up all the appropriate middleware as 95 00:06:38,780 --> 00:06:45,090 root handlers and so on we're then going to export that thing into the test file where it can then be 96 00:06:45,090 --> 00:06:47,910 safely used by Super Test. 97 00:06:47,950 --> 00:06:54,000 We will also import it into a new index J.S. file and inside of index not J.S. is where we're going 98 00:06:54,000 --> 00:06:59,790 to have some actual code to start up our app and start listening on port three thousand so whenever 99 00:06:59,790 --> 00:07:03,400 we want to actually run our application inside of a dev or a production environment. 100 00:07:03,420 --> 00:07:07,020 In other words we never want to really start up our server for real. 101 00:07:07,020 --> 00:07:11,640 We're still going to execute the index not just file and nothing's just going to pull in the app that 102 00:07:11,640 --> 00:07:16,620 was declared over here but we're going to have the separate file that exports just the app itself so 103 00:07:16,620 --> 00:07:20,100 we can still use it safely inside that test file. 104 00:07:20,250 --> 00:07:21,170 OK. 105 00:07:21,510 --> 00:07:22,440 So that's pretty much it. 106 00:07:22,440 --> 00:07:26,960 I just want to help you understand why we are doing this refactor now at this point you might be saying 107 00:07:26,970 --> 00:07:31,920 Steven this seems like kind of a big refactor just to get the Super Test library working. 108 00:07:31,920 --> 00:07:36,780 Well it turns out that there are actually some huge benefits from creating our Express app inside of 109 00:07:36,780 --> 00:07:42,360 its own file and then doing some actual startup logic inside of this other file over here and you're 110 00:07:42,360 --> 00:07:47,670 gonna see some less benefits later on as we start to work on some of our other services. 111 00:07:47,670 --> 00:07:48,880 All right so a quick pause right here. 112 00:07:48,930 --> 00:07:50,970 We'll start to implement all this in the next video.