3 ms·
I’ve been using Prisma for a while and I quite like it. Best ORM for TypeScript I’ve used. My biggest issue with it is testability. Sure, I can mock the Prisma
by brap 6y ago
I’ve been using Prisma for a while and I quite like it. Best ORM for TypeScript I’ve used.
My biggest issue with it is testability.
Sure, I can mock the Prisma client in tests with Jest or something, but if I want to test state I pretty much have to reimplement an in-memory DB using JS mocks.
I can also connect to a real DB made for testing but it’s quite overkill, I’m usually not interested in testing Prisma itself, just my own code.
I wish Prisma had test mode, where you could replace the client with a temporary in-memory SQLite DB without writing tons of boilerplate.
A drop-in replacement for PrismaClient for tests would have been wonderful.
- high_density 6y agousing tmpfs... it'll help a bit (I used mysql-on-tmpfs before)
- vbsteven 6y agoCould you elaborate on why you find a real DB overkill for testing purposes? Typically you would have a set of unit tests which test your code only and use mocks for external dependencies, in this case you would mock Prisma. Tests involving DB state are not unit tests anymore and require a bit more setup but most frameworks provide a test harness/runner to quickly spin up a part of your app with dependencies resolved (instead of mocked) so you can call controller/handler/service methods directly and verify their behavior. The boilerplate for this should mostly be limited to switching out an environment variable for the database connection and maybe setup some fixtures. Is this not possible in Prisma?
- ragnese 6y agoIsn't that pretty much always the dilemma with database stuff and testing? On the one hand, you can wrap your DB operations in some kind of mockable interface(s), and then test your business logic. On the other hand, you really need to test that the actual SQL ops work anyway- what if you screwed up a foreign key constraint?- that wouldn't show up in your business logic tests or your in-memory SQLite or whatever.
- matthewmueller 6y agoThanks for this feedback. We're very aware of this problem and are are actually working on a guide for working with Jest right now. It's a tricky problem because our use of the type-system is quite complex. I quite like your idea of a test mode or maybe a mock client that we generate.
- brap 6y ago> I quite like your idea of a test mode or maybe a mock client that we generate. Yes please!! :)
- nahtnam 6y agoI wonder if its possible to start a transaction at the beginning of each test and then rollback. That way we just need a db to run but it will never really be written to. Or maybe just invest in this: https://github.com/oguimbal/pg-mem/issues/27 https://github.com/oguimbal/pg-mem/issues/27 He has already put a lot of effort into it and I think with just a bit more support he can support Prisma