prisma / prisma-examples

🚀 Ready-to-run Prisma example projects
https://www.prisma.io/docs/
Apache License 2.0
6.08k stars 1.42k forks source link

Unit tests in some examples #2378

Open gustavoggsb opened 3 years ago

gustavoggsb commented 3 years ago

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.

nikolasburk commented 3 years ago

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 🙂

gustavoggsb commented 3 years ago

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.

nikolasburk commented 3 years ago

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.

DarkBitz commented 3 years ago

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.

gimme4bucks commented 2 years ago

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.

kiliw commented 2 years ago

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.