In this video we're going to take a look at some the different libraries and processes that we're going
to go through to implement all this testing stuff.
Now as a reminder we're going to really be focusing on this testing goal number one or hour off service.
So we're going to write out some tests that are going to try to send a request off to our service and
then make some assertion about the response or make sure that some data was written into Mongo DB.
We are not going to be worried right now about writing some tests or on models or event admitting and
receiving.
OK.
So with all that in mind let's take a look at how we're going to wire up all this testing stuff.
We're going to add in a new script to our package not just on file.
Whenever we run the command and run test we are going to start up a test runner called jest.
So just as the library that we're going to use to actually execute tests inside of our project then
on the right hand side this is pretty much what we're going to have just do the wind just starts running.
We're going to tell just to do the following things you see over here first off we're gonna start up
in in memory copy of Mongo DB.
So this is gonna be a copy of Mongo that's running in memory.
In other words it will not be you do not have to install Margo directly onto your machine.
Now even if you already have Mongo installed on your local machine we are still going to use this in
memory copy.
There's a couple of reasons for that and we'll go through them as we start to wire up the in-memory
copy after that we're gonna start up our Express app and then we'll use a library called super test
to make fake requests to our Express application and then after that we will run some assertions to
make sure that the requested exactly what we expected it to do.
So maybe respond respond to with some particular data or wrote some particular data to Mongo DB.
Now I want to focus on this third step right here.
The Super Test library in order to get Super Test working well we're going to do a little refactor to
our project.
So I want to show you some documentation around Super Test and help you understand why we're gonna do
this refactor inside of a new browser tab.
I'm going to open up the documentation for Super Test by going to NPM J ask.com and then doing a search
for super test
and here it is right here.
I'm going to scroll down just a little bit and find the example section inside this example section.
They've got a little bit of code right here.
Now I took a screenshot of this code and put a diagram together around it.
So I'm gonna go back over and take a look at that screenshot.
So here's that little bit of code that's inside the read me and there's just something very important
inside there that I want to point out right about in this section right here.
They have some code to create an express server.
Now at present you and I are doing that inside of our index not just file.
Yes technically we create root handlers inside those other files and then we associate them all together
inside the index such as file but we actually create our Express app like that express thing right there
inside the index such as file.
Why is that relevant.
Well down here is how we use super tests to actually test our Express server.
We have to get access to the Express application variable that is defined inside that index such as
file.
We're going to pass that express app into this request function that is coming from the Super Test library
and then we can make a request and write out some expectations around it the whole thing I want to focus
on here the whole big point I'm trying to make is that we are creating our Express application inside
the index such as file.
So at some point in time we're going to have to write out some code that's going to look a little bit
like this.
We're going to have to try to require the Express application from that index such as file over to some
testing file just so we can get X access to that app variable there's a little problem with that inside
of our index such as file.
We have a lot of startup code.
We have some code to tell the Express app to start listening on some given port.
We also have some hard coded stuff to say tell mango or Mongoose to connect to our Mongo DB database
on some particular U.R.L..
So there are some very particular very hard coded startup logic inside of index dot J.S. but most in
particular I want to focus on the fact that inside of index such s we automatically start listening
on port three thousand inside there.
Why is that relevant.
Well at some point time you and I might want to be able to run multiple different tests on the same
machine.
In other words we might want to run some tests around our off service at the same time that we are running
some tests around our order service the OSS service and the order service might both be trying to listen
on port three thousand.
So if we try to require in our Express app from that index not just file and both the off service and
the ticket service or whatever else are listening on the same port we're gonna very quickly end up with
an error and we will not be able to run tests from different services on the same machine at the same
time just because they are both listening on the same port so to get around this and to allow us to
run tests from multiple services on the same machine at the same time.
We're going to rely upon some default behavior or some nice functionality inside a Super Test to back
over here at the example section.
You'll notice that it says if this server is not listening for connections then it will automatically
start to listen on and ephemeral ports.
A federal port right here just means that super test is going to pick a random available port on your
computer and have the express app start to listen on that port.
And so that feature right there will allow us to run tests from multiple different services at the same
time and not have to worry about this port issue however if we want to take use of that functionality
that means that inside of our index not just file we can not have any code that's going to have the
express app automatically start to listen on port three thousand.
So how are we going to fix this all up.
Well here's what we're gonna do.
Here's the refactor we're going to go through in the next video we're going to start to split out our
index not just file into two different files.
We're gonna create a new file called app J.S. inside about us we're going to create our Express application
and export it the Express app that we create inside this file is not going to be listening on any port.
So in other words we are not going to have any code inside of app J.S. that looks like this right here.
We are not going to have anything inside this new app has filed it's going to have our app start listening
on port three thousand or anything else.
Instead we're just going to export the app after we have wired up all the appropriate middleware as
root handlers and so on we're then going to export that thing into the test file where it can then be
safely used by Super Test.
We will also import it into a new index J.S. file and inside of index not J.S. is where we're going
to have some actual code to start up our app and start listening on port three thousand so whenever
we want to actually run our application inside of a dev or a production environment.
In other words we never want to really start up our server for real.
We're still going to execute the index not just file and nothing's just going to pull in the app that
was declared over here but we're going to have the separate file that exports just the app itself so
we can still use it safely inside that test file.
OK.
So that's pretty much it.
I just want to help you understand why we are doing this refactor now at this point you might be saying
Steven this seems like kind of a big refactor just to get the Super Test library working.
Well it turns out that there are actually some huge benefits from creating our Express app inside of
its own file and then doing some actual startup logic inside of this other file over here and you're
gonna see some less benefits later on as we start to work on some of our other services.
All right so a quick pause right here.
We'll start to implement all this in the next video.