1 00:00:01,040 --> 00:00:04,400 Now that we've installed a bunch of dependencies we're gonna start to wired them all up inside of our 2 00:00:04,600 --> 00:00:05,380 service. 3 00:00:05,490 --> 00:00:09,770 A the configuration that we're about to add in is all about just getting just which is our test runner 4 00:00:10,040 --> 00:00:14,540 to start up in cyber project and give it some basic configuration to tell it how to test everything 5 00:00:14,570 --> 00:00:15,600 inside of here. 6 00:00:15,850 --> 00:00:19,710 We're going to first get started inside the package not just on file inside of here. 7 00:00:19,730 --> 00:00:26,020 I'm gonna find the script section and I'm going to add in a new script called test whenever we run test. 8 00:00:26,030 --> 00:00:27,680 We're going to execute the command. 9 00:00:27,680 --> 00:00:36,230 Just dash dash watch all dash dash no dash like so that this is going to start up just that's gonna 10 00:00:36,240 --> 00:00:42,030 tell it to run all the different tests inside of our project when ever any file changes the no cash 11 00:00:42,030 --> 00:00:47,940 flag right here is related to our attempt to try to use typescript with just just does not have typescript 12 00:00:47,940 --> 00:00:52,980 support out of the box and sometimes just is going to get really confused understanding when we change 13 00:00:53,010 --> 00:00:58,750 a typescript file this no cash flag right here is going to add in just a little bit of the fix that's 14 00:00:58,750 --> 00:01:03,090 going to catch some issues that you're going to see are going to start to arise anytime we try to change 15 00:01:03,120 --> 00:01:07,590 a typescript file and you're going to see that sometimes just does not always recognize those changes 16 00:01:07,980 --> 00:01:14,390 the no cash flag right there is to address that little issue then after our entire script section we're 17 00:01:14,390 --> 00:01:21,590 gonna add in another configuration block for just itself inside of here we're going to add in a preset 18 00:01:21,680 --> 00:01:28,560 of T S just so again just does not understand what type script is we installed a dependency called t 19 00:01:28,560 --> 00:01:35,520 s just that is going to add in typescript support for us we'll then set up a test environment flag of 20 00:01:35,700 --> 00:01:36,120 node 21 00:01:39,150 --> 00:01:45,480 do double check your spelling on environment make sure you've got N and then n Easy words misspell and 22 00:01:45,540 --> 00:01:53,400 then finally we're going to add in setup files after e and V so we are going to tell just to run a setup 23 00:01:53,400 --> 00:01:59,550 file inside of our project after its initially starts everything up inside this setup file we're going 24 00:01:59,550 --> 00:02:03,660 to put together a couple of different helpers just to make facilitating writing our tests a lot easier 25 00:02:04,320 --> 00:02:09,360 so we're gonna tell this thing to try to run the file in the current working directory so dot slash 26 00:02:09,940 --> 00:02:18,600 in the s our C folder in the test folder called setup dot t yes OK so let's save this we're then going 27 00:02:18,600 --> 00:02:23,460 to go ahead and create this setup file so again we're gonna do a little bit of initial setup inside 28 00:02:23,460 --> 00:02:28,620 there we're going to start up Mongo DB memory server we're going to get Mongoose to connect to it and 29 00:02:28,620 --> 00:02:33,720 a couple of other associated things inside of my WRC directory I'm going to make a new folder called 30 00:02:34,140 --> 00:02:39,060 test and then inside there I'll make a file called Setup Dot T Yes 31 00:02:41,840 --> 00:02:47,150 all right at the very top I'm going to import a couple of different things I'm going to get Mongo DV 32 00:02:47,380 --> 00:02:57,900 are some you just Mongo memory server from mongo DV memory server I'm going to import Mongoose 33 00:03:01,260 --> 00:03:08,660 I'm going to import app so that is our Express application that we've just moved over to that new app 34 00:03:08,660 --> 00:03:15,420 file and I think that should be pretty much it for right now so then inside of here we're going to make 35 00:03:15,420 --> 00:03:20,350 sure that before all of our different test startup we're gonna create a new instance of this Mongo DB 36 00:03:20,460 --> 00:03:25,890 memory server again this is going to start off a copy of Mongo DB in memory this is going to allow us 37 00:03:25,920 --> 00:03:30,380 to run multiple different test suites at the same time across different projects without them Altria 38 00:03:30,410 --> 00:03:36,220 and trying to reach out to the same copy of Mongo Mongo memory server also gives us direct memory access 39 00:03:36,220 --> 00:03:40,540 or essentially direct access to this Mongo DTV database which is going to eventually be a little bit 40 00:03:40,540 --> 00:03:45,790 handy for a couple of other reasons as well so to create an instance of this before all of our tests 41 00:03:45,790 --> 00:03:53,220 set up we're going to define a hook function we're gonna write out before all asin an arrow function 42 00:03:53,440 --> 00:03:59,600 and we will mark that zero function as async again we referred to it before all as a hook function what 43 00:03:59,610 --> 00:04:04,590 are we pass inside of here that function is going to run before all of our tests start to be executed 44 00:04:05,400 --> 00:04:10,380 so inside of here we can start to startup Mongo to be memory server and then have Mongoose connect to 45 00:04:10,380 --> 00:04:11,210 it. 46 00:04:11,360 --> 00:04:19,090 So we're gonna put inside of your Mongo is new Mongo memory server 47 00:04:23,010 --> 00:04:27,500 or then going to take a look at the memory server that was just created and we're trying to go in to 48 00:04:27,500 --> 00:04:32,770 try to get to yet you URL to connect to it will say Mongo you are. 49 00:04:32,810 --> 00:04:37,640 I is await Mongo Dot. 50 00:04:37,720 --> 00:04:45,510 Yet you are I after that we're gonna tell Mongoose to connect to this in memory server we'll do it a 51 00:04:45,510 --> 00:04:52,930 wait Mongoose connect we'll give it that you tried to connect to the Mongo you or I and then we'll pass 52 00:04:52,930 --> 00:04:55,360 in some configuration options to this thing as well. 53 00:04:55,450 --> 00:04:59,020 These options are gonna pass and don't really do a whole lot they're just gonna make sure that Mongoose 54 00:04:59,020 --> 00:05:05,230 does not complain about some related stuff or some behind the scenes stuff going on we'll say new use 55 00:05:05,290 --> 00:05:17,780 new U.R.L. parser true use unified topology true as well okay so Alex good after that we're gonna put 56 00:05:17,780 --> 00:05:23,030 together another little configuration block so this is a hook that's going to run before all of our 57 00:05:23,030 --> 00:05:28,380 tests are then gonna put together another hook of before each. 58 00:05:28,430 --> 00:05:32,990 So this is gonna be a function that's going to run before each of our tests are going to mark this thing 59 00:05:33,020 --> 00:05:39,830 as async as well the B for each test starts we're going to reach into this Mongo DB database and we're 60 00:05:39,830 --> 00:05:45,780 going to delete or essentially reset all the data inside their to do so we're going to use Mongoose 61 00:05:46,110 --> 00:05:51,450 to take a look at all the different connections that exist inside this Mongo we're going to delete all 62 00:05:51,450 --> 00:05:59,320 those different collections inside of here we'll write out elections is a wait don't forget that a weight 63 00:05:59,940 --> 00:06:06,730 Mongoose connection DV elections that's going to get us all the different collections that exist or 64 00:06:06,900 --> 00:06:10,630 then going to loop over them and tell them to delete all the data inside. 65 00:06:10,650 --> 00:06:15,750 So again this is just gonna reset all of our data and between each test that we run we'll do a four 66 00:06:15,750 --> 00:06:25,750 letter collection of collections and then await election dot delete many will pass in an empty object 67 00:06:28,140 --> 00:06:32,470 then finally we're going to make sure that after we finish running all of our different tests we need 68 00:06:32,470 --> 00:06:37,600 to stop that meant Mongo DB memory server and we're also going to tell muggers to disconnect from it 69 00:06:37,630 --> 00:06:46,380 as well so we'll do that inside of a after all and as he guests this is a hook that's gonna run after 70 00:06:46,440 --> 00:06:54,650 all of our tests are complete we will mark this as async and an inside do and a wait mango not stop 71 00:06:55,830 --> 00:07:04,440 and a wait Mongoose connection flows now one little issue here we're trying to access the Mongo database 72 00:07:04,610 --> 00:07:10,050 that we created inside the scope of this function up here so the mango variable is not in the scope 73 00:07:10,050 --> 00:07:15,270 of after all because I had been defined inside this Interscope so to get this working we need to be 74 00:07:15,270 --> 00:07:22,840 clear that variable ahead of time so say let Mongo I'm going to annotated type as any right now well 75 00:07:22,870 --> 00:07:30,020 then delete the construct there and now we should be able to access Mongo right there as well. 76 00:07:30,080 --> 00:07:33,070 Yeah looks like the error went away all right. 77 00:07:33,090 --> 00:07:37,150 So I know that we went through all this setup very quickly but this is just a little bit of stuff that 78 00:07:37,150 --> 00:07:41,220 pretty much had to be done if you need to replicate this all on your own project. 79 00:07:41,290 --> 00:07:46,000 Well you would end up taking a look at the documentation and getting a better idea of what's going on. 80 00:07:46,080 --> 00:07:48,550 We'll talk about some more config inside of your overtime. 81 00:07:48,610 --> 00:07:53,110 And we are going to actually have to come back and add in some additional code very quickly as we start 82 00:07:53,110 --> 00:07:56,950 to write out our first test and realize that what we've got inside of here is good but could be a little 83 00:07:56,950 --> 00:07:57,970 bit better. 84 00:07:58,030 --> 00:08:01,210 In particular you'll notice that the import for the app is unused right now. 85 00:08:01,210 --> 00:08:05,670 We are going to eventually come back and make use of it so we can just leave it there for right now. 86 00:08:05,670 --> 00:08:05,940 All right. 87 00:08:05,940 --> 00:08:10,770 So now we've got enough set up but the other one more pause and we'll start to write out our first test 88 00:08:10,800 --> 00:08:11,820 in just a moment.