Loading
Current section: Test Database 5 exercises
lesson

Intro to Test Database

Loading lesson

Transcript

00:00 Now it's time to improve our database setup. So right now all of our integration tests that talk to the database are talking to our development database, and that is just not my favorite thing. I already talked about how I like my end-to-end test to use the real database, and that's because I like running the end-to-end test alongside the actual development,

00:19 and I use the end-to-end test to help me with development. So it just makes sense for those to use the same database, and as long as I'm keeping track of everything and cleaning up after myself, then that normally isn't too much of a problem. But for these lower level tests, we're doing all kinds of really interesting and different things, and it's harder to keep track of things,

00:37 and so it would just be nice if they were a completely separate database, and so that's what we're going to be doing in this. If you really wanted to get your end-to-end test to do the same thing, you could probably figure out a way to do this exact same thing for that environment as well. But yeah, what we're going to be doing is different.

00:57 We're going to be applying this to our lower level integration level tests. So we want to keep things isolated. So every one of our tests, remember VTest runs all of our tests in parallel, at the same time in different processes, and we want to make sure that each one of those gets its own database.

01:16 So for the first couple of steps, you're going to just be running a single test, because if you run multiple, they're going to be talking to the same database, and that's a problem, and in fact, that's a problem that we have. Well, yeah, that's not something that we want to have. So yeah, eventually, we'll get to the point where they each have their own. Another question that people sometimes ask is,

01:35 SQLite actually supports in-memory databases, right? So why can't we just do an in-memory version of SQLite? That is definitely true, but there are two reasons we're not. For one, an easy reason is Prisma doesn't support in-memory databases yet. Eventually, they potentially will, but currently, that is not the case.

01:55 The second reason is I actually like being able to have that database written to disk so that I can see it, I can open it up if I need to, and so it is kind of nice to have that database there. Also, writing it to disk means that we can create a base database upon which we base everything else on,

02:14 and so things will actually be even faster. So even when the Prisma team does really support for in-memory databases with SQLite, I'm not sure that I'll adopt it. It kind of depends on how fast we can run migrations with that in-memory database, so we'll see. And not just migrations, but also seeds, and we'll talk about that, all of that stuff.

02:33 So we're gonna be doing this in our setup, and we're also introducing a new setup file for you that's a global setup, so look forward to that. It's gonna be fun. And then module import gotcha is gonna be a bit of a problem because you may not be aware of this, but the order in which you import modules actually does matter.

02:52 So importing module one before two is gonna be different from importing module two before one, and that's gonna have interesting implications for us with the environment variables and the fact that Prisma is looking for a particular environment variable. We'll talk about that. So another important thing to note is that you can do a dynamic import,

03:10 and then that allows you to do stuff, to do anything before that import happens, and we're gonna be doing that also. Important for you to know that, yeah, putting stuff above the imports will not work. The imports will always be evaluated before anything else in the module, so you can't just do that.

03:29 But a dynamic import, that will be evaluated later. And then you can also do a bare import, and that will happen first. So the module one will be evaluated first, and then do stuff will be evaluated, and then module two will be evaluated. So if you really, really had to do a non-dynamic import,

03:49 but you still needed to have something happen before that import happens, then you can make a separate module and import that one first. And we're gonna be doing that. So I think that you're ready for this exercise. Have a good time with it, and we'll see you when you're done.