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]
>> Speaker 1: The last thing we want to 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 dotenv And if we look at our env, we know that if app stage is test, that env will be loaded up So what we want to do is here inside of this one, we can use cross-env that's already installed on your machine, and you can say cross-env app.stage=test like this, so this will set the environment variable of app stage equal to test before it executes by test run which is going to run our tests

[00:00:00]
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 going to load up the testing env regardless But Vitest itself isn't like using any 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 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

[00:00:00]
Cool, okay, 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 come back 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 simple You can just use these hooks—we don't really have to do too much of this because 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 want to do that

[00:00:00]
Some of the ways we can do that is by using some of these hooks that we get from Vitest called like afterEach, beforeEach, afterAll, beforeAll 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 So let's just make a quick test

[00:00:00]
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 want to see if this works, right What we can do is get a test setup, we can make a new file here, we can call it setup.test.ts We can import our helpers, so I'm going to say createTestUser and createTestHabit

[00:00:00]
Then I'm going to go ahead and use describe—you should get that for free in your autocomplete because it's already snuck into your TypeScript config So we'll get the describe, and I'm just going to say I just want to test the setup Cool, so we'll do that and then I'll say test: "Should connect to the test DB" So we'll try this I'm about 40% sure So we get the user, the token equals await createTestUser like that

[00:00:00]
And then I'm just going to say expect—I'll talk about all this test and describe stuff when we actually get to testing, so you can just follow along right now 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 It's going to say user to be defined, I expect that to be there, and I'll just do that for now and I'll say, oh wait, clean up database when I'm done

[00:00:00]
We can try to run that I actually have like these things that when I click will run them, but I'm going to run them in a terminal so we can actually see them All we have to do is in this case type in npm test Oh, what happened I couldn't find any tests, so let's try this Say npm test and then maybe if I put setup.test as a search to find that one Okay, there it is

[00:00:00]
Okay, cool 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 By calling npm test with whatever this is, basically a search—it's going to do a regex and find anything that matches this so I don't have to put the full file name It's just going to look for anything If I just put setup, this will run too, so it's just looking for something that has that

[00:00:00]
I just thought it would find it, but looks like it worked You can see that it actually pushed our schema into the test database that we had, so that was the part that I really wanted to check It connected to the DB, it installed or 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.

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