
Lesson Description
The "Testing Helper Functions" 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 demonstrates dropping and recreating tables in tests to ensure a consistent starting state and catch schema-related bugs early. He also creates helper functions for tasks like adding test users or habits and cleaning up data efficiently.
Transcript from the "Testing Helper Functions" Lesson
[00:00:00]
>> Speaker 1: Why do we drop and recreate tables Well, kind of like what I told you I want each test to have the same state as all the other tests, so I want to drop everything, make sure there's no leftover data from the previous test runs, have a consistent starting state, and catch schema-related bugs early on, not during the test
[00:00:00]
Uh, yeah, that's why we're doing that, and then now we just need to make some of our database helper functions These are helper functions that are going to help us like make mock data, like putting a user in a database or creating a token or creating a habit, things that we'll use inside of our tests to get things done
[00:00:00]
Otherwise, I have to rewrite this stuff every single time Let's do that So for the helper functions, pretty straightforward We'll just go into the same folder I must have dragged the wrong thing
[00:00:00]
Hold on There we go OK, that's good now I must say all these paths are going to be broken, but it just auto-fixed it I kind of like one that works OK, so yeah, same thing inside of test setup, we want to add another file
[00:00:00]
And we want to call it DB helpers This file is almost like the seed script—it's just a bunch of helpers that we can use to interact with the database much easier inside our tests versus having to write this logic all over again
[00:00:00]
We're going to write some tests for users, habits, maybe and entries I should probably just do some for habits since that's the only one we did routes for We want to get the schema, we want to get the hash password from there because we're going to use that, or maybe not—probably not user stuff, but it's OK
[00:00:00]
And then generate token from there Cool The first thing is, we want a function that we can call that will create a test user for us that we can then use inside our tests because almost everything is going to revolve around the user, so let's just go ahead and make a function that creates that test user
[00:00:00]
Export const create test user async You can pass in some stuff to override it if you want You could do something like partial, and then you can put in a new user like that This way everything is optional, even though email, username, and password are required
[00:00:00]
By default, what we'll do is we'll just say it's an empty object if you didn't give us anything Default data for a user assuming you gave us nothing is all of this stuff Email would just be test with a random number at example.com
[00:00:00]
Username will be test user with a random number Password doesn't matter if these are the same First name is Test, last name is User, and then any other user data that you might have passed in will override anything that we offer
[00:00:00]
Next, I need to hash the password for whatever password is in here because we don't save plain text passwords, and if we test any of that, it would break if we didn't hash this password
[00:00:00]
So we'll go ahead and do that We'll create a new user, hash the password, and then generate a token using the user's ID, email, and username We'll return the token, the user, and the raw original password if you're going to try logging in
[00:00:00]
Now we want to write a helper function to create a habit This will take in a User ID because all habits can't be created without that, and then habit data We'll create default data with a name, timestamp, description, frequency, and target count
[00:00:00]
We'll insert the habit into the database with the User ID and default data Lastly, a programmatic way to clean up the databases by deleting entries, habits, users, and tags Those are all of our helpers.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops