API Design in Node.js, v5

Global Test Setup

Scott Moss
Netflix
API Design in Node.js, v5

Lesson Description

The "Global 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 sets up a global test configuration by organizing folders, importing modules, syncing schemas, and running raw SQL. He also explains how these steps keep tests isolated and maintain a clean database state.

Preview
Close

Transcript from the "Global Test Setup" Lesson

[00:00:00]
>> Speaker 1: OK, let's do our global test setup to set up some things, and I'll talk about why we're setting these things up the way that we're doing them

[00:00:00]
But it's mostly just so that we have a clean separation amongst our tests Let's do that, we're going to make a new folder called Tests (pluralized) and then in this folder we'll make a new folder called Setup

[00:00:00]
In that folder, we will make a global setup So what we want to do in this global setup is a few things We're going to import our DB from source/db/connection

[00:00:00]
And then we're going to import all our tables, so we have users, habits from source/db/schema We have our habits, entries, tags, and habit tags

[00:00:00]
Next, what we want to do is write some raw SQL We'll import SQL from Drizzle ORM and import execSync from Node, which basically allows us to run bash commands inside of Node, that's the best way I can describe it, inside a child process

[00:00:00]
We'll export a default async setup function We'll add some logs saying we're setting up the Test DB The first thing we want to do is drop everything in the database

[00:00:00]
There are many ways to do this in Drizzle I chose to do it this way to show you a different way of interacting with the database With the database, you can use exec, which takes in raw SQL

[00:00:00]
You can use this template-tagged SQL object that's really cool because it can substitute your tables and infer the SQL names without you having to write them yourself

[00:00:00]
So this template tag SQL, I can just say drop table if exists for entries, habits, users, tags, and habit tags with cascade for any relationships

[00:00:00]
We're going to drop the database as soon as we start the tests, so whatever's in the database is going to get dropped This is why you want your own database for tests

[00:00:00]
The next thing is because our test database is probably not up to date with the latest schema, we want to push whatever the latest schema is right now to the test database every time we run tests

[00:00:00]
I'll run the command pushing the schema using Drizzle Kit with execSync I'll use mpx Drizzle Kit push, but instead of using the configuration from the Drizzle config, I'll supply my own database URL

[00:00:00]
I'm using process.env instead of importing the env file because that env is for loading up the app environment, and I don't want to require all those environment variables just to run tests

[00:00:00]
I'll pass flags for the database URL, schema location, and dialect (PostgreSQL) I'll use standard out to inherit the child process's output in this terminal

[00:00:00]
I'll add logging for test database setup and error handling Lastly, I'll return a cleanup function that Vitest will run when all tests are done

[00:00:00]
I'll do the same database cleanup process, wrapped in a try-catch block I'll end with process.exit(0) to ensure the process completes properly.

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