DB Setup
π¨βπΌ alright, so it makes a lot more sense for us to have isolated databases for
each of our test files since they're so easy to create and delete before each
of our tests. We're going to iterate our way to a good solution. The first step
is just to make sure this is going to work for one test. We'll start with the
test in .
To do this, we need to do a couple steps:
- Set the
DATABASE_URL
to the test database (before anything imports prisma!) - Run
prisma migrate reset --force --skip-seed --skip-generate
to reset the database before all the tests start. We can skip seeding because we don't need (or want) to assume there's any existing data other than what our migrations apply. The--skip-generate
is important if we don't want to mess up our development client. - After each test, we need to delete all the users from the database to automatically clean up after ourselves.
- After all the tests are done, we need to disconnect from the database and delete the file.
There's one major sticking point to this: We need to make sure we set
process.env.DATABASE_URL
before anything imports prisma. Otherwise, prisma
will start up and use our development database.So what we're going to do is create a special file we'll import in our setup
that will manage all of this database setup for us, and import that file at the
top of the setup file before anything else has a chance to import prisma.
npx vitest auth