prisma-examples icon indicating copy to clipboard operation
prisma-examples copied to clipboard

Unit tests in some examples

Open gustavoggsb opened this issue 4 years ago • 6 comments

I'm trying to write some unit tests to my NestJS application using Prisma, which is proving to be a real challenge. It would be good if we could have unit tests in the examples, so everyone would know how to do it properly.

gustavoggsb avatar Dec 27 '20 21:12 gustavoggsb

Hey @gustavoggsb, thanks a lot for this issue! It's a great idea to show people how to unit test their code when they use Prisma, so we definitely want to add that to our examples!

That being said, it would be super helpful if you could mention a couple of questions or issues you had when trying to get started with unit tests. What exactly was unclear to you? What issues did you run into when trying to set up unit tests in your app with Prisma? What questions popped up when you tried to write the unit tests.

It would be immensely helpful if you could write a bit more about this so that we can make sure all of these points are addressed when we add unit test examples 🙂

nikolasburk avatar Jan 05 '21 09:01 nikolasburk

Hello @Nikolasburk, thanks for your attention. I'm building a NestJs application, and I was using TypeORM and decided to migrate to Prisma. However, I really don't know how to properly mock the database calls to prevent accessing the database. With TypeORM, this is what I do to mock the repository (therefore the database):

https://github.com/buglandia/api/blob/89ccb38e6b9ff0bcb9bf9f7f29cc57fa427ad3e8/src/common/test/users/users.service.spec.ts

This uses @nestjs/typeorm.

I would like to understand how I could achieve a similar result (mock the database) when using Prisma.

gustavoggsb avatar Jan 09 '21 17:01 gustavoggsb

Thanks for the additional details, we'll take these into consideration when we're adding an example.

Also, not sure if it helps but @eddeee888 just published an article on testing with Prisma, maybe that's helpful for you as well.

nikolasburk avatar Jan 11 '21 10:01 nikolasburk

Since Prisma only allows one provider in the datasource block, a real world configuration would be helpful, showing how SQLite is used for unit tests e.g. for services and PostgreSQL for production.

DarkBitz avatar Apr 30 '21 19:04 DarkBitz

Has anyone have an example on how to write a unit test with prisma and NestJS? I'm switching from MongoDB/Mongoose to Postgres/Prisma. Can't really find examples sad face.

gimme4bucks avatar Nov 12 '21 10:11 gimme4bucks

Like @gimme4bucks I'd also be very happy for some examples. We want to test some services of our nestjs backend and check the data in a test database, probably run in docker.

kiliw avatar Dec 02 '21 13:12 kiliw