Now that we've installed a bunch of dependencies we're gonna start to wired them all up inside of our
service.
A the configuration that we're about to add in is all about just getting just which is our test runner
to start up in cyber project and give it some basic configuration to tell it how to test everything
inside of here.
We're going to first get started inside the package not just on file inside of here.
I'm gonna find the script section and I'm going to add in a new script called test whenever we run test.
We're going to execute the command.
Just dash dash watch all dash dash no dash like so that this is going to start up just that's gonna
tell it to run all the different tests inside of our project when ever any file changes the no cash
flag right here is related to our attempt to try to use typescript with just just does not have typescript
support out of the box and sometimes just is going to get really confused understanding when we change
a typescript file this no cash flag right here is going to add in just a little bit of the fix that's
going to catch some issues that you're going to see are going to start to arise anytime we try to change
a typescript file and you're going to see that sometimes just does not always recognize those changes
the no cash flag right there is to address that little issue then after our entire script section we're
gonna add in another configuration block for just itself inside of here we're going to add in a preset
of T S just so again just does not understand what type script is we installed a dependency called t
s just that is going to add in typescript support for us we'll then set up a test environment flag of
node
do double check your spelling on environment make sure you've got N and then n Easy words misspell and
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
file inside of our project after its initially starts everything up inside this setup file we're going
to put together a couple of different helpers just to make facilitating writing our tests a lot easier
so we're gonna tell this thing to try to run the file in the current working directory so dot slash
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
to go ahead and create this setup file so again we're gonna do a little bit of initial setup inside
there we're going to start up Mongo DB memory server we're going to get Mongoose to connect to it and
a couple of other associated things inside of my WRC directory I'm going to make a new folder called
test and then inside there I'll make a file called Setup Dot T Yes
all right at the very top I'm going to import a couple of different things I'm going to get Mongo DV
are some you just Mongo memory server from mongo DV memory server I'm going to import Mongoose
I'm going to import app so that is our Express application that we've just moved over to that new app
file and I think that should be pretty much it for right now so then inside of here we're going to make
sure that before all of our different test startup we're gonna create a new instance of this Mongo DB
memory server again this is going to start off a copy of Mongo DB in memory this is going to allow us
to run multiple different test suites at the same time across different projects without them Altria
and trying to reach out to the same copy of Mongo Mongo memory server also gives us direct memory access
or essentially direct access to this Mongo DTV database which is going to eventually be a little bit
handy for a couple of other reasons as well so to create an instance of this before all of our tests
set up we're going to define a hook function we're gonna write out before all asin an arrow function
and we will mark that zero function as async again we referred to it before all as a hook function what
are we pass inside of here that function is going to run before all of our tests start to be executed
so inside of here we can start to startup Mongo to be memory server and then have Mongoose connect to
it.
So we're gonna put inside of your Mongo is new Mongo memory server
or then going to take a look at the memory server that was just created and we're trying to go in to
try to get to yet you URL to connect to it will say Mongo you are.
I is await Mongo Dot.
Yet you are I after that we're gonna tell Mongoose to connect to this in memory server we'll do it a
wait Mongoose connect we'll give it that you tried to connect to the Mongo you or I and then we'll pass
in some configuration options to this thing as well.
These options are gonna pass and don't really do a whole lot they're just gonna make sure that Mongoose
does not complain about some related stuff or some behind the scenes stuff going on we'll say new use
new U.R.L. parser true use unified topology true as well okay so Alex good after that we're gonna put
together another little configuration block so this is a hook that's going to run before all of our
tests are then gonna put together another hook of before each.
So this is gonna be a function that's going to run before each of our tests are going to mark this thing
as async as well the B for each test starts we're going to reach into this Mongo DB database and we're
going to delete or essentially reset all the data inside their to do so we're going to use Mongoose
to take a look at all the different connections that exist inside this Mongo we're going to delete all
those different collections inside of here we'll write out elections is a wait don't forget that a weight
Mongoose connection DV elections that's going to get us all the different collections that exist or
then going to loop over them and tell them to delete all the data inside.
So again this is just gonna reset all of our data and between each test that we run we'll do a four
letter collection of collections and then await election dot delete many will pass in an empty object
then finally we're going to make sure that after we finish running all of our different tests we need
to stop that meant Mongo DB memory server and we're also going to tell muggers to disconnect from it
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
all of our tests are complete we will mark this as async and an inside do and a wait mango not stop
and a wait Mongoose connection flows now one little issue here we're trying to access the Mongo database
that we created inside the scope of this function up here so the mango variable is not in the scope
of after all because I had been defined inside this Interscope so to get this working we need to be
clear that variable ahead of time so say let Mongo I'm going to annotated type as any right now well
then delete the construct there and now we should be able to access Mongo right there as well.
Yeah looks like the error went away all right.
So I know that we went through all this setup very quickly but this is just a little bit of stuff that
pretty much had to be done if you need to replicate this all on your own project.
Well you would end up taking a look at the documentation and getting a better idea of what's going on.
We'll talk about some more config inside of your overtime.
And we are going to actually have to come back and add in some additional code very quickly as we start
to write out our first test and realize that what we've got inside of here is good but could be a little
bit better.
In particular you'll notice that the import for the app is unused right now.
We are going to eventually come back and make use of it so we can just leave it there for right now.
All right.
So now we've got enough set up but the other one more pause and we'll start to write out our first test
in just a moment.