It's now time for us to start to write out an actual test.
So in this video we're going to write a very simple test around our sign up root handler because we
are trying to write a test around these sign up root handler.
We're going to create a folder inside of the roots directory so new folder called underscore underscore
test underscore underscore when we make use of jests we follow the convention that if we are trying
to test a given file we make a folder inside the same directory called underscore underscore test like
that right there and then inside that directory we'll make a new file that has the name of the file
that we are trying to test.
So sign up then dot test dot to yes it's going to follow the same convention for testing everything
whenever we want to test something inside of a file.
Make a folder in the same directory call it underscore underscore test underscore underscore make a
file inside there with the same name as the original file but with test dot.
Yes at the very end then inside this file we're gonna write out like I said a very simple test and figure
out how to run it.
So at the very top we're going to first import requests from two protests so remember Super Test is
a thing that's going to allow us to fake a request to the Express application.
Well then add in an import statement to get the app that we had declared inside of the abductees file.
So we will get app from up directory up
and then we can write start to write out a test statement.
So we will write out it and then a description for this test with this very first test that we are going
to put together.
We're just gonna make sure that we can send in a post request to this sign up or out right here with
a valid email and password and get some kind of response.
So I just wanted to send in a post request to that route with an email a password in the body.
And we're going to make sure that we get a response that has a status of 2 a 1.
That's all we're gonna do for this very first test so inside the string right here I'm going to put
in a description of it returns a to a one on successful sign up then as a second argument we'll put
in an async function all the different tests that we gonna put together with super tests are going to
follow a very similar structure.
We are going to return request.
We're going to pass in app and then we're going to chain on a number of different methods to it.
So the first you're going to chain on is to tell this thing where we want to make the request to and
what method we want to use for the request.
So in our case we want to make a post request and we want to make the post request to a root of API
users sign up.
So we'll do flash API flash users flash sign up we will then send off the request.
And when we do so we're going to specify some information to include in the body of the request.
So this right here is going to be provided as the body as I mentioned for this first test we want to
make sure we send along a valid email and develop password to sign up with.
So I going to specify an email of test at test dot com and a password of
so this is going to actually send the request off.
If we then want to make some assertions about the response we'll chain them onto the very end.
So for example we can write in the dot expect and then put in the status code that we expect to receive
in the response.
In this case we expect to get a tax code of two a one back and that's it.
That's everything we need to write out for a very basic and straightforward test.
Now you will notice that I marked this function as async even though we did not use the awake keyword
inside of here.
I'm just doing that out of habit.
We will eventually attempt to make multiple requests inside of a single test when we do we're going
to have to start to use the await keyword.
And so I found that it's easier to just add on async from the get go and not have to worry about adding
on adding it on it's something in the future or forgetting to add it on and being given some kind of
error will then say this file and to run it we will go over to our terminal and run NPM run test inside
or off directory so I'll change into off and then NPM run test and just you know the first time we run
this we are going to probably see an error which is OK but let's run it anyways.
So it looks like the test is running.
Now the very first test we run is going to take a little bit to actually start up and it looks like
we'll come back to why it took so long just a moment.
What's more relevant is that we got a 400 bad request back.
So why is that.
Well you might recall if we take a look at the implementation of our sign up root handler here sign
up dot to yes right here instead of here at some point in time we attempt to create a new date on a
Web token when we attempt to create that Jason web token.
We try to access environment variable of JWT underscore key we had previously been making a check to
ensure that this thing was defined inside of our index not t as file.
But right now we have split up our index not.
Yes an apt t as files into two separate files.
So we are no longer making sure that this enviroment variable exists in this matter fact it doesn't
exist right now when we are running all of our code inside this test environment.
This environment variable only gets defined when we run our code inside of a pod as remember we had
specified for this enviroment variable to be defined inside of our deployment config file so to get
this error to go away all we need to do is to make sure that we define this enviroment variable at some
point in time.
There are many different ways to define a environment variable in a kind of test environment.
It's going to take just a very simple very direct way it's not necessarily the best way of doing it
but it's going to get that error to go away right away.
I got to go back to the setup dot t s file I'm going to find you before all statements and inside there
we're just going to directly set the enviroment variable with a process EMV data to underscore key and
we'll set it equal to whatever string.
Again not the best way of handling this but it's going to work for right now.
I'm gonna say this I should be able to then go back over to my terminal and see that my test is now
passing awesome.
So we've gone through the process of writing out one full test we're going to start to write out a lot
more tests very quickly.
So we'll take a quick pause right here and continue in just a moment.