In this lesson you're going to learn how to write test cases that interact with our Express REST API
so our test cases will actually make requests to the various endpoints.
Then we can write assertions to make sure they worked as expected.
So if I want to write a test case that logs in successfully I could expect I get a two hundred back
with an authentication token in the response.
I could also write a test case that makes sure when the credentials are invalid I don't get logged in.
So we'll learn how to do that in a moment before we can get to that.
We have to remember that our test cases they're about to start loading in files in the source directory
related to the task manager app now currently that would be a big problem because those files depend
on certain environment variables existing and they don't we only load in that Dev dot EMV config file
when we run the dev script in package.
Jason.
Currently we're not loading in any configuration for the test script so we need to make sure that we
do supply a set of environment variables to jest when it runs.
So our test cases can actually start up the task manager server and to make requests to it.
Now to do that what we're going to do is create a separate environment.
We're going to create a new one called test so we'll create the following file test dot EMV to set up
those environment variables.
Now what needs to get defined in here.
Well everything that's defined in dev dot EMV so Port send grid API key.
The Mongo D.B. U.R.L. and our JWT secret.
Now you might be wondering why create a new environment at all.
Why not just load in dev dot EMV for our test cases.
And the answer has to do with the database.
So the value for Mongo DB.
You are all currently the dev environment is interacting with this database on our local Mongo DB server.
We don't want the test environment to also use that because the test environment it's going to populate
the database with seed data.
So dummy data that the test cases can use to do things like test log in and it's gonna run test cases
over and over again.
So it'll be wiping the database setting up that C data and running a test case and if it's doing that
for our dev database it's gonna make it hard for us to actually use the API in postman as we're setting
up new routes.
So all we're gonna do is take the dev dot EMV file copy it over to test dot EMV and add onto the end
of our Mongo DB connection string Python test.
So now our test environment is using a one database on our local Mongo DB server and the dev environment
is using another making sure they're not interacting with each other's data which is going to cause
problems.
Now all we need to do is make sure that when we run that test script the config file is loaded so we'll
be adding EMV BMD to the test command just like we did for the dev command up above and the path to
the EMV file dot forward slash config forward slash test dot e and the perfect.
So now we have a modified version of our test script that is going to load in the correct environment
variables and that means our test suite is now ready to actually start the process of starting up the
task manager app and interacting with it.
Now before we get to that and there is one more thing we're going to configure related to jest and that's
gonna happen right here in package dot Jason.
What we're gonna do is add a new root property and package done Jason.
So right here we can add it anywhere we like.
I'll add it just below scripts for example and this one is gonna be called Gest and this is going to
store our just configuration.
It's an object itself and we'll put a comma at the end.
And right here we can provide the various things we want to configure now for our purposes working with
node j s the only thing we really need to set is what's called test environment.
This is Jason.
So make sure to put your property names and values in double quotes right here.
That is test capital E environment and the value for this one is note.
Now if you want to learn more about how you can configure just the documentation is going to guide you
through that.
So over here in the sidebar if we scroll down to API docs once again we have configuring jest.
This talks about exactly what we just did.
Setting up a just property in your package dot Jason File to configure it.
Although there are other ways creating separate files which you could also use.
And on the right hand side bar they have all of the different things you can configure and there's a
lot.
Now the only thing we need is test environment which we already used.
And if we pull that up by clicking it we'll see the default value is J.S. Dom.
This is a javascript environment similar to what you'd see in the browser.
So by default Gest is assuming your testing browser based javascript though it also supports node as
we can see down below.
We set the value to node to use a node environment for our test cases instead.
And this is definitely something we want to do especially as we start to load in all of those node scripts
inside of the source directory.
So now that we have this in place just is all set up and it's ready to actually go ahead and start up
our app and connect to it making queries to the API from our test cases.
That's what we'll do in the next lesson.
So let's jump right in.