Setting Up a Test Database for Prisma Integration Tests
00:00 A lot of our tests are calling this insert new user and that's sticking that into our development database. We want to have a separate test database. Now, the trick here is that this insert new user, of course, that uses Prisma and the Prisma client comes from this Prisma client that we're creating right here.
00:17 And that determines where to connect to based on the environment variable database URL. Now, of course, we could set that database URL when we run our tests, but that would be kind of annoying. I'd rather just have that kind of hard-coded into our code as part of our test setup. And finding where we put that is a little bit tricky
00:37 because you have to make sure that you set that before Prisma is imported. So if we go into our test setup and we say, process ENV database URL, and set that to test Prisma data.db, for example, then that's not gonna work because we're actually already importing Prisma right here.
00:56 And in fact, it gets even more complicated because we're actually also importing it here. And if you follow the imports, we're also importing it there. So before we get the chance to set the database URL, it's gonna be imported. And we can't just stick it up here at the top either because the way that ES modules work is they're all resolved
01:16 before any of the other code in the module is processed. And so they're going to happen before we get a chance to set the database URL. But the order in which the imports appear actually does have significance. And so we can import the database URL or import a separate file that sets the database URL.
01:35 And then that will happen before anything else. And the setup test ENV file that we have right here, we've configured as our setup file in VTest, that is going to be the first thing that's evaluated for all of our tests. So we definitely won't import anything
01:54 before this first line up here. And so that's what your job is to do. You need to set up a database URL that gets set before Prisma shows up. And then, yeah, we'll be able to do a couple other things in this DB setup file. So I'm excited for you to try this out.
02:12 And yeah, have a good time with it.