Drop database in tests


#1

Hi everyone,

I’m looking a way to drop the database before my unit tests however I only found CLI command prisma reset. Are there any command in prisma client in order to accomplish this?

Regards


#2

CLI command prisma delete is faster than prisma reset. Unsure if there is a prisma client alternative.


#3

Hi @BenoitRanque

I’m doing unit tests with node plus mocha and before every test, I would like to reset or drop the database like;

lab.experiment(‘webhook’, () => {

lab.beforeEach(async () => await prisma.dropDatabase());

lab.test(‘should receive message and save as feature’, async () => {

})

});

Sorry if i wasn’t clear enough.


#4

Hi @lucasgmartins

In one of my applications, I defined a custom function that clears out the Prisma service manually.

Lets say you have a User type and a Post type in your datamodel. You could write a function like the following:

function dropDatabase() {
  return Promise.all([
    prisma.deleteManyPosts();
    prisma.deleteManyUsers();
  ]);
}

Then you can call this function prior to seeding the database or wherever you need it:

lab.beforeEach(async () => {
  console.log(Clearing database...);
  await dropDatabase();
});

Hope that helps! Happy coding!


#5

Hi @IMax153

Actually, I’m doing this way too. I just wonder that should exist an auxiliary function, would be good.

But thanks very much for your answer!


#6

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.