Loading
Current section: Test Database 5 exercises
solution

Setting Up an Isolated Database for Tests

Loading solution

Transcript

00:00 Let's go to our setup testenv and right here we're going to do our import of the db setup ts right here again because we can't do any sort of setup stuff at this point because we already are importing stuff. So inside of this we want to set up environment variables and things like this

00:19 so we're going to create a database file that points to test prisma data db and then we need to get the full path of that so let's get a full database path with path join and that's going to come from the path module. I like to give it the node prefix just to communicate that a little bit

00:36 better and then we can set the database environment or database url to that full path and with that then we now are actually able to set up the database url pointing to the a test database.

00:53 So if I were to save all this and see our tests they're exploding because this we don't actually have a database there yet we need to create one that has all of our migrations applied and so that's what we're going to do here before all of our tests from v test we're going to have this

01:09 async function and this we're going to use execa execa command from execa and we'll await execa command there it is and we're going to say prisma migrate reset force skip c come on there we go

01:29 and so that we can get some output in our terminal that can be useful to see that we're going to say standard io inherit and sometimes v test will actually kind of hide that output but yeah you

01:43 might be able to see it and it is nice when this is running so awesome we took care of that took care of this and now if i hit save you'll notice yeah v test is doing some pretty weird things with the output because we're also trying to pipe out this output while it's running all those tests

02:03 and it's not totally finished we still have a couple of things that we need to do to to wrap this stuff up but we have the database created and you can take a look at that in test prisma data db so we are now testing against a different database which is quite nice

02:20 okay so after each test we need to delete all the users that were created in that test database so that these can be truly isolated so we'll say after each and here we're going to do a sync here

02:35 and this is coming from v test and so then we can say await prisma oh wait hmm this is going to be a problem right because we're importing prisma before we set the database url well we can

02:48 absolutely do a dynamic import here so const prisma equals await that import and now we can use this safely and we'll say user dot delete many we can delete all the users because this is just a

03:02 test database so it's no big deal so that's pretty cool okay so then um we're going to say after all the tests are done then let's actually delete that test database and we'll disconnect prisma so let's

03:17 grab this and we'll say disconnect um let's do async and let's make sure this after all imported from the right place sometimes it wants to import from note test which is really annoying

03:30 okay so we're disconnecting we also can bring in fs extra import fs extra whoops from fs extra

03:42 and then we can use that fs extra to remove the database from that full path awesome so now the output is still a giant mess and that's because we have a couple of things we need to adjust now because we're automatically cleaning up all of the users and everything so we can

04:02 actually get rid of a bunch of things if we go to our db utils right down here we no longer need to keep track of the inserted users we don't need to add that anymore at all so that's awesome we

04:16 can go to our setup test env that's no longer a thing this is no longer a thing that's pretty awesome we don't need that anymore apparently so yeah that's pretty cool and also our provider

04:31 callback test right in here we've got cody hanging out in here telling us that we don't need to use the utility necessarily like we we can if you want to continue to use the utility that's fine but i actually like using a nested create instead so we're going to say user create and

04:51 then we just stick the user data right here user data and now we can get rid of that we don't need to use that utility anymore i prefer the nested create personally and of course we can get rid of that import and let's see did it pass oh my gosh it passed that is awesome if i run it again

05:10 you're going to see the output looks super funny and there are a couple of interesting things with multiple tests running against the same database that can kind of cause problems so it's not going to pass every single time but we are close we're an inch closer to having totally isolated tests

05:26 thanks to the fact that we are creating a test database so it's not going to be touching our our development database and we're able to completely delete all the users that were created in the course of the test and then ultimately delete the entire file itself and so if we open

05:44 up prisma right here we rerun the test you'll be able to see yeah well it happened pretty fast but the database does get created and then it gets deleted later so we're getting pretty close next exercise step we're gonna get even closer but a good job on this one so just to give you kind of

06:03 an overview of everything we did here we created a database file path that's where we want our test database to be we got the full url for that or the full path for that so we don't have to worry

06:16 about relative paths being a problem and we set that with the file protocol for our database url and then before all of our tests run and we're going to run prisma migrate reset we're going to

06:31 say force so it doesn't ask us if we're sure we want to delete the data we skip the seed because our migrations handle all that stuff for us and we skip generating because we don't want to override our development client and that gets us a database that is all ready to go using this

06:50 environment variable that's happening automatically we don't have to do like manually set that environment variable we could say env and then spread process env but that's happening automatically so we don't need to worry about that and then after each one of our tests we're going to just

07:08 delete all the users that were created during the course of the test and then after all the tests are done then we can disconnect and remove that database and yeah that drastically simplified our test we don't need to keep track of our users anymore