API Design in Node.js, v5

Verify Test Setup

Scott Moss
Netflix
API Design in Node.js, v5

Lesson Description

The "Verify Test Setup" Lesson is part of the full, API Design in Node.js, v5 course featured in this preview video. Here's what you'd learn in this lesson:

Scott explains how to set environment variables in package.json with cross-env to run tests in the correct environment. He also discusses using hooks like beforeEach and afterEach and writes a simple test to confirm the setup.

Preview
Close

Transcript from the "Verify Test Setup" Lesson

[00:00:00]
>> Scott Moss: The last thing we wanna do before we start writing tests and running them is we need to go through our package JSON. We have a test command here. We need to make sure that Vitest for sure uses our dot test. env and if we look at our ENV.

[00:00:20]
env and if we look at our ENV. We know that if app stages test. That ENV will be loaded up. So what we wanna do is here inside of this one, we can use this thing called Cross ENV that's already installed on your machine, and you can say Cross ENV and you can say app.

[00:00:35]
So what we wanna do is here inside of this one, we can use this thing called Cross ENV that's already installed on your machine, and you can say Cross ENV and you can say app. Stage equals. Test like this, so this will set the environment variable of app stage equal to test before it executes vitest run which is gonna run our test. And that way the code that we're testing when that code imports env.ts env.ts will then load up the Testing ENV and not the development ENV.

[00:01:01]
And that way the code that we're testing when that code imports env.ts env.ts will then load up the Testing ENV and not the development ENV. Vitest is gonna load up the Testing ENV regardless. But Viest itself isn't like using well outside of that database setups that we did it's not using the Database it's the code that we're Testing. That uses the env.ts file that I want to pull in the env.test environment variable file and not the .env file so super complicated, but.

[00:01:21]
That uses the env.ts file that I want to pull in the env.test environment variable file and not the .env file so super complicated, but. That's why I made that at the beginning of the course was just for this. Cool, OK, let's just test that this works, so we'll write a test to test this, and then we'll actually write real tests when we, when we come back, so. How do we test to make sure?

[00:01:41]
How do we test to make sure? This works We actually can just write a simple test, so. We have all of that, I'll talk about the best practices with the setup and stuff, but essentially it's. You know, you can just use these hooks.

[00:01:59]
You know, you can just use these hooks. We don't really have to do too much of this because the set up, the setup's gonna run before all the tests run, but then we still have to be responsible in between each test and how we wanna do that, and some of the ways we can do that is by using some of these hooks that we get from viTest called like after each, before each, after all, before all we can do things like dropping the database, clearing mocks, stuff like that. So we'll have to be responsible in that way, we already did a lot of this. And yeah, let's go ahead and verify our setup works.

[00:02:15]
And yeah, let's go ahead and verify our setup works. So let's just make a quick test. We'll make a test called setup.test.ts which is literally just Testing whether or not our setup works. So we're not actually testing anything crazy, we just wanna see if this works, right, so.

[00:02:35]
So we're not actually testing anything crazy, we just wanna see if this works, right, so. What we can do is got a test setup, we can make a new file here, we can call it. Set up. Test dot ts.

[00:02:50]
Test dot ts. We can import our helpers so I'm gonna say create Test user and create test habit. Like that. And then I'm going to go ahead and describe you should get that for free and you're autocomplete because.

[00:03:07]
And then I'm going to go ahead and describe you should get that for free and you're autocomplete because. Already snuck it into your TS config, you didn't even know it right there. It's in your types. So we'll get the describe, and I'm just gonna say I just wanna test the setup.

[00:03:28]
So we'll get the describe, and I'm just gonna say I just wanna test the setup. Just wanna test my setup, that's all I wanna do. Cool, so we'll do that and then I'll say test. Should connect to the Test DB.

[00:03:47]
Should connect to the Test DB. So we'll try this. I'm about, 40% sure this will work. So we get the user, the token.

[00:04:12]
So we get the user, the token. Equals await, create test user like that. And then I'm just going to say expect so I'll talk about all this test and describe stuff when we actually get the testing so you can just follow along right now but if you have questions, feel free. If you've written any tests in any of the JavaScript languages, it's exactly the same as any other testing Framework.

[00:04:28]
If you've written any tests in any of the JavaScript languages, it's exactly the same as any other testing Framework. It's gonna say user to be. Defined, I suspect that to be there, and I'll just do that for now and I'll say, await, clean up database when I'm done, so. We can try to run that.

[00:04:44]
We can try to run that. I actually have like these things that when I click will run them, but I'm gonna run them in a terminal so we can. Actually see them, so. All we have to do is.

[00:05:01]
All we have to do is. In this case, We can type in NPM. Test Oh, What happened? And they can actually import it I messed something up.

[00:05:15]
And they can actually import it I messed something up. First of all, I said I couldn't find any tests, so. Let's try this, so we say NPM test and then maybe if I put. Setup dot test.

[00:05:35]
Setup dot test. As a search to find that one, OK. OK, there it is. All right.

[00:05:53]
All right. OK, cool, I guess I just, I just assumed that it would find the test and run it, but it wanted me to specifically. Give it a hint of where the test was, so. By calling NPM tests.

[00:06:06]
By calling NPM tests. Whatever this is basically a search it's gonna do a regex and find anything that matches this so I don't have to put the full file name. It's just it's gonna look for any if I just put set up this will run too so it's just looking for something that has that. I just thought it would find it, but looks like it worked, you can see that it actually.

[00:06:23]
I just thought it would find it, but looks like it worked, you can see that it actually. Pushed our Schema into the To the test database that we had, so that was the part that I really wanted to check. It connected to the DB. It, installed a or I'm sorry, and it created a user.

[00:06:28]
It, installed a or I'm sorry, and it created a user. We tested that the user existed, which passed the test and then it stopped, so looks like it worked. And if you run this again, it'll just do everything all over again and doing it pushed to a Database that already has the same stuff won't cause any errors it'll just say good to Go so.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now